1 Commits

Author SHA1 Message Date
Kevin R
b55b6af007 Migration to MV3 2023-11-11 17:49:00 +01:00
16 changed files with 98 additions and 206 deletions

View File

@@ -28,6 +28,8 @@ bundle addon:
stage: build
script:
- zip ClearURLs -r -FS clearurls.js browser-polyfill.js manifest.json img/* external_js/* html/* core_js/* css/* fonts/* _locales/*
only:
- master
artifacts:
paths:
- ClearURLs.zip
@@ -53,8 +55,6 @@ pages:
- sha256sum public/data/data.min.json | awk '{print $1}' > public/data/rules.min.hash
- node build_tools/minifyDataJSON.js "public/data/data.min.json" "public/data/data.minify.json"
- sha256sum public/data/data.minify.json | awk '{print $1}' > public/data/rules.minify.hash
only:
- master
artifacts:
paths:
- public

View File

@@ -87,14 +87,6 @@
"message": "Show numbers of cleaned urls",
"description": "This string is used as title for the badges switch button on the popup page."
},
"popup_html_configs_whitelist_button_add": {
"message": "Whitelist Site",
"description": "This string is used as name for the whitelist button on the popup page."
},
"popup_html_configs_whitelist_button_remove": {
"message": "Remove from Whitelist",
"description": "This string is used as name for the whitelist button on the popup page."
},
"popup_html_statistics_head": {
"message": "Statistics",
"description": "This string is used as title for the statistics on the popup page."
@@ -187,10 +179,6 @@
"message": "The url to the rules.hash file (hash)",
"description": "This string is used as name for the rule.hash url label."
},
"setting_whitelist_list_label": {
"message": "Whitelisted sites",
"description": "This string is used as name for the whitelisted sites list label."
},
"setting_types_label": {
"message": "<a href='https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/ResourceType' target='_blank'>Request types</a> (expert level)",
"description": "This string is used as name for the types label."

View File

@@ -47,19 +47,6 @@ function removeFieldsFormURL(provider, pureUrl, quiet = false, request = null) {
let rawRules = provider.getRawRules();
let urlObject = new URL(url);
/*
* Skip whitelisted sites
*/
for (const site of storage.whitelist) {
if (url.indexOf(site) !== -1) {
return {
"changes": false,
"url": url,
"cancel": false
}
}
}
if (storage.localHostsSkipping && checkLocalURL(urlObject)) {
return {
"changes": false,
@@ -740,3 +727,9 @@ function start() {
["blocking"]
);
}
// TODO: Must be asked after install
browser.permissions.request({
permissions: ["webRequest", "webRequestBlocking", "tabs", "scripting", "webNavigation"],
origins: ["<all_urls>"],
});

View File

@@ -48,9 +48,9 @@ function increaseBadged(quiet = false, request) {
checkOSAndroid().then((res) => {
if (!res) {
if (storage.badgedStatus && !quiet) {
browser.browserAction.setBadgeText({text: (badges[tabId]).counter.toString(), tabId: tabId}).catch(handleError);
browser.action.setBadgeText({text: (badges[tabId]).counter.toString(), tabId: tabId}).catch(handleError);
} else {
browser.browserAction.setBadgeText({text: "", tabId: tabId}).catch(handleError);
browser.action.setBadgeText({text: "", tabId: tabId}).catch(handleError);
}
}
});

View File

@@ -22,36 +22,29 @@
* and based on: https://github.com/mdn/webextensions-examples/tree/master/context-menu-copy-link-with-types
*/
function contextMenuStart() {
if(storage.contextMenuEnabled) {
browser.contextMenus.create({
id: "copy-link-to-clipboard",
title: translate("clipboard_copy_link"),
contexts: ["link"]
});
browser.runtime.onInstalled.addListener(contextMenuStart);
browser.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "copy-link-to-clipboard") {
const url = pureCleaning(info.linkUrl);
const code = "copyToClipboard(" +
JSON.stringify(url)+");";
function contextMenuStart(details) {
browser.contextMenus.create({
id: "copy-link-to-clipboard",
title: translate("clipboard_copy_link"),
contexts: ["link"]
});
browser.tabs.executeScript({
code: "typeof copyToClipboard === 'function';",
}).then((results) => {
if (!results || results[0] !== true) {
return browser.tabs.executeScript(tab.id, {
file: "/external_js/clipboard-helper.js",
}).catch(handleError);
}
}).then(() => {
return browser.tabs.executeScript(tab.id, {
code,
});
}).catch((error) => {
console.error("Failed to copy text: " + error);
});
}
});
}
browser.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "copy-link-to-clipboard") {
const url = pureCleaning(info.linkUrl);
browser.scripting.executeScript({
target: {
tabId: tab.id,
allFrames: true
},
func: (text) => navigator.clipboard.writeText(text),
args: [
url
]
});
}
});
}

