From 486629ad0cadfdac3398a71c50ff5af735a2df47 Mon Sep 17 00:00:00 2001 From: Ray Lothian Date: Sun, 6 Sep 2020 09:36:28 +0200 Subject: [PATCH] supports sibling hostnames. fixes #93 --- extension/common.js | 42 +++++++++++++++++++++++++++--- extension/data/options/index.css | 1 + extension/data/options/index.html | 9 ++++++- extension/data/options/index.js | 43 ++++++++++++++++++++++++++++++- 4 files changed, 90 insertions(+), 5 deletions(-) diff --git a/extension/common.js b/extension/common.js index 383426d..38cd2f0 100644 --- a/extension/common.js +++ b/extension/common.js @@ -22,6 +22,10 @@ const prefs = { blacklist: [], whitelist: [], custom: {}, + siblings: { + 'www.google.com': 0, + 'www.youtube.com': 0 + }, // a list of domains that are considered siblings (use same index for all) mode: 'blacklist', color: '#777', cache: true, @@ -34,7 +38,7 @@ window.prefs = prefs; // access from popup const log = (...args) => prefs.log && console.log(...args); -// exand comma-separated keys of prefs.custom +// expand comma-separated keys of prefs.custom and add missing keys const expand = () => { log('expanding custom rules'); expand.rules = {}; @@ -42,6 +46,25 @@ const expand = () => { for (const k of key.split(/\s*,\s*/)) { if (k) { expand.rules[k] = prefs.custom[key]; + // make sure all siblings have the same expanded rule + const i = prefs.siblings[key]; + if (i !== undefined) { + for (const [hostname, j] of Object.entries(prefs.siblings)) { + if (i === j) { + expand.rules[hostname] = expand.rules[hostname] || prefs.custom[key]; + if (expand.rules._) { + const x = expand.rules._.indexOf(key); + const y = expand.rules._.indexOf(hostname); + if (x !== -1 && y === -1) { + expand.rules._.push(hostname); + } + if (x === -1 && y !== -1) { + expand.rules._.push(key); + } + } + } + } + } } } } @@ -373,7 +396,20 @@ function match({url, tabId, cookieStoreId = DCSI}) { return s.endsWith('.' + h) || h.endsWith('.' + s) || s.endsWith('.' + hh) || hh.endsWith('.' + s); } }).shift(); - let s = expand.rules[key] || expand.rules['*']; + let s; + // try to use an already resolved sibling hostname + const i = prefs.siblings[key]; + if (i !== undefined) { + for (const [hostname, j] of Object.entries(prefs.siblings)) { + if (j === i && expand.rules[hostname] && typeof expand.rules[hostname] === 'string') { + s = expand.rules[hostname]; + } + } + } + s = s || expand.rules[key]; + // use '*' when the hostname specific key is not found + s = s || expand.rules['*']; + console.log(s); // if s is an array select a random string if (Array.isArray(s)) { s = s[Math.floor(Math.random() * s.length)]; @@ -418,7 +454,7 @@ const onBeforeSendHeaders = d => { } const o = (cache[tabId] || ua.object(tabId, undefined, cookieStoreId)); const str = o ? o.userAgent : ''; - if (str) { + if (str && requestHeaders.length) { for (let i = 0, name = requestHeaders[0].name; i < requestHeaders.length; i += 1, name = (requestHeaders[i] || {}).name) { if (name === 'User-Agent' || name === 'user-agent') { requestHeaders[i].value = str === 'empty' ? '' : str; diff --git a/extension/data/options/index.css b/extension/data/options/index.css index 898deb0..f4d56ce 100644 --- a/extension/data/options/index.css +++ b/extension/data/options/index.css @@ -52,6 +52,7 @@ textarea { .mode-2 { grid-template-columns: min-content min-content; } +#toggle-sibling-desc, #toggle-parser-desc, #toggle-protected-desc, #toggle-custom-desc, diff --git a/extension/data/options/index.html b/extension/data/options/index.html index 7940724..436290f 100644 --- a/extension/data/options/index.html +++ b/extension/data/options/index.html @@ -29,7 +29,7 @@ Description - +
@@ -56,6 +56,13 @@ +
+

Sibling Hostnames

+ Description +
+ + +
diff --git a/extension/data/options/index.js b/extension/data/options/index.js index 9775111..5ac02c9 100644 --- a/extension/data/options/index.js +++ b/extension/data/options/index.js @@ -36,10 +36,28 @@ function save() { catch (e) { window.setTimeout(() => { notify('Parser JSON error: ' + e.message, 5000); - document.getElementById('parser').value = c; + document.getElementById('parser').value = p; }, 1000); } + let siblings = {}; + const s = document.getElementById('siblings').value; + try { + siblings = JSON.parse(s); + siblings = siblings.reduce((p, c, i) => { + c.forEach(hostname => p[hostname] = i); + return p; + }, {}); + } + catch (e) { + window.setTimeout(() => { + notify('Sibling JSON error: ' + e.message, 5000); + document.getElementById('siblings').value = c; + }, 1000); + } + console.log(siblings); + + chrome.storage.local.set({ exactMatch: document.getElementById('exactMatch').checked, faqs: document.getElementById('faqs').checked, @@ -49,6 +67,7 @@ function save() { whitelist: prepare(document.getElementById('whitelist').value), custom, parser, + siblings, mode: document.querySelector('[name="mode"]:checked').value, protected: document.getElementById('protected').value.split(/\s*,\s*/).filter(s => s.length > 4) }, () => { @@ -71,6 +90,10 @@ function restore() { blacklist: [], custom: {}, parser: {}, + siblings: { + 'www.google.com': 0, + 'www.youtube.com': 0 + }, protected: ['google.com/recaptcha', 'gstatic.com/recaptcha'] }, prefs => { document.getElementById('exactMatch').checked = prefs.exactMatch; @@ -82,6 +105,11 @@ function restore() { document.getElementById('whitelist').value = prefs.whitelist.join(', '); document.getElementById('custom').value = JSON.stringify(prefs.custom, null, 2); document.getElementById('parser').value = JSON.stringify(prefs.parser, null, 2); + document.getElementById('siblings').value = JSON.stringify(Object.entries(prefs.siblings).reduce((p, [hostname, index]) => { + p[index] = p[index] || []; + p[index].push(hostname); + return p; + }, []), null, 2); document.getElementById('protected').value = prefs.protected.join(', '); }); } @@ -114,6 +142,16 @@ document.getElementById('sample-2').addEventListener('click', e => { }, null, 2); }); +document.getElementById('sample-3').addEventListener('click', e => { + e.preventDefault(); + + document.getElementById('siblings').value = JSON.stringify([[ + 'www.google.com', 'www.youtube.com', 'www.youtube.be' + ], [ + 'www.gmx.com', 'www.mail.com' + ]], null, 2); +}); + document.getElementById('donate').addEventListener('click', () => { chrome.tabs.create({ url: chrome.runtime.getManifest().homepage_url + '?rd=donate' @@ -205,3 +243,6 @@ document.getElementById('toggle-protected-desc').addEventListener('click', () => document.getElementById('toggle-parser-desc').addEventListener('click', () => { document.querySelector('[for="toggle-parser-desc"]').classList.toggle('hidden'); }); +document.getElementById('toggle-sibling-desc').addEventListener('click', () => { + document.querySelector('[for="toggle-sibling-desc"]').classList.toggle('hidden'); +});