Skip to content
This repository was archived by the owner on Nov 6, 2023. It is now read-only.
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
62 changes: 53 additions & 9 deletions chromium/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@ chrome.storage.onChanged.addListener(function(changes, areaName) {
}
}
});
chrome.tabs.onActivated.addListener(function() {
updateState();
});
chrome.windows.onFocusChanged.addListener(function() {
updateState();
});
if (chrome.tabs) {
chrome.tabs.onActivated.addListener(function() {
updateState();
});
}
if (chrome.windows) {
chrome.windows.onFocusChanged.addListener(function() {
updateState();
});
}
chrome.webNavigation.onCompleted.addListener(function() {
updateState();
});
Expand Down Expand Up @@ -106,6 +110,9 @@ loadStoredUserRules();
* disabled: extension is disabled from the popup menu.
*/
var updateState = function() {
if (!chrome.tabs) {
return;
}
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
if (!tabs || tabs.length === 0) {
return;
Expand Down Expand Up @@ -176,9 +183,11 @@ function AppliedRulesets() {
this.active_tab_rules = {};

var that = this;
chrome.tabs.onRemoved.addListener(function(tabId, info) {
that.removeTab(tabId);
});
if (chrome.tabs) {
chrome.tabs.onRemoved.addListener(function(tabId, info) {
that.removeTab(tabId);
});
}
}

AppliedRulesets.prototype = {
Expand Down Expand Up @@ -618,3 +627,38 @@ chrome.runtime.onConnect.addListener(function (port) {
});
}
});

// This is necessary for communication with the popup in Firefox Private
// Browsing Mode, see https://bugzilla.mozilla.org/show_bug.cgi?id=1329304
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you confirm that this will only receive messages from background.js, not from content scripts or other extensions?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right. There is a separate IPC API for inter-extension, chrome.runtime.onMessageExternal. See https://developer.chrome.com/extensions/messaging#external

