This commit is contained in:
parent
a985977fdd
commit
f6ef6f977c
3 changed files with 93 additions and 22 deletions
55
common.js
55
common.js
|
@ -13,18 +13,22 @@ var prefs = {
|
||||||
ua: '',
|
ua: '',
|
||||||
blacklist: [],
|
blacklist: [],
|
||||||
whitelist: [],
|
whitelist: [],
|
||||||
|
custom: {},
|
||||||
mode: 'blacklist'
|
mode: 'blacklist'
|
||||||
};
|
};
|
||||||
|
|
||||||
chrome.storage.local.get(prefs, ps => {
|
chrome.storage.local.get(prefs, ps => {
|
||||||
Object.assign(prefs, ps);
|
Object.assign(prefs, ps);
|
||||||
update(prefs.ua);
|
update(prefs.mode === 'custom' ? 'Mapped from user\'s JSON object' : prefs.ua);
|
||||||
});
|
});
|
||||||
chrome.storage.onChanged.addListener(ps => {
|
chrome.storage.onChanged.addListener(ps => {
|
||||||
Object.keys(ps).forEach(key => prefs[key] = ps[key].newValue);
|
Object.keys(ps).forEach(key => prefs[key] = ps[key].newValue);
|
||||||
if (ps.ua) {
|
if (ps.ua) {
|
||||||
update(prefs.ua);
|
update(prefs.ua);
|
||||||
}
|
}
|
||||||
|
if (ps.mode) {
|
||||||
|
update(ps.mode.newValue === 'custom' ? 'Mapped from user\'s JSON object' : prefs.ua);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function hostname(url) {
|
function hostname(url) {
|
||||||
|
@ -55,7 +59,7 @@ function match(url) {
|
||||||
return prefs.blacklist.some(s => s === h);
|
return prefs.blacklist.some(s => s === h);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else if (prefs.mode === 'whitelist') {
|
||||||
if (prefs.whitelist.length) {
|
if (prefs.whitelist.length) {
|
||||||
const h = hostname(url);
|
const h = hostname(url);
|
||||||
return prefs.whitelist.some(s => s === h) === false;
|
return prefs.whitelist.some(s => s === h) === false;
|
||||||
|
@ -64,6 +68,10 @@ function match(url) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
const h = hostname(url);
|
||||||
|
return prefs.custom[h];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var cache = {};
|
var cache = {};
|
||||||
|
@ -73,12 +81,22 @@ var onBeforeSendHeaders = ({tabId, url, requestHeaders, type}) => {
|
||||||
if (type === 'main_frame') {
|
if (type === 'main_frame') {
|
||||||
cache[tabId] = match(url);
|
cache[tabId] = match(url);
|
||||||
}
|
}
|
||||||
if (cache[tabId]) {
|
if (cache[tabId] === true) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (let i = 0, name = requestHeaders[0].name; i < requestHeaders.length; i += 1, name = requestHeaders[i].name) {
|
for (let i = 0, name = requestHeaders[0].name; i < requestHeaders.length; i += 1, name = requestHeaders[i].name) {
|
||||||
if (name === 'User-Agent' || name === 'user-agent') {
|
if (name === 'User-Agent' || name === 'user-agent') {
|
||||||
requestHeaders[i].value = ua.userAgent;
|
if (prefs.mode === 'custom') {
|
||||||
|
if (cache[tabId]) {
|
||||||
|
requestHeaders[i].value = cache[tabId];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
requestHeaders[i].value = ua.userAgent;
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
requestHeaders
|
requestHeaders
|
||||||
};
|
};
|
||||||
|
@ -145,23 +163,34 @@ User-Agent String: ${str || navigator.userAgent}`
|
||||||
// FAQs & Feedback
|
// FAQs & Feedback
|
||||||
chrome.storage.local.get({
|
chrome.storage.local.get({
|
||||||
'version': null,
|
'version': null,
|
||||||
'faqs': navigator.userAgent.indexOf('Firefox')
|
'faqs': navigator.userAgent.indexOf('Firefox') === -1,
|
||||||
|
'last-update': 0,
|
||||||
}, prefs => {
|
}, prefs => {
|
||||||
const version = chrome.runtime.getManifest().version;
|
const version = chrome.runtime.getManifest().version;
|
||||||
|
|
||||||
if (prefs.version ? (prefs.faqs && prefs.version !== version) : true) {
|
if (prefs.version ? (prefs.faqs && prefs.version !== version) : true) {
|
||||||
const p = Boolean(prefs.version);
|
const now = Date.now();
|
||||||
chrome.storage.local.set({version}, () => {
|
const doUpdate = (now - prefs['last-update']) / 1000 / 60 / 60 / 24 > 30;
|
||||||
chrome.tabs.create({
|
chrome.storage.local.set({
|
||||||
url: 'http://add0n.com/useragent-switcher.html?version=' + version +
|
version,
|
||||||
'&type=' + (p ? ('upgrade&p=' + prefs.version) : 'install'),
|
'last-update': doUpdate ? Date.now() : prefs['last-update']
|
||||||
active: p === false
|
}, () => {
|
||||||
});
|
// do not display the FAQs page if last-update occurred less than 30 days ago.
|
||||||
|
if (doUpdate) {
|
||||||
|
const p = Boolean(prefs.version);
|
||||||
|
chrome.tabs.create({
|
||||||
|
url: chrome.runtime.getManifest().homepage_url + '?version=' + version +
|
||||||
|
'&type=' + (p ? ('upgrade&p=' + prefs.version) : 'install'),
|
||||||
|
active: p === false
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
{
|
{
|
||||||
const {name, version} = chrome.runtime.getManifest();
|
const {name, version} = chrome.runtime.getManifest();
|
||||||
chrome.runtime.setUninstallURL('http://add0n.com/feedback.html?name=' + name + '&version=' + version);
|
chrome.runtime.setUninstallURL(
|
||||||
|
chrome.runtime.getManifest().homepage_url + '?rd=feedback&name=' + name + '&version=' + version
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,10 @@
|
||||||
<style>
|
<style>
|
||||||
body { padding: 10px; }
|
body { padding: 10px; }
|
||||||
textarea { width: 100%; }
|
textarea { width: 100%; }
|
||||||
|
#custom {
|
||||||
|
white-space: nowrap;
|
||||||
|
overflow: auto;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
|
@ -24,13 +28,22 @@
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><textarea id="whitelist" rows="3"></textarea></td>
|
<td><textarea id="whitelist" rows="3" placeholder="e.g.: www.google.com, www.bing.com"></textarea></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
<label><input type="radio" name="mode" value="custom" id="mode-custom"> Custom mode: Only apply user-agent string to the tabs with a matching top-level hostnames.</label> <a href="#" id="sample">Insert</a> a sample.
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><textarea id="custom" rows="5"></textarea></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><label><input type="checkbox" id="faqs"> Open FAQs page on updates</label></td>
|
<td><label><input type="checkbox" id="faqs"> Open FAQs page on updates</label></td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
<p>
|
<p>
|
||||||
|
<button id="donate">Support Development</button>
|
||||||
<button id="save">Save</button>
|
<button id="save">Save</button>
|
||||||
<span id="status"></span>
|
<span id="status"></span>
|
||||||
</p>
|
</p>
|
||||||
|
|
|
@ -1,4 +1,13 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
function notify(msg) {
|
||||||
|
// Update status to let user know options were saved.
|
||||||
|
const status = document.getElementById('status');
|
||||||
|
status.textContent = msg;
|
||||||
|
clearTimeout(notify.id);
|
||||||
|
notify.id = setTimeout(() => status.textContent = '', 750);
|
||||||
|
}
|
||||||
|
|
||||||
function prepare(str) {
|
function prepare(str) {
|
||||||
return str.split(/\s*,\s*/)
|
return str.split(/\s*,\s*/)
|
||||||
.map(s => s.replace('http://', '')
|
.map(s => s.replace('http://', '')
|
||||||
|
@ -7,17 +16,23 @@ function prepare(str) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function save() {
|
function save() {
|
||||||
|
let custom = {};
|
||||||
|
try {
|
||||||
|
custom = JSON.parse(document.getElementById('custom').value);
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
notify(e.message);
|
||||||
|
}
|
||||||
|
|
||||||
chrome.storage.local.set({
|
chrome.storage.local.set({
|
||||||
faqs: document.getElementById('faqs').checked,
|
faqs: document.getElementById('faqs').checked,
|
||||||
blacklist: prepare(document.getElementById('blacklist').value),
|
blacklist: prepare(document.getElementById('blacklist').value),
|
||||||
whitelist: prepare(document.getElementById('whitelist').value),
|
whitelist: prepare(document.getElementById('whitelist').value),
|
||||||
mode: document.getElementById('mode-blacklist').checked ? 'blacklist' : 'whitelist'
|
custom,
|
||||||
|
mode: document.querySelector('[name="mode"]:checked').value
|
||||||
}, () => {
|
}, () => {
|
||||||
restore();
|
restore();
|
||||||
// Update status to let user know options were saved.
|
notify('Options saved.');
|
||||||
const status = document.getElementById('status');
|
|
||||||
status.textContent = 'Options saved.';
|
|
||||||
setTimeout(() => status.textContent = '', 750);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,14 +41,28 @@ function restore() {
|
||||||
faqs: true,
|
faqs: true,
|
||||||
mode: 'blacklist',
|
mode: 'blacklist',
|
||||||
whitelist: [],
|
whitelist: [],
|
||||||
blacklist: []
|
blacklist: [],
|
||||||
|
custom: {}
|
||||||
}, prefs => {
|
}, prefs => {
|
||||||
document.getElementById('faqs').checked = prefs.faqs;
|
document.getElementById('faqs').checked = prefs.faqs;
|
||||||
document.getElementById('mode-blacklist').checked = prefs.mode === 'blacklist';
|
document.querySelector(`[name="mode"][value="${prefs.mode}"`).checked = true;
|
||||||
document.getElementById('mode-whitelist').checked = prefs.mode === 'whitelist';
|
|
||||||
document.getElementById('blacklist').value = prefs.blacklist.join(', ');
|
document.getElementById('blacklist').value = prefs.blacklist.join(', ');
|
||||||
document.getElementById('whitelist').value = prefs.whitelist.join(', ');
|
document.getElementById('whitelist').value = prefs.whitelist.join(', ');
|
||||||
|
document.getElementById('custom').value = JSON.stringify(prefs.custom, null, 2);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
document.addEventListener('DOMContentLoaded', restore);
|
document.addEventListener('DOMContentLoaded', restore);
|
||||||
document.getElementById('save').addEventListener('click', save);
|
document.getElementById('save').addEventListener('click', save);
|
||||||
|
|
||||||
|
document.getElementById('sample').addEventListener('click', () => {
|
||||||
|
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.bing.com': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0'
|
||||||
|
}, null, 2);
|
||||||
|
});
|
||||||
|
|
||||||
|
document.getElementById('donate').addEventListener('click', () => {
|
||||||
|
chrome.tabs.create({
|
||||||
|
url: 'https://www.paypal.me/addondonation/10usd'
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in a new issue