Skip to content
This repository was archived by the owner on Nov 6, 2023. It is now read-only.
Closed
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
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "addon-sdk"]
path = addon-sdk
url = https://github.com/mozilla/addon-sdk.git
[submodule "translations"]
path = translations
url = https://git.torproject.org/translation.git
Expand Down
1 change: 0 additions & 1 deletion addon-sdk
Submodule addon-sdk deleted from 12f7d5
3 changes: 3 additions & 0 deletions chromium/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"parserOptions": {
"ecmaVersion": 2017
},
"env": {
"browser": true,
"commonjs": true,
Expand Down
80 changes: 72 additions & 8 deletions chromium/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -181,6 +189,7 @@ var removeRule = function(ruleset) {
* */
function AppliedRulesets() {
this.active_tab_rules = {};
this.active_tab_hostnames = {};

var that = this;
if (chrome.tabs) {
Expand All @@ -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) {
Expand All @@ -207,8 +212,21 @@ AppliedRulesets.prototype = {
return null;
},

addHostnameToTab: function(tabId, ruleset_name, hostname) {

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.

Looks like the "addHostnameToTab" work snuck back in by accident.

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.

@jsha this is just because this was forked from the first iteration of firefox-webextensions-core - should I rebase it?

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.

I could rebase from master, now that firefox-webextensions-core is merged.

Copy link
Copy Markdown

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.

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];
}
};

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand All @@ -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") {

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 add a comment explaining when this is used? I.e., importing from the Options page rather than automatically from the parent extension?

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.

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 => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do we really need to wait until the settings are stored?

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.

@koops76 we do for the options ui. We only want to display the Settings imported string once the settings have actually been imported.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Then use Promise.all to await all four promises.

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);
});
}
}
5 changes: 4 additions & 1 deletion chromium/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"incognito-cache-clearing.js"
]
},
"options_ui": {
"page": "options.html"
},
"browser_action": {
"default_icon": {
"38": "icons/icon-active-38.png"
Expand All @@ -32,7 +35,7 @@
},
"incognito": "spanning",
"manifest_version": 2,
"minimum_chrome_version": "52",
"minimum_chrome_version": "55",
"name": "__MSG_about_ext_name__",
"permissions": [
"webNavigation",
Expand Down
17 changes: 17 additions & 0 deletions chromium/options.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.section-header{

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Add a space between the { and the selector.

margin-bottom: 10px;
}

.section-header-span{
border-bottom: 1px solid #ccc;
font-size: 15px;
}

#import{
margin-bottom: 10px;
float: right;
}

#import-confirmed{
display: none;
}
22 changes: 22 additions & 0 deletions chromium/options.html
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>

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Move the scripts to the end of body tag.

<script src="translation.js"></script>
<script src="send-message.js"></script>
</head>
<body>
<div id='import-confirmed' i18n="options_imported"></div>

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Use data-i18n for valid HTML5.


<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>
28 changes: 28 additions & 0 deletions chromium/options.js
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");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

const?


function import_json(e) {
e.preventDefault();

let settings = JSON.parse(json_data);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

const?

sendMessage("import_settings", settings, resp => {
document.querySelector("#import-confirmed").style.display = "block";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

document.getElementById?

document.querySelector("form").style.display = "none";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Maybe find a form by id instead of simply taking the first one in the document?

});
}

document.querySelector("#import-settings").addEventListener("change", event => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

document.getElementById?

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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Maybe find a form by id instead of simply taking the first one in the document?

});
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
2 changes: 2 additions & 0 deletions chromium/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

<link href="popup.css" rel="stylesheet" type="text/css"/>
<script src="popup.js"></script>
<script src="translation.js"></script>

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Move the scripts to the end of body tag.

<script src="send-message.js"></script>

</head>

