Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Whitelist #372

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@
"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."
Expand Down Expand Up @@ -179,6 +187,10 @@
"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."
Expand Down
13 changes: 13 additions & 0 deletions clearurls.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ 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) {
Copy link
Member

Choose a reason for hiding this comment

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

!==

return {
"changes": false,
"url": url,
"cancel": false
}
}
}

if (storage.localHostsSkipping && checkLocalURL(urlObject)) {
return {
"changes": false,
Expand Down
68 changes: 68 additions & 0 deletions core_js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,40 @@ function changeStatistics()
elTotal.textContent = totalCounter.toLocaleString();
}

/**
* Set the whitelist button text
*/
function setWhitelistText()
{
let element = document.getElementById('whitelist_btn');
let currentSite, siteFound;
browser.tabs.query({active: true, currentWindow: true}, function(tabs) {
currentSite = tabs[0].url;
});

browser.runtime.sendMessage({
function: "getData",
params: ['whitelist']
}).then((data) => {
data.response.forEach(site => {
Copy link
Member

Choose a reason for hiding this comment

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

Should be replaced by the following: let siteFound = data.response.some(site => currentSite.indexOf(site) !== -1);

if (currentSite.indexOf(site) != -1) {
siteFound = true
}
});
if (!siteFound) {
Copy link
Member

Choose a reason for hiding this comment

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

Swap both cases

if (data.response.length != 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Remove this If. It causes the button to retain the "btn-danger" class when a page is added and immediately removed.

element.classList.replace('btn-danger', 'btn-primary')
}
element.textContent = translate('popup_html_configs_whitelist_button_add')
document.getElementById('whitelist_btn').onclick = changeWhitelist;
Copy link
Member

Choose a reason for hiding this comment

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

Pass the desired state here: () => {changeWhitelist(true)}

} else {
element.classList.replace('btn-primary', 'btn-danger')
element.textContent = translate('popup_html_configs_whitelist_button_remove')
document.getElementById('whitelist_btn').onclick = () => {changeWhitelist(true)};
}
}).catch(handleError);
}

/**
* Set the value for the hashStatus on startUp.
*/
Expand Down Expand Up @@ -155,6 +189,39 @@ 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} remove If true remove current site instead of adding
*/
function changeWhitelist(removeWl = false) {
if (removeWl != true) { // Handle click obj
Copy link
Member

Choose a reason for hiding this comment

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

Assignment is useless. It is better to pass the correct state in the onclick event

removeWl = false
}
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 == false) {
Copy link
Member

Choose a reason for hiding this comment

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

Swap both cases

data.response.push(domain)
} else {
data.response = data.response.filter(wlSite => wlSite != domain)
Copy link
Member

Choose a reason for hiding this comment

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

Unsafe comparison, better !==

}
browser.runtime.sendMessage({
function: "setData",
params: ['whitelist', data.response]
}).then(() => {
setWhitelistText();
}).catch(handleError);
}).catch(handleError);
}

/**
* Reset the global statistic
*/
Expand Down Expand Up @@ -220,6 +287,7 @@ 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');
}

Expand Down
3 changes: 3 additions & 0 deletions core_js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ 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({
Expand Down Expand Up @@ -122,6 +123,7 @@ function getData() {

loadData("ruleURL")
.then(() => loadData("hashURL"))
.then(() => loadData("whitelist"))
.then(() => loadData("types"))
.then(() => loadData("logLimit"))
.then(logData => {
Expand Down Expand Up @@ -216,6 +218,7 @@ 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'));
Expand Down
1 change: 1 addition & 0 deletions core_js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ 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;
Expand Down
3 changes: 3 additions & 0 deletions html/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ <h5><b id="configs_head"></b></h5>
<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>
Expand Down
5 changes: 5 additions & 0 deletions html/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@
<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" />
Expand Down