This commit is contained in:
Ray Lothian 2019-11-05 14:04:18 +00:00
parent df861e16ca
commit 00909fa7b9
2 changed files with 35 additions and 18 deletions

View file

@ -19,8 +19,22 @@ const prefs = {
protected: ['google.com/recaptcha', 'gstatic.com/recaptcha'], protected: ['google.com/recaptcha', 'gstatic.com/recaptcha'],
parser: {} // maps ua string to a ua object parser: {} // maps ua string to a ua object
}; };
// exand comma-separated keys of prefs.custom
const expand = () => {
expand.rules = {};
for (const key of Object.keys(prefs.custom)) {
for (const k of key.split(/\s*,\s*/)) {
if (k) {
expand.rules[k] = prefs.custom[key];
}
}
}
};
expand.rules = {};
chrome.storage.local.get(prefs, ps => { chrome.storage.local.get(prefs, ps => {
Object.assign(prefs, ps); Object.assign(prefs, ps);
expand();
chrome.tabs.query({}, ts => { chrome.tabs.query({}, ts => {
ts.forEach(t => tabs[t.id] = t.windowId); ts.forEach(t => tabs[t.id] = t.windowId);
ua.update(); ua.update();
@ -58,6 +72,9 @@ chrome.storage.onChanged.addListener(ps => {
if (ps.ua || ps.mode) { if (ps.ua || ps.mode) {
ua.update(); ua.update();
} }
else if (ps.custom) {
expand();
}
}); });
const ua = { const ua = {
@ -229,9 +246,10 @@ function hostname(url) {
} }
// returns true, false or an object; true: ignore, false: use from ua object. // returns true, false or an object; true: ignore, false: use from ua object.
function match({url, tabId}) { function match({url, tabId}) {
const h = hostname(url);
if (prefs.mode === 'blacklist') { if (prefs.mode === 'blacklist') {
if (prefs.blacklist.length) { if (prefs.blacklist.length) {
const h = hostname(url);
return prefs.blacklist.some(s => { return prefs.blacklist.some(s => {
if (s === h) { if (s === h) {
return true; return true;
@ -244,7 +262,6 @@ function match({url, tabId}) {
} }
else if (prefs.mode === 'whitelist') { else if (prefs.mode === 'whitelist') {
if (prefs.whitelist.length) { if (prefs.whitelist.length) {
const h = hostname(url);
return prefs.whitelist.some(s => { return prefs.whitelist.some(s => {
if (s === h) { if (s === h) {
return true; return true;
@ -259,30 +276,30 @@ function match({url, tabId}) {
} }
} }
else { else {
const h = hostname(url); const [hh] = h.split(':');
const key = Object.keys(prefs.custom).filter(s => { const key = Object.keys(expand.rules).filter(s => {
if (s === h) { if (s === h || s === hh) {
return true; return true;
} }
else if (prefs.exactMatch === false) { else if (prefs.exactMatch === false) {
return s.endsWith('.' + h) || h.endsWith('.' + s); return s.endsWith('.' + h) || h.endsWith('.' + s) || s.endsWith('.' + hh) || hh.endsWith('.' + s);
} }
}).shift(); }).shift();
let s = prefs.custom[key] || prefs.custom['*']; let s = expand.rules[key] || expand.rules['*'];
// if s is an array select a random string // if s is an array select a random string
if (Array.isArray(s)) { if (Array.isArray(s)) {
s = s[Math.floor(Math.random() * s.length)]; s = s[Math.floor(Math.random() * s.length)];
// set session mode if key is either on _[key] or _['*'] lists // set session mode if key is either on _[key] or _['*'] lists
if (prefs.custom._ && Array.isArray(prefs.custom._)) { if (expand.rules._ && Array.isArray(expand.rules._)) {
if (prefs.custom._.indexOf(key) !== -1) { if (expand.rules._.indexOf(key) !== -1) {
prefs.custom[key] = s; expand.rules[key] = s;
} }
else if (prefs.custom._.indexOf('*') !== -1) { else if (expand.rules._.indexOf('*') !== -1) {
if (prefs.custom[key]) { if (expand.rules[key]) {
prefs.custom[key] = s; expand.rules[key] = s;
} }
else if (prefs.custom['*']) { else if (expand.rules['*']) {
prefs.custom['*'] = s; expand.rules['*'] = s;
} }
} }
} }
@ -351,12 +368,12 @@ const onCommitted = ({frameId, url, tabId}) => {
}, () => { }, () => {
if (chrome.runtime.lastError) { if (chrome.runtime.lastError) {
if (frameId === 0) { if (frameId === 0) {
ua.tooltip('[Default] ' + navigator.userAgent); ua.tooltip('[Default] ' + navigator.userAgent, tabId);
ua.icon('ignored', tabId); ua.icon('ignored', tabId);
} }
} }
else if (frameId === 0) { else if (frameId === 0) {
ua.tooltip('[Custom] ' + o.userAgent); ua.tooltip('[Custom] ' + o.userAgent, tabId);
ua.icon('active', tabId); ua.icon('active', tabId);
} }
}); });

View file

@ -90,7 +90,7 @@ document.getElementById('sample').addEventListener('click', e => {
document.getElementById('custom').value = JSON.stringify({ document.getElementById('custom').value = JSON.stringify({
'www.google.com': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36', 'www.google.com': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',
'www.bing.com': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0', 'www.bing.com, www.yahoo.com, www.wikipedia.org': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0',
'www.example.com': ['random-useragent-1', 'random-user-agent-2'], 'www.example.com': ['random-useragent-1', 'random-user-agent-2'],
'*': 'useragent-for-all-hostnames' '*': 'useragent-for-all-hostnames'
}, null, 2); }, null, 2);