Skip to content
This repository has been archived by the owner on Dec 23, 2018. It is now read-only.

Switch to WebExtensions for good #61

Merged
merged 5 commits into from
Oct 6, 2017
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
16 changes: 15 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
smart-referer*.xpi
# Temporary files used by some editors
*.swp
*~

# Project storage of some editors
/.idea
/.project
/.settings
/.vscode

# Stuff that never was meant to be public
/+junk

# `web-ext` tool build artifacts
/web-ext-artifacts
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "webextension/deps/public-suffix-list"]
path = webextension/deps/public-suffix-list
path = deps/public-suffix-list
url = https://github.com/wrangr/psl.git
27 changes: 15 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
# Once we migrate to WebExtensions for good, this can probably be replaced by the `web-ext` tool…
# Eventually this will be completely replaced by the `web-ext` tool after
# issue https://github.com/mozilla/web-ext/issues/940 has been implemented…


# Name of target file is based on version number
DISTFILE := smart-referer.xpi
DISTFILE := web-ext-artifacts/smart-referer.xpi

# Assemble list of files that will be included in the final archive
# ~ Only consider currently tracked GIT files
FILELIST := $(shell git ls-files)
# ~ Skip ALL hidden files
FILELIST := $(filter-out .%, ${FILELIST})
FILELIST := $(filter-out .%, ${FILELIST})
# ~ Skip screenshots
FILELIST := $(filter-out screenshots/%, ${FILELIST})
# ~ Skip build system files
FILELIST := $(filter-out Makefile, ${FILELIST})
FILELIST := $(filter-out Makefile, ${FILELIST})
# ~ Only include the pre-assembled, non-minimized distribution file of the PSL
FILELIST := $(filter-out webextension/deps/public-suffix-list%, ${FILELIST}) webextension/deps/public-suffix-list/dist/psl.js
FILELIST := $(filter-out deps/public-suffix-list%, ${FILELIST}) deps/public-suffix-list/dist/psl.js



Expand All @@ -26,6 +29,7 @@ xpi: ${DISTFILE}

${DISTFILE}: ${FILELIST}
rm "${DISTFILE}" 2>/dev/null ||:
mkdir -p "$(dir ${DISTFILE})"
zip -9 "${DISTFILE}" ${FILELIST}

# Target for making a new GIT release (and building an XPI for it)
Expand All @@ -40,7 +44,7 @@ release: ${DISTFILE}
############################
# Determine target version #
############################
version="$$(grep '<em:version>.*</em:version>' "install.rdf" | cut -d'>' -f2 | cut -d'<' -f 1)"
version="$$(grep --only-matching '"version":\s*".*"' "manifest.json" | cut -d'"' -f4)"
if [ -z "$${version}" ];
then
echo 'Could not detect the current version number' >&2
Expand All @@ -52,7 +56,7 @@ release: ${DISTFILE}
######################################
if git show-ref "v$${version}" >/dev/null;
then
echo 'Release $${version} already exists, please increment the version number in the "install.rdf" file' >&2
echo 'Release $${version} already exists, please increment the version number in the "manifest.json" file' >&2
exit 2
fi

Expand All @@ -68,15 +72,14 @@ release: ${DISTFILE}
#################################
# Tag the newly created version #
#################################
git tag "v$${version}"
git tag --sign -m "Version $${version}" "v$${version}"

#########################
# Archive the final XPI #
#########################
cp "${DISTFILE}" "smart-referer-$${version}.xpi"
cp "${DISTFILE}" "$(dir ${DISTFILE})$(notdir $(basename ${DISTFILE}))-$${version}$(suffix ${DISTFILE})"