Expand Down
31 changes: 9 additions & 22 deletions chromium/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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";
}
Expand Down Expand Up @@ -191,16 +197,6 @@ document.addEventListener("DOMContentLoaded", function () {
httpNowhereCheckbox.setAttribute('checked', '');

@ghost ghost Aug 10, 2017 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

httpNowhereCheckbox.checked = true?

}
});

// auto-translate all elements with i18n attributes
var elem = document.querySelectorAll("[i18n]");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Use document.querySelectorAll("[data-i18n]") instead.

for (let el of elem) {
el.innerHTML = chrome.i18n.getMessage(el.getAttribute("i18n"));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Use el.getAttribute("data-i18n") or el.dataset.i18n.

}

// other translations
e("aboutTitle").setAttribute("title", chrome.i18n.getMessage("about_title"));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

e("aboutTitle").title = chrome.i18n.getMessage("about_title")

e("add-rule-link").addEventListener("click", addManualRule);
});


Expand Down Expand Up @@ -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);
}
8 changes: 8 additions & 0 deletions chromium/send-message.js
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);
}
11 changes: 11 additions & 0 deletions chromium/translation.js
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]");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

const elements = document.querySelectorAll("[data-i18n]");

for (let el of elem) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

const?

el.innerHTML = chrome.i18n.getMessage(el.getAttribute("i18n"));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

el.innerText = chrome.i18n.getMessage(el.getAttribute("data-i18n"));

}

// other translations
e("aboutTitle").setAttribute("title", chrome.i18n.getMessage("about_title"));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

e("aboutTitle").title = chrome.i18n.getMessage("about_title")

e("add-rule-link").addEventListener("click", addManualRule);
});
19 changes: 17 additions & 2 deletions install-dev-dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ if type apt-get >/dev/null ; then
# In Debian, `python-` is assumed to be python 2.7, no need to specify - dkg
$SUDO_SHIM apt-get install libxml2-dev libxml2-utils libxslt1-dev \
python-dev $BROWSERS zip sqlite3 python-pip libcurl4-openssl-dev xvfb \
libssl-dev git $CHROMEDRIVER
libssl-dev git curl $CHROMEDRIVER
if ! type geckodriver >/dev/null; then
curl -LO "https://github.com/mozilla/geckodriver/releases/download/v0.16.1/geckodriver-v0.16.1-linux64.tar.gz"
tar -zxvf "geckodriver-v0.16.1-linux64.tar.gz"
rm -f "geckodriver-v0.16.1-linux64.tar.gz"
$SUDO_SHIM mv geckodriver /usr/bin/geckodriver
$SUDO_SHIM chown root /usr/bin/geckodriver
$SUDO_SHIM chmod 755 /usr/bin/geckodriver
fi
elif type brew >/dev/null ; then
brew list python &>/dev/null || brew install python
brew install libxml2 gnu-sed chromedriver
Expand All @@ -42,9 +50,16 @@ elif type dnf >/dev/null ; then
rm -f "chromedriver_linux$ARCH.zip"
$SUDO_SHIM mv chromedriver /usr/bin/chromedriver
$SUDO_SHIM chown root /usr/bin/chromedriver
$SUDO_SHIM chmod +x /usr/bin/chromedriver
$SUDO_SHIM chmod 755 /usr/bin/chromedriver
fi
if ! type geckodriver >/dev/null; then
curl -LO "https://github.com/mozilla/geckodriver/releases/download/v0.16.1/geckodriver-v0.16.1-macos.tar.gz"
tar -zxvf "geckodriver-v0.16.1-macos.tar.gz"
rm -f "geckodriver-v0.16.1-macos.tar.gz"
$SUDO_SHIM mv geckodriver /usr/bin/geckodriver
$SUDO_SHIM chown root /usr/bin/geckodriver
$SUDO_SHIM chmod 755 /usr/bin/geckodriver
fi
# This is needed for Firefox on some systems. See here for more information:
# https://github.com/EFForg/https-everywhere/pull/5584#issuecomment-238655443
if [ ! -f /var/lib/dbus/machine-id ]; then
Expand Down
Loading