| |
@@ -28,6 +28,7 @@
|
| |
var {ResponseProcessor} = require("./bg/ResponseProcessor");
|
| |
var {Storage, ListStore} = require("./bg/Storage");
|
| |
var {ListManager} = require("./bg/ListManager");
|
| |
+ var {ExternalLicenses} = require("./bg/ExternalLicenses");
|
| |
|
| |
console.log("main_background.js");
|
| |
/**
|
| |
@@ -152,18 +153,21 @@
|
| |
|
| |
var activeMessagePorts = {};
|
| |
var activityReports = {};
|
| |
- function createReport(initializer = null) {
|
| |
+ async function createReport(initializer = null) {
|
| |
let template = {
|
| |
"accepted": [],
|
| |
"blocked": [],
|
| |
"blacklisted": [],
|
| |
"whitelisted": [],
|
| |
"unknown": [],
|
| |
- url: "",
|
| |
};
|
| |
if (initializer) {
|
| |
template = Object.assign(template, initializer);
|
| |
+ if (!template.url && initializer.tabId) {
|
| |
+ template.url = (await browser.tabs.get(initializer.tabId)).url;
|
| |
+ }
|
| |
}
|
| |
+
|
| |
template.site = ListStore.siteItem(template.url);
|
| |
template.siteStatus = listManager.getStatus(template.site);
|
| |
return template;
|
| |
@@ -177,7 +181,7 @@
|
| |
async function openReportInTab(data) {
|
| |
let popupURL = await browser.browserAction.getPopup({});
|
| |
let tab = await browser.tabs.create({url: `${popupURL}#fromTab=${data.tabId}`});
|
| |
- activityReports[tab.id] = createReport(data);
|
| |
+ activityReports[tab.id] = await createReport(data);
|
| |
}
|
| |
|
| |
/**
|
| |
@@ -222,9 +226,9 @@
|
| |
* Make sure it will use the right URL when refering to a certain script.
|
| |
*
|
| |
*/
|
| |
- function updateReport(tabId, oldReport, updateUI = false){
|
| |
+ async function updateReport(tabId, oldReport, updateUI = false){
|
| |
let {url} = oldReport;
|
| |
- let newReport = createReport({url, tabId});
|
| |
+ let newReport = await createReport({url, tabId});
|
| |
for (let property of Object.keys(oldReport)) {
|
| |
let entries = oldReport[property];
|
| |
if (!Array.isArray(entries)) continue;
|
| |
@@ -263,10 +267,10 @@
|
| |
* Make sure it will use the right URL when refering to a certain script.
|
| |
*
|
| |
*/
|
| |
- async function addReportEntry(tabId, scriptHashOrUrl, action, update = false) {
|
| |
+ async function addReportEntry(tabId, scriptHashOrUrl, action) {
|
| |
let report = activityReports[tabId];
|
| |
if (!report) report = activityReports[tabId] =
|
| |
- createReport({url: (await browser.tabs.get(tabId)).url});
|
| |
+ await createReport({tabId});
|
| |
|
| |
let type, actionValue;
|
| |
for (type of ["accepted", "blocked", "whitelisted", "blacklisted"]) {
|
| |
@@ -312,7 +316,7 @@
|
| |
}
|
| |
|
| |
browser.sessions.setTabValue(tabId, report.url, report);
|
| |
-
|
| |
+ updateBadge(tabId, report);
|
| |
return entryType;
|
| |
}
|
| |
|
| |
@@ -397,7 +401,7 @@
|
| |
p.postMessage({"show_info": activityReports[tab.id]});
|
| |
} else{
|
| |
// create a new entry
|
| |
- let report = activityReports[tab.id] = createReport({"url": tab.url, tabId: tab.id});
|
| |
+ let report = activityReports[tab.id] = await createReport({"url": tab.url, tabId: tab.id});
|
| |
p.postMessage({show_info: report});
|
| |
dbg_print(`[TABID: ${tab.id}] No data found, creating a new entry for this window.`);
|
| |
}
|
| |
@@ -781,7 +785,7 @@
|
| |
|
| |
let sourceHash = hash(response);
|
| |
let domain = get_domain(url);
|
| |
- let report = activityReports[tabId] || (activityReports[tabId] = createReport({url, tabId}));
|
| |
+ let report = activityReports[tabId] || (activityReports[tabId] = await createReport({tabId}));
|
| |
updateBadge(tabId, report, !verdict);
|
| |
let category = await addReportEntry(tabId, sourceHash, {"url": domain, [verdict ? "accepted" : "blocked"]: [url, reason]});
|
| |
let scriptSource = verdict ? response : editedSource;
|
| |
@@ -903,8 +907,23 @@
|
| |
*/
|
| |
async function handle_script(response, whitelisted){
|
| |
let {text, request} = response;
|
| |
- let {url, tabId} = request;
|
| |
+ let {url, tabId, frameId} = request;
|
| |
url = ListStore.urlItem(url);
|
| |
+ if (!whitelisted) {
|
| |
+ let scriptInfo = await ExternalLicenses.check({url, tabId, frameId});
|
| |
+ if (scriptInfo) {
|
| |
+ let verdict;
|
| |
+ let msg = scriptInfo.toString();
|
| |
+ if (scriptInfo.allFree) {
|
| |
+ verdict = "accepted";
|
| |
+ } else {
|
| |
+ verdict = "blocked";
|
| |
+ text = `/* ${msg} */`;
|
| |
+ }
|
| |
+ addReportEntry(tabId, url, {url, [verdict]: [url, msg]});
|
| |
+ return text;
|
| |
+ }
|
| |
+ }
|
| |
let edited = await get_script(text, url, tabId, whitelisted, -2);
|
| |
return Array.isArray(edited) ? edited[0] : edited;
|
| |
}
|
| |
@@ -985,7 +1004,10 @@
|
| |
|
| |
var parser = new DOMParser();
|
| |
var html_doc = parser.parseFromString(html, "text/html");
|
| |
-
|
| |
+
|
| |
+ // moves external licenses reference, if any, before any <SCRIPT> element
|
| |
+ ExternalLicenses.optimizeDocument(html_doc);
|
| |
+
|
| |
var amt_scripts = 0;
|
| |
var total_scripts = 0;
|
| |
var scripts = html_doc.scripts;
|
| |
@@ -1077,12 +1099,11 @@
|
| |
async function handle_html(response, whitelisted) {
|
| |
let {text, request} = response;
|
| |
let {url, tabId, type} = request;
|
| |
- url = ListStore.urlItem(url);
|
| |
if (type === "main_frame") {
|
| |
- activityReports[tabId] = createReport({url, tabId});
|
| |
+ activityReports[tabId] = await createReport({url, tabId});
|
| |
updateBadge(tabId);
|
| |
}
|
| |
- return await edit_html(text, url, tabId, whitelisted);
|
| |
+ return await edit_html(text, ListStore.urlItem(url), tabId, whitelisted);
|
| |
}
|
| |
|
| |
var whitelist = new ListStore("pref_whitelist", Storage.CSV);
|
| |
Crude example page with two scripts, one declaring all free licenses (accepted) and one declaring mixed licenses (blocked) here: https://noscript.net/test/librejs/weblabels/