Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/NativeJSRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ namespace JsRuntime {
std::list<ApplicationDetails> getApplications();
void setExternalApplicationHandler(std::shared_ptr<IExternalApplicationHandler> handler);
std::string getBaseUserAgent();
void setLocation(std::stringstream& window, const std::string& url);
private:
Comment thread
gurpreet319 marked this conversation as resolved.
bool downloadFile(std::string& url, MemoryStruct& chunk);
void processDevConsoleRequests();
Comment thread
gurpreet319 marked this conversation as resolved.
Expand Down
73 changes: 68 additions & 5 deletions src/NativeJSRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,8 @@ void NativeJSRenderer::runApplicationInternal(ApplicationRequest& appRequest)
if(context->getModuleSettings().enableMiniJSDOM || context->getModuleSettings().enableJSDOM)
{
std::stringstream window;
window<<"window.location = {\"href\":\"" << url << "\"};";
NativeJSLogger::log(INFO, "Adding the window location: %s to js file\n", window.str().c_str());
setLocation(window, url);
NativeJSLogger::log(INFO, "Adding the window location: %s to js file\n", window.str().c_str());
context->runScript(window.str().c_str(),true, url, nullptr, true);
}
NativeJSLogger::log(INFO, "nativeJS application thunder execution url: %s\n", url.c_str());
Expand All @@ -377,12 +377,13 @@ void NativeJSRenderer::runApplicationInternal(ApplicationRequest& appRequest)
{
NativeJSLogger::log(INFO, "About to launch local app\n");
JavaScriptContext* context = (JavaScriptContext*)mContextMap[id].context;
context->setUrl(mContextMap[id].url);
context->setUrl(mContextMap[id].url);
if(context->getModuleSettings().enableMiniJSDOM || context->getModuleSettings().enableJSDOM)
{
std::stringstream window;
window<<"window.location = {\"href\":\"file:/" << url << "\"};";
NativeJSLogger::log(INFO, "Adding the window location: %s to js file\n", window.str().c_str());
std::string localUrl = "file://" + url;
Comment thread
gurpreet319 marked this conversation as resolved.
setLocation(window, localUrl);
NativeJSLogger::log(INFO, "Adding the window location: %s to js file\n", window.str().c_str());
context->runScript(window.str().c_str(),true, url, nullptr, true);
}
NativeJSLogger::log(INFO, "Running test application: %s\n", url.c_str());
Expand Down Expand Up @@ -689,4 +690,66 @@ std::string NativeJSRenderer::getBaseUserAgent()
{
return mBaseUserAgent;
}
void NativeJSRenderer::setLocation(std::stringstream& window, const std::string& url)
{
auto schemePos = url.find("://");

std::string protocol, host, hostname, port, pathname, search, hash, origin = "null";

if (schemePos != std::string::npos)
{
protocol = url.substr(0, schemePos) + ":";

auto authorityStart = schemePos + 3;
auto pathStart = url.find_first_of("/?#", authorityStart);

host = (pathStart == std::string::npos)
? url.substr(authorityStart)
: url.substr(authorityStart, pathStart - authorityStart);

auto portPos = host.find(':');
if (portPos != std::string::npos)
{
hostname = host.substr(0, portPos);
port = host.substr(portPos + 1);
}
else
{
hostname = host;
}

if (pathStart != std::string::npos)
{
auto queryPos = url.find('?', pathStart);
auto hashPos = url.find('#', pathStart);

auto pathEnd = std::min(
queryPos == std::string::npos ? url.size() : queryPos,
hashPos == std::string::npos ? url.size() : hashPos);
Comment thread
gurpreet319 marked this conversation as resolved.

pathname = url.substr(pathStart, pathEnd - pathStart);
Comment thread
gurpreet319 marked this conversation as resolved.

if (queryPos != std::string::npos)
search = url.substr(queryPos,
(hashPos == std::string::npos ? url.size() : hashPos) - queryPos);

if (hashPos != std::string::npos)
hash = url.substr(hashPos);
}

if (protocol != "file:")
origin = protocol + "//" + host;
}

window << "window.location={"
<< "\"href\":\"" << url << "\","
<< "\"protocol\":\"" << protocol << "\","
<< "\"host\":\"" << host << "\","
<< "\"hostname\":\"" << hostname << "\","
<< "\"port\":\"" << port << "\","
<< "\"pathname\":\"" << pathname << "\","
<< "\"search\":\"" << search << "\","
<< "\"hash\":\"" << hash << "\","
<< "\"origin\":\"" << origin << "\""
<< "};";
Comment thread
gurpreet319 marked this conversation as resolved.
}
79 changes: 79 additions & 0 deletions src/jsc/modules/linkedjsdomwrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ var jsdom = new JSDOM('<html></html>');
document = jsdom.document;
global.document = document;
window = jsdom.window;
global.window = window;
Event = window.Event;
DOMParser = window.DOMParser;
navigator = window.navigator;
global.navigator = navigator;
tv = window.tv = {}
//fetch = FetchLib;
try
Expand Down Expand Up @@ -84,3 +86,80 @@ crypto.getRandomValues = getRandom;

setInterval(checkUrlChange, 500);
})();

(function() {

function run(code, url) {
try {
(0, eval)(code + "\n//# sourceURL=" + url);
Comment thread
gurpreet319 marked this conversation as resolved.
Dismissed
return null;
} catch (e) {
return e;
}
}

function load(url, ok, fail) {
var x = new XMLHttpRequest();
x.open("GET", url, true);

x.onreadystatechange = function() {
if (x.readyState !== 4) return;

if (x.status >= 200 && x.status < 300)
ok(x.responseText || "");
else
fail(new Error("HTTP " + x.status));
};

x.onerror = fail;
x.send();
}

var orig = HTMLElement.prototype.appendChild;

HTMLElement.prototype.appendChild = function(node) {

var ret = orig.call(this, node);

if (
node &&
node.tagName &&
node.tagName.toLowerCase() === "script" &&
node.src
) {
load(
node.src,
function(code) {

var err = run(code, node.src);

if (err)
return node.onerror && node.onerror(err);

node.readyState = "complete";

if (typeof node.onreadystatechange === "function")
node.onreadystatechange.call(node);

if (typeof node.dispatchEvent === "function" && typeof Event === "function") {
node.dispatchEvent(new Event("load"));
} else if (
typeof node.onload === "function" &&
node.onload !== node.onreadystatechange
) {
node.onload.call(node);
}
},
function(err) {
if (typeof node.dispatchEvent === "function" && typeof Event === "function") {
node.dispatchEvent(new Event("error"));
} else if (typeof node.onerror === "function") {
node.onerror(err);
}
}
Comment thread
Copilot marked this conversation as resolved.
);
}

return ret;
};
})();