Skip to content

Commit

Permalink
Check for valid script as content is received
Browse files Browse the repository at this point in the history
  • Loading branch information
Sxderp committed Dec 11, 2017
1 parent 8debfa8 commit 742b6b2
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions src/bg/user-script-detect.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ const userScriptTypes = {
};


// Check if enough content is available to open an install message
function checkScript(userScriptContent, url, tabId) {
let scriptDetails = parseUserScript(userScriptContent, url, true);
if (scriptDetails) {
let installUrl = browser.runtime.getURL('src/content/install-dialog.html')
+ '?' + escape(JSON.stringify(scriptDetails));
browser.tabs.update(tabId, {'url': installUrl, 'loadReplace': true});
return true;
} else {
return false;
}
}


function detectUserScriptOnHeadersReceived(details) {
let hasContentType = false;
for (header of details.responseHeaders) {
Expand All @@ -38,35 +52,26 @@ function detectUserScriptOnHeadersReceived(details) {
let userScriptUrl = details.url;
let userScriptContent = '';

// Not sure if this is called more than once. If it's only called once
// then just use event.data directly rather than concatenating and then
// doing the checks in onstop.
filter.ondata = event => {
userScriptContent = userScriptContent
+ decoder.decode(event.data, {'stream': true});
if (checkScript(userScriptContent, details.url, details.tabId)) {
// We have enough for the details. Since a entire tab is currently used
// for the install dialog, close the filter so the request stops
// processing.
filter.close();
}
};
filter.onstop = event => {
let userScriptDetails;
try {
userScriptDetails = parseUserScript(userScriptContent, userScriptUrl, true);
} catch (err) {
console.log('Error parsing user script:', userScriptUrl, err);
// One last check to see if we have a valid script.
if (checkScript(userScriptContent, details.url, details.tabId)) {
// Close the filter without further processing.
filter.close();
} else {
// No script, flush our content to the filter and close.
filter.write(encoder.encode(userScriptContent));
filter.disconnect();
return;
filter.close();
}

if (userScriptDetails === null) {
console.log('User script details were null:', userScriptUrl);
filter.write(encoder.encode(userScriptContent));
filter.disconnect();
return;
}

let installUrl = browser.runtime.getURL('src/content/install-dialog.html')
+ '?' + escape(JSON.stringify(userScriptDetails));
browser.tabs.update(details.tabId, {'url': installUrl, 'loadReplace': true});
filter.close();
};

return {};
Expand Down

0 comments on commit 742b6b2

Please sign in to comment.