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 support for custom rules #250

Open
wants to merge 2 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
6 changes: 5 additions & 1 deletion _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@
"message": "The url to the data.json file (rules)",
"description": "This string is used as name for the rule url label."
},
"setting_custom_rules_label": {
"message": "Custom rules",
"description": "This string is used as name for the custom rules label."
},
"settings_html_save_button": {
"message": "Save & reload addon",
"description": "This string is used as name for the save&reload button on the settings page."
Expand Down Expand Up @@ -375,4 +379,4 @@
"message": " ",
"description": "not needed, only to prevent exceptions"
}
}
}
87 changes: 46 additions & 41 deletions clearurls.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ function start() {
* @param {object} obj
*/
function getKeys(obj) {
for (const key in obj) {
for (const key of Object.keys(obj)) {
prvKeys.push(key);
}
}
Expand All @@ -182,51 +182,51 @@ function start() {
* Initialize the providers form the JSON object.
*
*/
function createProviders() {
let data = storage.ClearURLsData;

function createProviders(data) {
for (let p = 0; p < prvKeys.length; p++) {
//Create new provider
providers.push(new Provider(prvKeys[p], data.providers[prvKeys[p]].getOrDefault('completeProvider', false),
data.providers[prvKeys[p]].getOrDefault('forceRedirection', false)));

//Add URL Pattern
providers[p].setURLPattern(data.providers[prvKeys[p]].getOrDefault('urlPattern', ''));

let rules = data.providers[prvKeys[p]].getOrDefault('rules', []);
//Add rules to provider
for (let r = 0; r < rules.length; r++) {
providers[p].addRule(rules[r]);
}
if (prvKeys[p] in data.providers) {
//Create new provider
providers.push(new Provider(prvKeys[p], data.providers[prvKeys[p]].getOrDefault('completeProvider', false),
data.providers[prvKeys[p]].getOrDefault('forceRedirection', false)));

//Add URL Pattern
providers[p].setURLPattern(data.providers[prvKeys[p]].getOrDefault('urlPattern', ''));

let rules = data.providers[prvKeys[p]].getOrDefault('rules', []);
//Add rules to provider
for (let r = 0; r < rules.length; r++) {
providers[p].addRule(rules[r]);
}

let rawRules = data.providers[prvKeys[p]].getOrDefault('rawRules', []);
//Add raw rules to provider
for (let raw = 0; raw < rawRules.length; raw++) {
providers[p].addRawRule(rawRules[raw]);
}
let rawRules = data.providers[prvKeys[p]].getOrDefault('rawRules', []);
//Add raw rules to provider
for (let raw = 0; raw < rawRules.length; raw++) {
providers[p].addRawRule(rawRules[raw]);
}

let referralMarketingRules = data.providers[prvKeys[p]].getOrDefault('referralMarketing', []);
//Add referral marketing rules to provider
for (let referralMarketing = 0; referralMarketing < referralMarketingRules.length; referralMarketing++) {
providers[p].addReferralMarketing(referralMarketingRules[referralMarketing]);
}
let referralMarketingRules = data.providers[prvKeys[p]].getOrDefault('referralMarketing', []);
//Add referral marketing rules to provider
for (let referralMarketing = 0; referralMarketing < referralMarketingRules.length; referralMarketing++) {
providers[p].addReferralMarketing(referralMarketingRules[referralMarketing]);
}

let exceptions = data.providers[prvKeys[p]].getOrDefault('exceptions', []);
//Add exceptions to provider
for (let e = 0; e < exceptions.length; e++) {
providers[p].addException(exceptions[e]);
}
let exceptions = data.providers[prvKeys[p]].getOrDefault('exceptions', []);
//Add exceptions to provider
for (let e = 0; e < exceptions.length; e++) {
providers[p].addException(exceptions[e]);
}

let redirections = data.providers[prvKeys[p]].getOrDefault('redirections', []);
//Add redirections to provider
for (let re = 0; re < redirections.length; re++) {
providers[p].addRedirection(redirections[re]);
}
let redirections = data.providers[prvKeys[p]].getOrDefault('redirections', []);
//Add redirections to provider
for (let re = 0; re < redirections.length; re++) {
providers[p].addRedirection(redirections[re]);
}

let methods = data.providers[prvKeys[p]].getOrDefault('methods', []);
//Add HTTP methods list to provider
for (let re = 0; re < methods.length; re++) {
providers[p].addMethod(methods[re]);
let methods = data.providers[prvKeys[p]].getOrDefault('methods', []);
//Add HTTP methods list to provider
for (let re = 0; re < methods.length; re++) {
providers[p].addMethod(methods[re]);
}
}
}
}
Expand All @@ -239,7 +239,12 @@ function start() {
*/
function toObject(retrievedText) {
getKeys(storage.ClearURLsData.providers);
createProviders();
createProviders(storage.ClearURLsData);

if("providers" in storage.customRules) {
getKeys(storage.customRules.providers);
createProviders(storage.customRules);
}
}

/**
Expand Down
16 changes: 14 additions & 2 deletions core_js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ 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(
"customRules",
document.querySelector('textarea[name=customRules]').value
), handleError)
.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 +126,7 @@ function getData() {

loadData("ruleURL")
.then(() => loadData("hashURL"))
.then(() => loadData("customRules"))
.then(() => loadData("types"))
.then(() => loadData("logLimit"))
.then(logData => {
Expand Down Expand Up @@ -163,10 +168,16 @@ async function loadData(name) {
params: [name]
}).then(data => {
settings[name] = data.response;
if (document.querySelector('input[id=' + name + ']') == null) {
if (document.querySelector('*[id=' + name + ']') == null) {
console.debug(name)
}
document.querySelector('input[id=' + name + ']').value = data.response;
switch (name) {
case "customRules":
document.querySelector('*[id=' + name + ']').value = JSON.stringify(data.response);
break;
default:
document.querySelector('*[id=' + name + ']').value = data.response;
}
resolve(data);
}, handleError);
});
Expand Down Expand Up @@ -202,6 +213,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('custom_rules_label').textContent = translate('setting_custom_rules_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
3 changes: 3 additions & 0 deletions core_js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function storageDataAsString(key) {
switch (key) {
case "ClearURLsData":
case "log":
case "customRules":
return JSON.stringify(value);
case "types":
return value.toString();
Expand Down Expand Up @@ -152,6 +153,7 @@ function getEntireData() {
*/
function setData(key, value) {
switch (key) {
case "customRules":
case "ClearURLsData":
case "log":
storage[key] = JSON.parse(value);
Expand Down Expand Up @@ -216,6 +218,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.customRules = {};
storage.contextMenuEnabled = true;
storage.historyListenerEnabled = true;
storage.localHostsSkipping = true;
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="custom_rules_label"></label><br />
<textarea id="customRules" name="customRules" class="form-control"></textarea>
</p>
<br />
<p>
<label id="types_label"></label><br />
<input type="text" id="types" value="" name="types" class="form-control" />
Expand Down