if (message.type == "get_option") {
storage.get(message.object, function(item){
sendResponse(item);
});
return true;
} else if (message.type == "set_option") {
storage.set(message.object);
} else if (message.type == "get_is_extension_enabled") {
sendResponse(isExtensionEnabled);
} else if (message.type == "set_is_extension_enabled") {
isExtensionEnabled = message.object;
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 == "set_ruleset_active_status") {
var ruleset = activeRulesets.getRulesets(message.object.tab_id)[message.object.name];
ruleset.active = message.object.active;
sendResponse(true);
} else if (message.type == "add_new_rule") {
addNewRule(message.object, function() {
sendResponse(true);
});
return true;
} else if (message.type == "update_state") {
updateState();
} else if (message.type == "remove_rule") {
removeRule(message.object);
}
});
8 changes: 6 additions & 2 deletions chromium/incognito-cache-clearing.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ function detect_incognito_destruction(windowId) {


// Listen to window creation, so we can detect if an incognito window is created
chrome.windows.onCreated.addListener(detect_incognito_creation);
if (chrome.windows) {
chrome.windows.onCreated.addListener(detect_incognito_creation);
}

// Listen to window destruction, so we can clear caches if all incognito windows are destroyed
chrome.windows.onRemoved.addListener(detect_incognito_destruction);
if (chrome.windows) {
chrome.windows.onRemoved.addListener(detect_incognito_destruction);
}
10 changes: 7 additions & 3 deletions chromium/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"author": {
"email": "eff.software.projects@gmail.com"
},
"applications": {
"gecko": {
"id": "https-everywhere-eff@eff.org",
"update_url": "https://www.eff.org/files/https-everywhere-updates.json"
}
},
"author": "eff.software.projects@gmail.com",
"background": {
"scripts": [
"rules.js",
Expand Down
1 change: 1 addition & 0 deletions chromium/popup.css
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ section.rules {
/* Override display attribute of text labels in widgets.css. */
.rule span {
display: inline !important;
margin-left: 0.6em;
}

body {
Expand Down
137 changes: 82 additions & 55 deletions chromium/popup.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"use strict";

var backgroundPage = chrome.extension.getBackgroundPage();
var stableRules = null;
var unstableRules = null;
var hostReg = /.*\/\/[^$/]*\//;
var storage = backgroundPage.storage;

function e(id) {
return document.getElementById(id);
Expand All @@ -15,18 +13,27 @@ function e(id) {
* @param checkbox checkbox being clicked
* @param ruleset the ruleset tied tot he checkbox
*/
function toggleRuleLine(checkbox, ruleset) {
ruleset.active = checkbox.checked;

if (ruleset.active != ruleset.default_state) {
localStorage[ruleset.name] = ruleset.active;
} else {
delete localStorage[ruleset.name];
// purge the name from the cache so that this unchecking is persistent.
backgroundPage.all_rules.ruleCache.delete(ruleset.name);
}
// Now reload the selected tab of the current window.
chrome.tabs.reload();
function toggleRuleLine(checkbox, ruleset, tab_id) {
var ruleset_active = checkbox.checked;
var set_ruleset = {
active: ruleset_active,
name: ruleset.name,
tab_id: tab_id
};

sendMessage("set_ruleset_active_status", set_ruleset, function(){

if (ruleset_active != ruleset.default_state) {
localStorage[ruleset.name] = ruleset_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);
}

// Now reload the selected tab of the current window.
chrome.tabs.reload();
});
}

/**
Expand All @@ -50,7 +57,7 @@ function appendRuleLineToListDiv(ruleset, list_div) {
checkbox.setAttribute("checked", "");
}
checkbox.onchange = function(ev) {
toggleRuleLine(checkbox, ruleset);
toggleRuleLine(checkbox, ruleset, tab_id);
};
label.appendChild(checkbox);

Expand All @@ -65,11 +72,14 @@ function appendRuleLineToListDiv(ruleset, list_div) {
break;
}
}
var xhr = new XMLHttpRequest();
try {
xhr.open("GET", favicon.src, true);
label.appendChild(favicon);
} catch (e) {}

if (navigator.userAgent.match("Chrome")) {
var xhr = new XMLHttpRequest();
try {
xhr.open("GET", favicon.src, true);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this XMLHTTPRequest stuff. I'm not sure what it was supposed to do at all.

label.appendChild(favicon);
} catch (e) {}
}

// label text
var text = document.createElement("span");
Expand All @@ -85,7 +95,7 @@ function appendRuleLineToListDiv(ruleset, list_div) {
line.appendChild(remove);

remove.addEventListener("click", function(){
backgroundPage.removeRule(ruleset);
sendMessage("remove_rule", ruleset);
list_div.removeChild(line);
});
}
Expand All @@ -99,29 +109,36 @@ function appendRuleLineToListDiv(ruleset, list_div) {

// Change the UI to reflect extension enabled/disabled
function updateEnabledDisabledUI() {
document.getElementById('onoffswitch').checked = backgroundPage.isExtensionEnabled;
// Hide or show the rules sections
if (backgroundPage.isExtensionEnabled) {
document.body.className = ""
} else {
document.body.className = "disabled"
}
backgroundPage.updateState();
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");
});
}

// Toggle extension enabled/disabled status
function toggleEnabledDisabled() {
if (backgroundPage.isExtensionEnabled) {
// User wants to disable us
backgroundPage.isExtensionEnabled = false;
} else {
// User wants to enable us
backgroundPage.isExtensionEnabled = true;
var extension_toggle_effect = function(){
updateEnabledDisabledUI();
// The extension state changed, so reload this tab.
chrome.tabs.reload();
window.close();
}
updateEnabledDisabledUI();
// 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);
}
});
}

/**
Expand All @@ -130,21 +147,22 @@ function toggleEnabledDisabled() {
*/
function gotTab(tabArray) {
var activeTab = tabArray[0];
var rulesets = backgroundPage.activeRulesets.getRulesets(activeTab.id);

for (var r in rulesets) {
var listDiv = stableRules;
if (!rulesets[r].default_state) {
listDiv = unstableRules;
sendMessage("get_active_rulesets", activeTab.id, function(rulesets){
for (var r in rulesets) {
var listDiv = stableRules;
if (!rulesets[r].default_state) {
listDiv = unstableRules;
}
appendRuleLineToListDiv(rulesets[r], listDiv);
listDiv.style.position = "static";
listDiv.style.visibility = "visible";
}
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"));
}
// Only show the "Add a rule" link if we're on an HTTPS page
if (/^https:/.test(activeTab.url)) {
show(e("add-rule-link"));
}
});
}

/**
Expand Down Expand Up @@ -223,7 +241,7 @@ function addManualRule() {
redirectTo : e("new-rule-redirect").value,
urlMatcher : e("new-rule-regex").value
};
backgroundPage.addNewRule(params, function() {
sendMessage("add_new_rule", params, function() {
location.reload();
});
});
Expand Down Expand Up @@ -252,11 +270,20 @@ function toggleHttpNowhere() {
function getOption_(opt, defaultOpt, callback) {
var details = {};
details[opt] = defaultOpt;
return storage.get(details, callback);
sendMessage("get_option", details, callback);
}

function setOption_(opt, value) {
var details = {};
details[opt] = value;
return storage.set(details);
sendMessage("set_option", details);
}

function sendMessage(type, object, callback) {
var packet = {};
packet.type = type;
if(object){
packet.object = object;
}
chrome.runtime.sendMessage(packet, callback);
}
6 changes: 5 additions & 1 deletion chromium/storage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
var storage = chrome.storage.local;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This entire file can be deleted and replaced with simple line in each file storage is used.

const storage = chrome.storage.sync || chrome.storage.local

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Within for example the tor browser, chrome.storage.sync may exist but throw an error. That's the reason for the lastError check.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File a bug for Tor Browser then.

if (chrome.storage.sync) {
storage = chrome.storage.sync;
chrome.storage.sync.set({"sync-set-test": true}, () => {
if(!chrome.runtime.lastError){
storage = chrome.storage.sync;
}
});
}
if (typeof exports != 'undefined') {
exports = storage;
Expand Down