# Technically JPM is only for Jetpack-based add-ons, but it has enough compatibility hacks to work
# with out bootstrapped extension just fine
.PHONY: run
run:
jpm --binary $$(which firefox) ${JPMARGS} run
web-ext run ${WEXTARGS}
File renamed without changes
File renamed without changes
4 changes: 2 additions & 2 deletions webextension/icon.js → background/icon.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ browser.storage.onChanged.addListener((changes, areaName) => {
if(changes[name].newValue === true) {
//COMPAT: Firefox for Android 56+
if(typeof(browser.browserAction.setIcon) !== "undefined") {
browser.browserAction.setIcon({ path: { 256: "icon.svg" } });
browser.browserAction.setIcon({ path: { 256: "assets/icon.svg" } });
}

browser.browserAction.setTitle({ title: generateIconTitle(true) });
} else {
//COMPAT: Firefox for Android 56+
if(typeof(browser.browserAction.setIcon) !== "undefined") {
browser.browserAction.setIcon({ path: { 256: "icon-light.svg" } });
browser.browserAction.setIcon({ path: { 256: "assets/icon-light.svg" } });
}

browser.browserAction.setTitle({ title: generateIconTitle(false) });
Expand Down
92 changes: 27 additions & 65 deletions webextension/main.js → background/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,66 +47,36 @@ Promise.resolve().then(() => {
}).then((result) => {
// Update the default options with the real ones loaded from storage
Object.assign(options, result);

//WEXT-MIGRATION: Query options from legacy add-on shim
if(!options.migrated) {
console.info("Attempting preference migration from legacy add-on…");
return browser.runtime.sendMessage("read-legacy-prefs").then((result) => {
console.info("Received preferences from legacy add-on:");
console.info(result);

Object.assign(options, result);
}).catch((error) => {
if(error.message.toLowerCase().includes("could not establish connection")) {
console.info("Not running as part of legacy add-on, skipping migration!");

options.migrated = true;
} else {
return Promise.reject(error);
}
});
}
}).then(() => {

// Write back the final option list so that the defaults are properly displayed on the
// options page as well
return browser.storage.local.set(options);
}).then(() => {
//WEXT-MIGRATION: Remove options in legacy add-on shim after the new values have been saved
if(!options.migrated) {
console.info("Purging preferences from legacy add-on…");
return browser.runtime.sendMessage("purge-legacy-prefs").then(() => {
options.migrated = true;

return browser.storage.local.set(options);
}).then(() => {
console.info("Preference migration successfully completed!");
});
} else {
return Promise.resolve();
}
}).then(() => {
// Do initial policy fetch (will cause timer for more updates to be set)
refreshPolicy();

// Keep track of new developments in option land
browser.storage.onChanged.addListener((changes, areaName) => {
if(areaName !== "local") {
return;
}

// Apply change
// Copy changes to local options state
for(let name of Object.keys(changes)) {
options[name] = changes[name].newValue;
}
});

// Done setting up options
}).then(() => {
// Do initial policy fetch (will cause timer for more updates to be set)
updatePolicy();

// Also update policy when its settings are changed
browser.storage.onChanged.addListener((changes, areaName) => {

// Apply changes to option keys
for(let name of Object.keys(changes)) {
if(areaName === "local" && (name === "allow" || name === "whitelist")) {
updatePolicy();
switch(name) {
case "allow":
case "whitelist":
refreshPolicy();
break;

case "enable":
applyRefererConfiguration();
break;
}
}
});
Expand All @@ -117,7 +87,7 @@ Promise.resolve().then(() => {
* Download an updated version of the online whitelist
*/
let policyUpdateHandle = 0;
function updatePolicy() {
function refreshPolicy() {
if(!options.whitelist) {
policy = new Policy(options.allow);
return;
Expand All @@ -136,7 +106,7 @@ function updatePolicy() {
policy = new Policy(`${options.allow} ${responseText}`);
}).finally(() => {
// Schedule another policy update
policyUpdateHandle = setTimeout(updatePolicy, 86400000);
policyUpdateHandle = setTimeout(refreshPolicy, 86400000);
}).catch(console.exception);
}

Expand Down Expand Up @@ -180,33 +150,25 @@ function requestListener(request) {
/*****************
* Orchestration *
*****************/
/**

/**
* Start or stop the HTTP header and JavaScript modifications
*/
let processingEnabled = false;
function setProcessingStatus(enable) {
if(!processingEnabled && enable) {
function applyRefererConfiguration() {
if(!processingEnabled && options["enable"]) {
processingEnabled = true;
browser.webRequest.onBeforeSendHeaders.addListener(
requestListener,
{urls: ["<all_urls>"]},
["blocking", "requestHeaders"]
);
} else if(processingEnabled && !enable) {
} else if(processingEnabled && !options["enable"]) {
browser.webRequest.onBeforeSendHeaders.removeListener(requestListener);
processingEnabled = false;
}
}

// Monitor options for changes to the request processing setting
browser.storage.onChanged.addListener((changes, areaName) => {
for(let name of Object.keys(changes)) {
if(areaName === "local" && name === "enable") {
setProcessingStatus(changes[name].newValue);
}
}
});

// Enable request processing by default
setProcessingStatus(true);
// Enable request processing based on the default configuration until the
// actual configuration has been retrieved from disk
applyRefererConfiguration();
File renamed without changes.
104 changes: 0 additions & 104 deletions bootstrap.js

This file was deleted.

6 changes: 3 additions & 3 deletions webextension/options.html → content/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

<script type="text/javascript" src="options.js"></script>

<script type="text/javascript" src="deps/l10n/l10n.js"></script>
<script type="text/javascript" src="deps/wext-options/options.js"></script>
<link rel="stylesheet" type="text/css" href="deps/wext-options/options.css" />
<script type="text/javascript" src="../deps/l10n/l10n.js"></script>
<script type="text/javascript" src="../deps/wext-options/options.js"></script>
<link rel="stylesheet" type="text/css" href="../deps/wext-options/options.css" />
<style type="text/css">
/**
* Resize form elements to the width of their container
Expand Down
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions deps/public-suffix-list
Submodule public-suffix-list added at 26efa0
Loading