Repository navigation
Embedded webextension isolated #11792
Changes from all commits
7dbab77
9fbfe76
28f49a1
9283142
bd23706
3386b94
dea6955
1de4f50
c2b7b83
32bd342
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,7 @@ | ||
| { | ||
| "parserOptions": { | ||
| "ecmaVersion": 2017 | ||
| }, | ||
| "env": { | ||
| "browser": true, | ||
| "commonjs": true, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,14 @@ storage.get({enableMixedRulesets: false}, function(item) { | |
| all_rules.addFromXml(loadExtensionFile('rules/default.rulesets', 'xml')); | ||
| }); | ||
|
|
||
| // Load in the legacy custom rulesets, if any | ||
| function load_legacy_custom_rulesets(legacy_custom_rulesets){ | ||
| for(let legacy_custom_ruleset of legacy_custom_rulesets){ | ||
| all_rules.addFromXml((new DOMParser()).parseFromString(legacy_custom_ruleset, 'text/xml')); | ||
| } | ||
| } | ||
| storage.get({legacy_custom_rulesets: []}, item => load_legacy_custom_rulesets(item.legacy_custom_rulesets)); | ||
|
|
||
| var USER_RULE_KEY = 'userRules'; | ||
| // Records which tabId's are active in the HTTPS Switch Planner (see | ||
| // devtools-panel.js). | ||
|
|
@@ -181,6 +189,7 @@ var removeRule = function(ruleset) { | |
| * */ | ||
| function AppliedRulesets() { | ||
| this.active_tab_rules = {}; | ||
| this.active_tab_hostnames = {}; | ||
|
|
||
| var that = this; | ||
| if (chrome.tabs) { | ||
|
|
@@ -192,12 +201,8 @@ function AppliedRulesets() { | |
|
|
||
| AppliedRulesets.prototype = { | ||
| addRulesetToTab: function(tabId, ruleset) { | ||
| if (tabId in this.active_tab_rules) { | ||
| this.active_tab_rules[tabId][ruleset.name] = ruleset; | ||
| } else { | ||
| this.active_tab_rules[tabId] = {}; | ||
| this.active_tab_rules[tabId][ruleset.name] = ruleset; | ||
| } | ||
| this.active_tab_rules[tabId] = this.active_tab_rules[tabId] || {}; | ||
| this.active_tab_rules[tabId][ruleset.name] = ruleset; | ||
| }, | ||
|
|
||
| getRulesets: function(tabId) { | ||
|
|
@@ -207,8 +212,21 @@ AppliedRulesets.prototype = { | |
| return null; | ||
| }, | ||
|
|
||
| addHostnameToTab: function(tabId, ruleset_name, hostname) { | ||
| this.active_tab_hostnames[tabId] = this.active_tab_hostnames[tabId] || {}; | ||
| this.active_tab_hostnames[tabId][ruleset_name] = hostname; | ||
| }, | ||
|
|
||
| getHostnames: function(tabId) { | ||
| if (tabId in this.active_tab_hostnames) { | ||
| return this.active_tab_hostnames[tabId]; | ||
| } | ||
| return null; | ||
| }, | ||
|
|
||
| removeTab: function(tabId) { | ||
| delete this.active_tab_rules[tabId]; | ||
| delete this.active_tab_hostnames[tabId]; | ||
| } | ||
| }; | ||
|
|
||
|
|
@@ -292,6 +310,7 @@ function onBeforeRequest(details) { | |
|
|
||
| for (let ruleset of potentiallyApplicable) { | ||
| activeRulesets.addRulesetToTab(details.tabId, ruleset); | ||
| activeRulesets.addHostnameToTab(details.tabId, ruleset.name, uri.hostname); | ||
| if (ruleset.active && !newuristr) { | ||
| newuristr = ruleset.apply(canonical_url); | ||
| } | ||
|
|
@@ -645,8 +664,11 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){ | |
| sendResponse(isExtensionEnabled); | ||
| } else if (message.type == "delete_from_ruleset_cache") { | ||
| all_rules.ruleCache.delete(message.object); | ||
| } else if (message.type == "get_active_rulesets") { | ||
| sendResponse(activeRulesets.getRulesets(message.object)); | ||
| } else if (message.type == "get_active_rulesets_and_hostnames") { | ||
| sendResponse({ | ||
| rulesets: activeRulesets.getRulesets(message.object), | ||
| hostnames: activeRulesets.getHostnames(message.object) | ||
| }); | ||
| } else if (message.type == "set_ruleset_active_status") { | ||
| var ruleset = activeRulesets.getRulesets(message.object.tab_id)[message.object.name]; | ||
| ruleset.active = message.object.active; | ||
|
|
@@ -660,5 +682,47 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){ | |
| updateState(); | ||
| } else if (message.type == "remove_rule") { | ||
| removeRule(message.object); | ||
| } else if (message.type == "import_settings") { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a comment explaining when this is used? I.e., importing from the Options page rather than automatically from the parent extension?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. |
||
| // This is used when importing settings from the options ui | ||
| import_settings(message.object).then(() => { | ||
| sendResponse(true); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| // Send a message to the embedded webextension bootstrap.js to get settings to import | ||
| chrome.runtime.sendMessage("import-legacy-data", import_settings); | ||
|
|
||
| /** | ||
| * Import extension settings (custom rulesets, ruleset toggles, globals) from an object | ||
| * @param settings the settings object | ||
| */ | ||
| async function import_settings(settings){ | ||
| if(settings.changed){ | ||
| // Load custom rulesets and add to storage | ||
| await new Promise(resolve => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need to wait until the settings are stored?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @koops76 we do for the options ui. We only want to display the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then use |
||
| storage.set({"legacy_custom_rulesets": settings.custom_rulesets}, resolve); | ||
| }); | ||
|
|
||
| // Load all the ruleset toggles into memory and store | ||
| let rule_toggle_promises = []; | ||
| for(let ruleset_name in settings.rule_toggle){ | ||
| localStorage[ruleset_name] = settings.rule_toggle[ruleset_name]; | ||
| } | ||
|
|
||
| all_rules = new RuleSets(localStorage); | ||
| all_rules.addFromXml(loadExtensionFile('rules/default.rulesets', 'xml')); | ||
| load_legacy_custom_rulesets(settings.custom_rulesets); | ||
|
|
||
| // Set/store globals | ||
| await new Promise(resolve => { | ||
| storage.set({'httpNowhere': settings.prefs.http_nowhere_enabled}, resolve); | ||
| }); | ||
| await new Promise(resolve => { | ||
| storage.set({'showCounter': settings.prefs.show_counter}, resolve); | ||
| }); | ||
| await new Promise(resolve => { | ||
| storage.set({'globalEnabled': settings.prefs.global_enabled}, resolve); | ||
| }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| .section-header{ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a space between the |
||
| margin-bottom: 10px; | ||
| } | ||
|
|
||
| .section-header-span{ | ||
| border-bottom: 1px solid #ccc; | ||
| font-size: 15px; | ||
| } | ||
|
|
||
| #import{ | ||
| margin-bottom: 10px; | ||
| float: right; | ||
| } | ||
|
|
||
| #import-confirmed{ | ||
| display: none; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <meta charset="utf-8"> | ||
| <link href="options.css" rel="stylesheet" type="text/css"/> | ||
| <script src="options.js"></script> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move the scripts to the end of |
||
| <script src="translation.js"></script> | ||
| <script src="send-message.js"></script> | ||
| </head> | ||
| <body> | ||
| <div id='import-confirmed' i18n="options_imported"></div> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
|
|
||
| <form> | ||
| <div class="section-header"><span class="section-header-span" i18n="options_importSettings"></span></div> | ||
| <section id="ImportSettings" class="options"> | ||
| <input id="import-settings" type="file" accept="application/json" value="importSettings" /> | ||
| </section> | ||
| <button type="submit" id="import" i18n="options_import" disabled></button> | ||
| </form> | ||
|
|
||
| </body> | ||
| </html> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| document.addEventListener("DOMContentLoaded", () => { | ||
|
|
||
| let json_data; | ||
| let import_button = document.querySelector("#import"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| function import_json(e) { | ||
| e.preventDefault(); | ||
|
|
||
| let settings = JSON.parse(json_data); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| sendMessage("import_settings", settings, resp => { | ||
| document.querySelector("#import-confirmed").style.display = "block"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| document.querySelector("form").style.display = "none"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe find a |
||
| }); | ||
| } | ||
|
|
||
| document.querySelector("#import-settings").addEventListener("change", event => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| const file = event.target.files[0]; | ||
| const reader = new FileReader(); | ||
| reader.onload = event => { | ||
| json_data = event.target.result; | ||
| import_button.disabled = false; | ||
| }; | ||
|
|
||
| reader.readAsText(file); | ||
| }); | ||
|
|
||
| document.querySelector("form").addEventListener("submit", import_json); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe find a |
||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,8 @@ | |
|
|
||
| <link href="popup.css" rel="stylesheet" type="text/css"/> | ||
| <script src="popup.js"></script> | ||
| <script src="translation.js"></script> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move the scripts to the end of |
||
| <script src="send-message.js"></script> | ||
|
|
||
| </head> | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,7 +41,7 @@ function toggleRuleLine(checkbox, ruleset, tab_id) { | |
| * @param ruleset the ruleset to build the line for | ||
| * @returns {*} | ||
| */ | ||
| function appendRuleLineToListDiv(ruleset, list_div) { | ||
| function appendRuleLineToListDiv(ruleset, hostnames, tab_id, list_div) { | ||
|
|
||
| // parent block for line | ||
| var line = document.createElement("div"); | ||
|
|
@@ -72,6 +72,9 @@ function appendRuleLineToListDiv(ruleset, list_div) { | |
| break; | ||
| } | ||
| } | ||
| if (favicon.src == "chrome://favicon/") { | ||
| favicon.src += "https://" + hostnames[ruleset.name]; | ||
| } | ||
|
|
||
| if (navigator.userAgent.match("Chrome")) { | ||
| var xhr = new XMLHttpRequest(); | ||
|
|
@@ -148,13 +151,16 @@ function toggleEnabledDisabled() { | |
| function gotTab(tabArray) { | ||
| var activeTab = tabArray[0]; | ||
|
|
||
| sendMessage("get_active_rulesets", activeTab.id, function(rulesets){ | ||
| sendMessage("get_active_rulesets_and_hostnames", activeTab.id, function(rulesets_and_hostnames){ | ||
| var rulesets = rulesets_and_hostnames.rulesets; | ||
| var hostnames = rulesets_and_hostnames.hostnames; | ||
|
|
||
| for (var r in rulesets) { | ||
| var listDiv = stableRules; | ||
| if (!rulesets[r].default_state) { | ||
| listDiv = unstableRules; | ||
| } | ||
| appendRuleLineToListDiv(rulesets[r], listDiv); | ||
| appendRuleLineToListDiv(rulesets[r], hostnames, activeTab.id, listDiv); | ||
| listDiv.style.position = "static"; | ||
| listDiv.style.visibility = "visible"; | ||
| } | ||
|
|
@@ -191,16 +197,6 @@ document.addEventListener("DOMContentLoaded", function () { | |
| httpNowhereCheckbox.setAttribute('checked', ''); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| }); | ||
|
|
||
| // auto-translate all elements with i18n attributes | ||
| var elem = document.querySelectorAll("[i18n]"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| for (let el of elem) { | ||
| el.innerHTML = chrome.i18n.getMessage(el.getAttribute("i18n")); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| } | ||
|
|
||
| // other translations | ||
| e("aboutTitle").setAttribute("title", chrome.i18n.getMessage("about_title")); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| e("add-rule-link").addEventListener("click", addManualRule); | ||
| }); | ||
|
|
||
|
|
||
|
|
@@ -278,12 +274,3 @@ function setOption_(opt, value) { | |
| details[opt] = value; | ||
| sendMessage("set_option", details); | ||
| } | ||
|
|
||
| function sendMessage(type, object, callback) { | ||
| var packet = {}; | ||
| packet.type = type; | ||
| if(object){ | ||
| packet.object = object; | ||
| } | ||
| chrome.runtime.sendMessage(packet, callback); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| function sendMessage(type, object, callback) { | ||
| var packet = {}; | ||
| packet.type = type; | ||
| if(object){ | ||
| packet.object = object; | ||
| } | ||
| chrome.runtime.sendMessage(packet, callback); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| document.addEventListener("DOMContentLoaded", function () { | ||
| // auto-translate all elements with i18n attributes | ||
| var elem = document.querySelectorAll("[i18n]"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| for (let el of elem) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| el.innerHTML = chrome.i18n.getMessage(el.getAttribute("i18n")); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| // other translations | ||
| e("aboutTitle").setAttribute("title", chrome.i18n.getMessage("about_title")); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| e("add-rule-link").addEventListener("click", addManualRule); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the "addHostnameToTab" work snuck back in by accident.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jsha this is just because this was forked from the first iteration of
firefox-webextensions-core- should I rebase it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could rebase from
master, now thatfirefox-webextensions-coreis merged.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Hainish Rebase it I guess.