diff --git a/chromium/background.js b/chromium/background.js index 4553669cdc12..92b7c25ce31f 100644 --- a/chromium/background.js +++ b/chromium/background.js @@ -52,23 +52,31 @@ var switchPlannerInfo = {}; // Is HTTPSe enabled, or has it been manually disabled by the user? var isExtensionEnabled = true; -// Load prefs about whether http nowhere is on. Structure is: -// { httpNowhere: true/false } -var httpNowhereOn = false; -storage.get({httpNowhere: false}, function(item) { - httpNowhereOn = item.httpNowhere; +// Load preferences. Structure is: +// { httpNowhere: Boolean, showAppliedCount: Boolean } + +let httpNowhere = false; +let showAppliedCount = true; + +storage.get({ httpNowhere: false, showAppliedCount: true }, item => { + httpNowhere = item.httpNowhere + showAppliedCount = item.showAppliedCount; updateState(); }); -chrome.storage.onChanged.addListener(function(changes, areaName) { + +chrome.storage.onChanged.addListener((changes, areaName) => { if (areaName === 'sync' || areaName === 'local') { - for (var key in changes) { - if (key === 'httpNowhere') { - httpNowhereOn = changes[key].newValue; - updateState(); - } + if ('httpNowhere' in changes) { + httpNowhere = changes.httpNowhere.newValue; + updateState(); + } + if ('showAppliedCount' in changes) { + showAppliedCount = changes.showAppliedCount.newValue; + updateState(); } } }); + if (chrome.tabs) { chrome.tabs.onActivated.addListener(function() { updateState(); @@ -110,6 +118,25 @@ var loadStoredUserRules = function() { loadStoredUserRules(); +function getActiveRulesetCount(id) { + const applied = activeRulesets.getRulesets(id); + + if (!applied) + { + return 0; + } + + let activeCount = 0; + + for (const key in applied) { + if (applied[key].active) { + activeCount++; + } + } + + return activeCount; +} + /** * Set the icon color correctly * inactive: extension is enabled, but no rules were triggered on this page. @@ -121,17 +148,19 @@ var updateState = function() { if (!chrome.tabs) { return; } - chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { + chrome.tabs.query({active: true, lastFocusedWindow: true}, function(tabs) { if (!tabs || tabs.length === 0) { return; } - var applied = activeRulesets.getRulesets(tabs[0].id) - var iconState = "inactive"; + + const activeCount = getActiveRulesetCount(tabs[0].id); + let iconState = "inactive"; + if (!isExtensionEnabled) { iconState = "disabled"; - } else if (httpNowhereOn) { + } else if (httpNowhere) { iconState = "blocking"; - } else if (applied) { + } else if (activeCount > 0) { iconState = "active"; } chrome.browserAction.setIcon({ @@ -142,6 +171,12 @@ var updateState = function() { chrome.browserAction.setTitle({ title: "HTTPS Everywhere (" + iconState + ")" }); + + chrome.browserAction.setBadgeBackgroundColor({color: "#00cc00"}); + + const showBadge = activeCount > 0 && isExtensionEnabled && showAppliedCount; + + chrome.browserAction.setBadgeText({text: showBadge ? "" + activeCount : ""}); }); } @@ -245,7 +280,7 @@ function onBeforeRequest(details) { // Should the request be canceled? var shouldCancel = ( - httpNowhereOn && + httpNowhere && uri.protocol === 'http:' && !/\.onion$/.test(uri.hostname) && !/^localhost$/.test(uri.hostname) && @@ -305,6 +340,8 @@ function onBeforeRequest(details) { } } + updateState(); + if (newuristr && using_credentials_in_url) { // re-insert userpass info which was stripped temporarily const uri_with_credentials = new URL(newuristr); @@ -323,7 +360,7 @@ function onBeforeRequest(details) { newuristr); } - if (httpNowhereOn) { + if (httpNowhere) { // If loading a main frame, try the HTTPS version as an alternative to // failing. if (shouldCancel) { diff --git a/chromium/options.css b/chromium/options.css index 1e9f0418e2b9..0252f2bce0e4 100644 --- a/chromium/options.css +++ b/chromium/options.css @@ -1,17 +1,17 @@ -.section-header{ +.section-header { margin-bottom: 10px; } -.section-header-span{ +.section-header-span { border-bottom: 1px solid #ccc; font-size: 15px; } -#import{ +#import { margin-bottom: 10px; float: right; } -#import-confirmed{ +#import-confirmed { display: none; } diff --git a/chromium/options.html b/chromium/options.html index 3a7ae116aa33..dd3f5ff2978e 100644 --- a/chromium/options.html +++ b/chromium/options.html @@ -1,22 +1,33 @@ - + - - - - + -
+
+ + + + +
+ +
-
-
-
- -
- -
+
+ +
+
+ +
+ + + + + + + + diff --git a/chromium/options.js b/chromium/options.js index d979c504e0f2..9383fda1d691 100644 --- a/chromium/options.js +++ b/chromium/options.js @@ -1,28 +1,63 @@ -document.addEventListener("DOMContentLoaded", () => { - - let json_data; - let import_button = document.querySelector("#import"); - - function import_json(e) { - e.preventDefault(); - - let settings = JSON.parse(json_data); - sendMessage("import_settings", settings, resp => { - document.querySelector("#import-confirmed").style.display = "block"; - document.querySelector("form").style.display = "none"; - }); - } - - document.querySelector("#import-settings").addEventListener("change", event => { - 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); -}); +'use strict' + +function readFile (file) { + return new Promise((resolve, reject) => { + const reader = new FileReader() + + reader.addEventListener('error', event => { + reject(new Error('FileReader error: ' + event.target.error.name)) + }) + + reader.addEventListener('load', event => { + resolve(event.target.result) + }) + + reader.readAsText(file) + }) +} + +function saveFile (blob, fileName) { + const blobUrl = URL.createObjectURL(blob) + + const anchor = document.createElement('a') + anchor.href = blobUrl + anchor.download = fileName + anchor.click() +} + +document.addEventListener('DOMContentLoaded', () => { + const showAppliedCountCheckbox = document.getElementById('show-applied-count') + + sendMessage('get_option', { showAppliedCount: true }).then(item => { + showAppliedCountCheckbox.checked = item.showAppliedCount + showAppliedCountCheckbox.addEventListener('change', event => { + sendMessage('set_option', { showAppliedCount: showAppliedCountCheckbox.checked }) + }) + }) + + const importButton = document.getElementById('import') + const importSettings = document.getElementById('import-settings') + + importButton.addEventListener('click', event => { + const file = importSettings.files[0] + + readFile(file).then(data => { + const settings = JSON.parse(fileContents) + sendMessage('import_settings', settings) + }) + }) + + document.getElementById('import-settings').addEventListener('change', event => { + importButton.disabled = (event.target.files.length === 0) + }) + + document.getElementById('rulesetSettingsExport').addEventListener('click', () => { + const json = JSON.stringify(localStorage) + const blob = new Blob([json], { type: 'application/json' }) + saveFile(blob, 'settings.json') + }) + + document.getElementById('rulesetSettingsImport').addEventListener('click', () => { + + }) +}) diff --git a/chromium/popup.css b/chromium/popup.css index 82c3dbb35b7e..38e33853a9e9 100644 --- a/chromium/popup.css +++ b/chromium/popup.css @@ -18,8 +18,7 @@ h1, h2, h3, .rule { /* Initially hide section (until rules get added). */ section.rules { - position: fixed; - visibility: hidden; + display: none; } .rule{ diff --git a/chromium/popup.html b/chromium/popup.html index b6c92d4cf855..9f24b216b526 100644 --- a/chromium/popup.html +++ b/chromium/popup.html @@ -1,6 +1,7 @@ + - + @@ -16,58 +17,58 @@
-

+

- +
- +
- +
-

-

+

+

-

-

+

+

diff --git a/chromium/popup.js b/chromium/popup.js index 1aeea420ccc2..771061595a89 100644 --- a/chromium/popup.js +++ b/chromium/popup.js @@ -1,270 +1,208 @@ -"use strict"; - -var stableRules = null; -var unstableRules = null; -var hostReg = /.*\/\/[^$/]*\//; - -function e(id) { - return document.getElementById(id); -} +'use strict' /** * Handles rule (de)activation in the popup * @param checkbox checkbox being clicked * @param ruleset the ruleset tied tot he checkbox */ -function toggleRuleLine(checkbox, ruleset, tab_id) { - var ruleset_active = checkbox.checked; - var set_ruleset = { - active: ruleset_active, +function setRulesetActive (ruleset, tabId, active) { + const rulesetData = { + active, name: ruleset.name, - tab_id: tab_id - }; - - sendMessage("set_ruleset_active_status", set_ruleset, function(){ + tab_id: tabId + } - if (ruleset_active != ruleset.default_state) { - localStorage[ruleset.name] = ruleset_active; + sendMessage('set_ruleset_active_status', rulesetData).then(() => { + if (active !== ruleset.default_state) { + localStorage[ruleset.name] = active } else { - delete localStorage[ruleset.name]; - // purge the name from the cache so that this unchecking is persistent. - sendMessage("delete_from_ruleset_cache", ruleset.name); + delete localStorage[ruleset.name] + + // Purge the name from the cache so that this unchecking is persistent. + sendMessage('delete_from_ruleset_cache', ruleset.name) } // Now reload the selected tab of the current window. - chrome.tabs.reload(); - }); + chrome.tabs.reload() + }) } +const hostReg = /.*\/\/[^$/]*\// + /** * Creates a rule line (including checkbox and icon) for the popup * @param ruleset the ruleset to build the line for * @returns {*} */ -function appendRuleLineToListDiv(ruleset, list_div) { - +function appendRuleLineToListDiv (ruleset, tabId, listDiv) { // parent block for line - var line = document.createElement("div"); - line.className = "rule checkbox"; + const line = document.createElement('div') + line.className = 'rule checkbox' - // label "container" - var label = document.createElement("label"); + // label 'container' + const label = document.createElement('label') // checkbox - var checkbox = document.createElement("input"); - checkbox.type = "checkbox"; - if (ruleset.active) { - checkbox.setAttribute("checked", ""); - } - checkbox.onchange = function(ev) { - toggleRuleLine(checkbox, ruleset, tab_id); - }; - label.appendChild(checkbox); - - // favicon (from chrome's cache) - var favicon = document.createElement("img"); - favicon.className = "favicon"; - favicon.src = "chrome://favicon/"; - for (let rule of ruleset.rules) { - var host = hostReg.exec(rule.to); - if (host) { - favicon.src += host[0]; - break; + const checkbox = document.createElement('input') + checkbox.type = 'checkbox' + checkbox.checked = ruleset.active + + checkbox.addEventListener('change', () => { + setRulesetActive(ruleset, tabId, checkbox.checked) + }) + + label.appendChild(checkbox) + + // Temporarily disabled until we will find a better way. + + if (false && navigator.userAgent.match('Chrome')) { + // favicon (from chrome's cache) + const favicon = document.createElement('img') + favicon.className = 'favicon' + + for (const rule of ruleset.rules) { + const host = hostReg.exec(rule.to)[0] + if (host) { + favicon.src = 'chrome://favicon/' + host + break + } } - } - if (navigator.userAgent.match("Chrome")) { - var xhr = new XMLHttpRequest(); - try { - xhr.open("GET", favicon.src, true); - label.appendChild(favicon); - } catch (e) {} + label.appendChild(favicon) } // label text - var text = document.createElement("span"); - text.innerText = ruleset.name; + const text = document.createElement('span') + text.innerText = ruleset.name + if (ruleset.note.length) { - text.title = ruleset.note; + text.title = ruleset.note } - if(ruleset.note == "user rule") { - var remove = document.createElement("img"); - remove.src = chrome.extension.getURL("remove.png"); - remove.className = "remove"; - line.appendChild(remove); + if (ruleset.note === 'user rule') { + const remove = document.createElement('img') + remove.src = chrome.extension.getURL('remove.png') + remove.className = 'remove' + line.appendChild(remove) - remove.addEventListener("click", function(){ - sendMessage("remove_rule", ruleset); - list_div.removeChild(line); - }); + remove.addEventListener('click', function () { + sendMessage('remove_rule', ruleset) + listDiv.removeChild(line) + }) } - label.appendChild(text); - - line.appendChild(label); - - list_div.appendChild(line); + label.appendChild(text) + line.appendChild(label) + listDiv.appendChild(line) } -// Change the UI to reflect extension enabled/disabled -function updateEnabledDisabledUI() { - sendMessage("get_is_extension_enabled", null, function(enabled){ - document.getElementById('onoffswitch').checked = enabled; - // Hide or show the rules sections - if (enabled) { - document.body.className = "" - } else { - document.body.className = "disabled" - } - sendMessage("update_state"); - }); +// Set extension enabled/disabled status +function setEnabled (enabled) { + sendMessage('set_is_extension_enabled', enabled).then(() => { + // The extension state changed, so reload this tab. + chrome.tabs.reload() + window.close() + }) } -// Toggle extension enabled/disabled status -function toggleEnabledDisabled() { - var extension_toggle_effect = function(){ - updateEnabledDisabledUI(); +// Set HTTP nowhere mode +function setHttpNowhere (enabled) { + sendMessage('set_option', { httpNowhere: enabled }).then(() => { // The extension state changed, so reload this tab. - chrome.tabs.reload(); - window.close(); - } - - sendMessage("get_is_extension_enabled", null, function(enabled){ - if (enabled) { - // User wants to disable us - sendMessage("set_is_extension_enabled", false, extension_toggle_effect); - } else { - // User wants to enable us - sendMessage("set_is_extension_enabled", true, extension_toggle_effect); - } - }); + chrome.tabs.reload() + window.close() + }) } /** - * Create the list of rules for a specific tab - * @param tabArray + * Fill in content into the popup on load */ -function gotTab(tabArray) { - var activeTab = tabArray[0]; - - sendMessage("get_active_rulesets", activeTab.id, function(rulesets){ - for (var r in rulesets) { - var listDiv = stableRules; - if (!rulesets[r].default_state) { - listDiv = unstableRules; +document.addEventListener('DOMContentLoaded', () => { + const stableRules = document.getElementById('StableRules') + const unstableRules = document.getElementById('UnstableRules') + chrome.tabs.query({ active: true, lastFocusedWindow: true }, tabs => { + const activeTab = tabs[0] + + sendMessage('get_active_rulesets', activeTab.id).then(rulesets => { + for (const name in rulesets) { + const listDiv = rulesets[name].default_state ? stableRules : unstableRules + + appendRuleLineToListDiv(rulesets[name], activeTab.id, listDiv) + + // Un-hide the div + listDiv.className = '' } - appendRuleLineToListDiv(rulesets[r], listDiv); - listDiv.style.position = "static"; - listDiv.style.visibility = "visible"; - } - // Only show the "Add a rule" link if we're on an HTTPS page - if (/^https:/.test(activeTab.url)) { - show(e("add-rule-link")); - } - }); -} -/** - * Fill in content into the popup on load - */ -document.addEventListener("DOMContentLoaded", function () { - stableRules = document.getElementById("StableRules"); - unstableRules = document.getElementById("UnstableRules"); - chrome.tabs.query({ active: true, currentWindow: true }, gotTab); + // Only show the "Add a rule" link if we're on an HTTPS page + const isHttps = activeTab.url.slice(0, 6) === 'https:' + + document.getElementById('add-rule-link').style.display = isHttps ? 'block' : 'none' + }) + }) // Set up the enabled/disabled switch & hide/show rules - updateEnabledDisabledUI(); - document.getElementById('onoffswitch').addEventListener('click', toggleEnabledDisabled); + sendMessage('get_is_extension_enabled').then(enabled => { + const extensionEnabled = document.getElementById('onoffswitch') + extensionEnabled.checked = enabled + extensionEnabled.addEventListener('change', event => { + setEnabled(event.target.checked) + }) + + // Hide or show the rules sections + document.body.className = enabled ? '' : 'disabled' + }) // Print the extension's current version. - var the_manifest = chrome.runtime.getManifest(); - var version_info = document.getElementById('current-version'); - version_info.innerText = the_manifest.version; + const versionInfo = document.getElementById('current-version') + versionInfo.innerText = chrome.runtime.getManifest().version // Set up toggle checkbox for HTTP nowhere mode - getOption_('httpNowhere', false, function(item) { - var httpNowhereCheckbox = document.getElementById('http-nowhere-checkbox'); - httpNowhereCheckbox.addEventListener('click', toggleHttpNowhere, false); - var httpNowhereEnabled = item.httpNowhere; - if (httpNowhereEnabled) { - httpNowhereCheckbox.setAttribute('checked', ''); - } - }); -}); + sendMessage('get_option', { httpNowhere: false }).then(item => { + const httpNowhereCheckbox = document.getElementById('http-nowhere-checkbox') + httpNowhereCheckbox.checked = item.httpNowhere + httpNowhereCheckbox.addEventListener('change', event => { + setHttpNowhere(event.target.checked) + }) + }) - -var escapeForRegex = function( value ) { - return value.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&"); -}; - -function hide(elem) { - elem.style.display = "none"; -} - -function show(elem) { - elem.style.display = "block"; -} + document.getElementById('add-rule-link').addEventListener('click', addManualRule) +}) /** * Handles the manual addition of rules */ -function addManualRule() { - chrome.tabs.query({ active: true, currentWindow: true }, function(tab) { - hide(e("add-rule-link")); - show(e("add-new-rule-div")); - var newUrl = document.createElement('a'); - newUrl.href = tab[0].url; - newUrl.protocol = "https:"; - e("new-rule-host").value = newUrl.host; - var oldUrl = document.createElement('a'); - oldUrl.href = tab[0].url; - oldUrl.protocol = "http:"; - var oldMatcher = "^" + escapeForRegex(oldUrl.protocol + "//" + oldUrl.host+ "/"); - e("new-rule-regex").value = oldMatcher; - var redirectPath = newUrl.protocol + "//" + newUrl.host + "/"; - e("new-rule-redirect").value = redirectPath; - e("new-rule-name").value = "Manual rule for " + oldUrl.host; - e("add-new-rule-button").addEventListener("click", function() { - var params = { - host : e("new-rule-host").value, - redirectTo : e("new-rule-redirect").value, - urlMatcher : e("new-rule-regex").value - }; - sendMessage("add_new_rule", params, function() { - location.reload(); - }); - }); - - e("cancel-new-rule").addEventListener("click", function() { - show(e("add-rule-link")); - hide(e("add-new-rule-div")); - }); - e("new-rule-show-advanced-link").addEventListener("click", function() { - show(e("new-rule-advanced")); - hide(e("new-rule-regular-text")); - }); - e("new-rule-hide-advanced-link").addEventListener("click", function() { - hide(e("new-rule-advanced")); - show(e("new-rule-regular-text")); - }); - }); -} - -function toggleHttpNowhere() { - getOption_('httpNowhere', false, function(item) { - setOption_('httpNowhere', !item.httpNowhere); - }); -} - -function getOption_(opt, defaultOpt, callback) { - var details = {}; - details[opt] = defaultOpt; - sendMessage("get_option", details, callback); -} +function addManualRule () { + chrome.tabs.query({ active: true, lastFocusedWindow: true }, tabs => { + const url = new URL(tabs[0].url) + document.getElementById('add-new-rule-div').style.display = 'block' + document.getElementById('add-rule-link').style.display = 'none' + document.getElementById('new-rule-host').value = url.host + document.getElementById('new-rule-regex').value = '^http:' + document.getElementById('new-rule-redirect').value = 'https:' + document.getElementById('new-rule-name').value = 'Manual rule for ' + url.host + document.getElementById('add-new-rule-button').addEventListener('click', () => { + const params = { + host: document.getElementById('new-rule-host').value, + redirectTo: document.getElementById('new-rule-redirect').value, + urlMatcher: document.getElementById('new-rule-regex').value + } -function setOption_(opt, value) { - var details = {}; - details[opt] = value; - sendMessage("set_option", details); + sendMessage('add_new_rule', params).then(() => { + location.reload() + }) + }) + + document.getElementById('cancel-new-rule').addEventListener('click', () => { + document.getElementById('add-rule-link').style.display = 'block' + document.getElementById('add-new-rule-div').style.display = 'none' + }) + document.getElementById('new-rule-show-advanced-link').addEventListener('click', () => { + document.getElementById('new-rule-advanced').style.display = 'block' + document.getElementById('new-rule-regular-text').style.display = 'none' + }) + document.getElementById('new-rule-hide-advanced-link').addEventListener('click', () => { + document.getElementById('new-rule-advanced').style.display = 'none' + document.getElementById('new-rule-regular-text').style.display = 'block' + }) + }) } diff --git a/chromium/send-message.js b/chromium/send-message.js index 2ed895523d79..859afefed337 100644 --- a/chromium/send-message.js +++ b/chromium/send-message.js @@ -1,8 +1,7 @@ -function sendMessage(type, object, callback) { - var packet = {}; - packet.type = type; - if(object){ - packet.object = object; - } - chrome.runtime.sendMessage(packet, callback); +'use strict' + +function sendMessage (type, object) { + return new Promise(resolve => { + chrome.runtime.sendMessage({ type, object }, resolve) + }) } diff --git a/chromium/translation.js b/chromium/translation.js index 61ae5ff14808..c0f955684b4f 100644 --- a/chromium/translation.js +++ b/chromium/translation.js @@ -1,11 +1,8 @@ -document.addEventListener("DOMContentLoaded", function () { - // auto-translate all elements with i18n attributes - var elem = document.querySelectorAll("[i18n]"); - for (let el of elem) { - el.innerHTML = chrome.i18n.getMessage(el.getAttribute("i18n")); - } +'use strict' - // other translations - e("aboutTitle").setAttribute("title", chrome.i18n.getMessage("about_title")); - e("add-rule-link").addEventListener("click", addManualRule); -}); +document.addEventListener('DOMContentLoaded', () => { + // auto-translate all elements with data-i18n attributes + for (const el of document.querySelectorAll('[data-i18n]')) { + el.innerText = chrome.i18n.getMessage(el.getAttribute('data-i18n')) + } +})