View File

@@ -27,9 +27,6 @@
function injectFunction() {
let ele = document.createElement('script');
let s = document.getElementsByTagName('script')[0];
if (s === undefined) {
return;
}
ele.type = 'text/javascript';
ele.textContent = "Object.defineProperty(window, 'rwt', {" +

View File

@@ -17,35 +17,45 @@
*/
/*jshint esversion: 6 */
/*
* This script is responsible for listen on history changes.
* This technique is often used to inject tracking code into the location bar,
* because all feature events will use the updated URL.
*/
browser.webNavigation.onHistoryStateUpdated.addListener(historyCleaner);
function historyListenerStart() {
if(storage.historyListenerEnabled) {
browser.webNavigation.onHistoryStateUpdated.addListener(historyCleaner);
}
}
/**
* Function that is triggered on history changes. Injects script into page
* to clean links that were pushed to the history stack with the
* history.replaceState method.
* @param {state object} details The state object is a JavaScript object
* which is associated with the new history entry created by replaceState()
*/
* Function that is triggered on history changes. Injects script into page
* to clean links that were pushed to the history stack with the
* history.replaceState method.
* @param {state object} details The state object is a JavaScript object
* which is associated with the new history entry created by replaceState()
*/
function historyCleaner(details) {
if(storage.globalStatus) {
if (storage.globalStatus && storage.historyListenerEnabled) {
const urlBefore = details.url;
const urlAfter = pureCleaning(details.url);
if(urlBefore !== urlAfter) {
browser.tabs.executeScript(details.tabId, {
frameId: details.frameId,
code: 'history.replaceState(null,"",'+JSON.stringify(urlAfter)+');'
}).then(() => {}, onError);
if (urlBefore !== urlAfter) {
browser.scripting.executeScript({
target: {
tabId: details.tabId,
frameIds: [
details.frameId
]
},
func: (url) => history.replaceState({state: null}, "", url),
args: [
urlAfter
]
}).then(() => {
}, onError);
}
}
}

View File

@@ -62,34 +62,6 @@ function changeStatistics()
elTotal.textContent = totalCounter.toLocaleString();
}
/**
* Set the whitelist button text
*/
function setWhitelistText()
{
let element = document.getElementById('whitelist_btn');
let currentSite;
browser.tabs.query({active: true, currentWindow: true}, function(tabs) {
currentSite = tabs[0].url;
});
browser.runtime.sendMessage({
function: "getData",
params: ['whitelist']
}).then((data) => {
let siteFound = data.response.some(site => currentSite.indexOf(site) !== -1);
if (siteFound) {
element.classList.replace('btn-primary', 'btn-danger')
element.textContent = translate('popup_html_configs_whitelist_button_remove')
document.getElementById('whitelist_btn').onclick = () => {changeWhitelist(true)};
} else {
element.classList.replace('btn-danger', 'btn-primary')
element.textContent = translate('popup_html_configs_whitelist_button_add')
document.getElementById('whitelist_btn').onclick = () => {changeWhitelist(false)};
}
}).catch(handleError);
}
/**
* Set the value for the hashStatus on startUp.
*/
@@ -183,36 +155,6 @@ function setSwitchButton(id, varname)
element.checked = this[varname];
}
/**
* Adds (or removes) the site the user is on to the whitelist
* Whitelisted sites do not get filtered
* @param {boolean} removeWl If true remove current site instead of adding
*/
function changeWhitelist(removeWl) {
let site;
browser.tabs.query({active: true, currentWindow: true}, function(tabs) { // Couldn't figure out how to access currentUrl var
site = tabs[0].url; // So this is used instead
});
browser.runtime.sendMessage({
function: "getData",
params: ['whitelist']
}).then((data) => {
let siteUrl = new URL(site)
let domain = siteUrl.hostname
if (removeWl) {
data.response = data.response.filter(wlSite => wlSite !== domain)
} else {
data.response.push(domain)
}
browser.runtime.sendMessage({
function: "setData",
params: ['whitelist', data.response]
}).then(() => {
setWhitelistText();
}).catch(handleError);
}).catch(handleError);
}
/**
* Reset the global statistic
*/
@@ -278,7 +220,6 @@ function setText()
injectText('configs_switch_filter','popup_html_configs_switch_filter');
injectText('configs_head','popup_html_configs_head');
injectText('configs_switch_statistics','configs_switch_statistics');
setWhitelistText();
document.getElementById('donate').title = translate('donate_button');
}

View File

@@ -82,7 +82,6 @@ function save() {
saveData("badged_color", pickr.getColor().toHEXA().toString())
.then(() => saveData("ruleURL", document.querySelector('input[name=ruleURL]').value))
.then(() => saveData("hashURL", document.querySelector('input[name=hashURL]').value))
.then(() => saveData("whitelist", document.querySelector('input[name=whitelist]').value.split(',')))
.then(() => saveData("types", document.querySelector('input[name=types]').value))
.then(() => saveData("logLimit", Math.max(0, Math.min(5000, document.querySelector('input[name=logLimit]').value))))
.then(() => browser.runtime.sendMessage({
@@ -123,7 +122,6 @@ function getData() {
loadData("ruleURL")
.then(() => loadData("hashURL"))
.then(() => loadData("whitelist"))
.then(() => loadData("types"))
.then(() => loadData("logLimit"))
.then(logData => {
@@ -134,8 +132,7 @@ function getData() {
}
}).catch(handleError);
loadData("contextMenuEnabled")
.then(() => loadData("historyListenerEnabled"))
loadData("historyListenerEnabled")
.then(() => loadData("localHostsSkipping"))
.then(() => loadData("referralMarketing"))
.then(() => loadData("domainBlocking"))
@@ -144,7 +141,6 @@ function getData() {
.then(() => {
changeSwitchButton("localHostsSkipping", "localHostsSkipping");
changeSwitchButton("historyListenerEnabled", "historyListenerEnabled");
changeSwitchButton("contextMenuEnabled", "contextMenuEnabled");
changeSwitchButton("referralMarketing", "referralMarketing");
changeSwitchButton("domainBlocking", "domainBlocking");
changeSwitchButton("pingBlocking", "pingBlocking");
@@ -218,11 +214,9 @@ function setText() {
document.getElementById('reset_settings_btn').setAttribute('title', translate('setting_html_reset_button_title'));
document.getElementById('rule_url_label').textContent = translate('setting_rule_url_label');
document.getElementById('hash_url_label').textContent = translate('setting_hash_url_label');
document.getElementById('whitelist_list_label').textContent = translate('setting_whitelist_list_label');
document.getElementById('types_label').innerHTML = translate('setting_types_label');
document.getElementById('save_settings_btn').textContent = translate('settings_html_save_button');
document.getElementById('save_settings_btn').setAttribute('title', translate('settings_html_save_button_title'));
injectText("context_menu_enabled", "context_menu_enabled");
document.getElementById('history_listener_enabled').innerHTML = translate('history_listener_enabled');
injectText("local_hosts_skipping", "local_hosts_skipping");
document.getElementById('export_settings_btn_text').textContent = translate('setting_html_export_button');

View File

@@ -86,7 +86,7 @@ function saveOnDisk(keys) {
}
/**
* Schedule to save a key to disk in 30 seconds.
* Schedule to save a key to disk in 60 seconds.
* @param {String} key
*/
function deferSaveOnDisk(key) {
@@ -95,13 +95,20 @@ function deferSaveOnDisk(key) {
return;
}
setTimeout(function () {
browser.alarms.create("deferSaveOnDisk", {
delayInMinutes: 1
});
hasPendingSaves = true;
}
browser.alarms.onAlarm.addListener(function (alarmInfo) {
if (alarmInfo.name === "deferSaveOnDisk") {
saveOnDisk(Array.from(pendingSaves));
pendingSaves.clear();
hasPendingSaves = false;
}, 30000);
hasPendingSaves = true;
}
}
});
/**
* Start sequence for ClearURLs.
@@ -115,12 +122,6 @@ function genesis() {
//Set correct icon on startup
changeIcon();
// Start the context_menu
contextMenuStart();
// Start history listener
historyListenerStart();
}, handleError);
}
@@ -216,8 +217,6 @@ function initSettings() {
storage.badged_color = "#ffa500";
storage.hashURL = "https://rules2.clearurls.xyz/rules.minify.hash";
storage.ruleURL = "https://rules2.clearurls.xyz/data.minify.json";
storage.whitelist = []; // TODO: If we do whitelist per rule, this needs to be obj
storage.contextMenuEnabled = true;
storage.historyListenerEnabled = true;
storage.localHostsSkipping = true;
storage.referralMarketing = false;

View File

@@ -181,9 +181,9 @@ function changeIcon() {
checkOSAndroid().then((res) => {
if (!res) {
if (storage.globalStatus) {
browser.browserAction.setIcon({path: "img/clearurls_128x128.png"}).catch(handleError);
browser.action.setIcon({path: "img/clearurls_128x128.png"}).catch(handleError);
} else {
browser.browserAction.setIcon({path: "img/clearurls_gray_128x128.png"}).catch(handleError);
browser.action.setIcon({path: "img/clearurls_gray_128x128.png"}).catch(handleError);
}
}
});
@@ -200,13 +200,13 @@ function setBadgedStatus() {
let color = storage.badged_color;
if (storage.badged_color.charAt(0) !== '#')
color = '#' + storage.badged_color;
browser.browserAction.setBadgeBackgroundColor({
browser.action.setBadgeBackgroundColor({
'color': color
}).catch(handleError);
// Works only in Firefox: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/browserAction/setBadgeTextColor#Browser_compatibility
// Works only in Firefox: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/action/setBadgeTextColor#Browser_compatibility
if (getBrowser() === "Firefox") {
browser.browserAction.setBadgeTextColor({
browser.action.setBadgeTextColor({
color: "#FFFFFF"
}).catch(handleError);
}

View File

@@ -24,20 +24,23 @@
*
* This watchdog restarts the whole Add-on, when the check fails.
*/
const CHECK_INTERVAL = 60000;
const __dirtyURL = "https://clearurls.roebert.eu?utm_source=addon";
const __cleanURL = new URL("https://clearurls.roebert.eu").toString();
setInterval(function() {
if(isStorageAvailable() && storage.globalStatus) {
if(new URL(pureCleaning(__dirtyURL, true)).toString() !== __cleanURL) {
browser.alarms.create("watchdog", {
periodInMinutes: 1,
});
browser.alarms.onAlarm.addListener(function (alarmInfo) {
if (alarmInfo.name === "watchdog" && isStorageAvailable() && storage.globalStatus) {
if (new URL(pureCleaning(__dirtyURL, true)).toString() !== __cleanURL) {
storage.watchDogErrorCount += 1;
console.log(translate('watchdog', storage.watchDogErrorCount));
saveOnExit();
if(storage.watchDogErrorCount < 3) reload();
} else if(storage.watchDogErrorCount > 0){
if (storage.watchDogErrorCount < 3) reload();
} else if (storage.watchDogErrorCount > 0) {
storage.watchDogErrorCount = 0;
saveOnExit();
}
}
}, CHECK_INTERVAL);
});

View File

@@ -1,16 +0,0 @@
/*
* Source: https://github.com/mdn/webextensions-examples/tree/master/context-menu-copy-link-with-types
*/
function copyToClipboard(text) {
function oncopy(event) {
document.removeEventListener("copy", oncopy, true);
event.stopImmediatePropagation();
event.preventDefault();
event.clipboardData.setData("text/plain", text);
}
document.addEventListener("copy", oncopy, true);
document.execCommand("copy");
}

View File

@@ -86,9 +86,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<label id="configs_switch_statistics"></label>
</label>
<div class="clearfix"></div>
<div class="text-center">
<button type="button" id="whitelist_btn" class="btn btn-primary btn-sm text-wrap"></button>
</div>
<br />
</div>
</div>

View File

@@ -105,11 +105,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<input type="url" id="hashURL" value="" name="hashURL" class="form-control" />
</p>
<br />
<p>
<label id="whitelist_list_label"></label><br />
<input type="text" id="whitelist" value="" name="whitelist" class="form-control" />
</p>
<br />
<p>
<label id="types_label"></label><br />
<input type="text" id="types" value="" name="types" class="form-control" />
@@ -141,13 +136,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<span class="slider round"></span>
</label>
</p>
<p>
<label id="context_menu_enabled" style="font-weight: bold;"></label><br />
<label class="switch">
<input type="checkbox" id="contextMenuEnabled">
<span class="slider round"></span>
</label>
</p>
<p>
<label id="referral_marketing_enabled" style="font-weight: bold;"></label><br />
<label class="switch">

View File

@@ -1,12 +1,14 @@
{
"manifest_version": 2,
"manifest_version": 3,
"name": "ClearURLs",
"version": "1.27.0",
"version": "1.28.0",
"author": "Kevin Roebert",
"description": "__MSG_extension_description__",
"homepage_url": "https://docs.clearurls.xyz",
"default_locale": "en",
"content_security_policy": "script-src 'self'; object-src 'none'",
"content_security_policy": {
"extension_pages": "script-src 'self'; object-src 'none'"
},
"browser_specific_settings": {
"gecko": {
"id": "{74145f27-f039-47ce-a470-a662b129930a}"
@@ -25,8 +27,7 @@
"96": "img/clearurls_96x96.png",
"128": "img/clearurls_128x128.png"
},
"browser_action": {
"browser_style": true,
"action": {
"default_icon": {
"16": "img/clearurls_16x16.png",
"19": "img/clearurls_19x19.png",
@@ -44,7 +45,6 @@
"default_popup": "html/popup.html"
},
"permissions": [
"<all_urls>",
"webRequest",
"webRequestBlocking",
"storage",
@@ -52,7 +52,12 @@
"contextMenus",
"webNavigation",
"tabs",
"downloads"
"downloads",
"scripting",
"alarms"
],
"host_permissions": [
"<all_urls>"
],
"background": {
"scripts": [