Ray Lothian 2017-12-13 16:15:54 +03:30
parent a985977fdd
commit f6ef6f977c
3 changed files with 93 additions and 22 deletions

View file

@ -13,18 +13,22 @@ var prefs = {
ua: '',
blacklist: [],
whitelist: [],
custom: {},
mode: 'blacklist'
};
chrome.storage.local.get(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 => {
Object.keys(ps).forEach(key => prefs[key] = ps[key].newValue);
if (ps.ua) {
update(prefs.ua);
}
if (ps.mode) {
update(ps.mode.newValue === 'custom' ? 'Mapped from user\'s JSON object' : prefs.ua);
}
});
function hostname(url) {
@ -55,7 +59,7 @@ function match(url) {
return prefs.blacklist.some(s => s === h);
}
}
else {
else if (prefs.mode === 'whitelist') {
if (prefs.whitelist.length) {
const h = hostname(url);
return prefs.whitelist.some(s => s === h) === false;
@ -64,6 +68,10 @@ function match(url) {
return true;
}
}
else {
const h = hostname(url);
return prefs.custom[h];
}
}
var cache = {};
@ -73,12 +81,22 @@ var onBeforeSendHeaders = ({tabId, url, requestHeaders, type}) => {
if (type === 'main_frame') {
cache[tabId] = match(url);
}
if (cache[tabId]) {
if (cache[tabId] === true) {
return;
}
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 = ua.userAgent;
if (prefs.mode === 'custom') {
if (cache[tabId]) {
requestHeaders[i].value = cache[tabId];
}
else {
return;
}
}
else {
requestHeaders[i].value = ua.userAgent;
}
return {
requestHeaders
};
@ -145,23 +163,34 @@ User-Agent String: ${str || navigator.userAgent}`
// FAQs & Feedback
chrome.storage.local.get({
'version': null,
'faqs': navigator.userAgent.indexOf('Firefox')
'faqs': navigator.userAgent.indexOf('Firefox') === -1,
'last-update': 0,
}, prefs => {
const version = chrome.runtime.getManifest().version;
if (prefs.version ? (prefs.faqs && prefs.version !== version) : true) {
const p = Boolean(prefs.version);
chrome.storage.local.set({version}, () => {
chrome.tabs.create({
url: 'http://add0n.com/useragent-switcher.html?version=' + version +
'&type=' + (p ? ('upgrade&p=' + prefs.version) : 'install'),
active: p === false
});
const now = Date.now();
const doUpdate = (now - prefs['last-update']) / 1000 / 60 / 60 / 24 > 30;
chrome.storage.local.set({
version,
'last-update': doUpdate ? Date.now() : prefs['last-update']
}, () => {
// 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();
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
);
}

View file

@ -5,6 +5,10 @@
<style>
body { padding: 10px; }
textarea { width: 100%; }
#custom {
white-space: nowrap;
overflow: auto;
}
</style>
</head>
@ -24,13 +28,22 @@
</td>
</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>
<td><label><input type="checkbox" id="faqs"> Open FAQs page on updates</label></td>
</tr>
</table>
<p>
<button id="donate">Support Development</button>
<button id="save">Save</button>
<span id="status"></span>
</p>

View file

@ -1,4 +1,13 @@
'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) {
return str.split(/\s*,\s*/)
.map(s => s.replace('http://', '')
@ -7,17 +16,23 @@ function prepare(str) {
}
function save() {
let custom = {};
try {
custom = JSON.parse(document.getElementById('custom').value);
}
catch (e) {
notify(e.message);
}
chrome.storage.local.set({
faqs: document.getElementById('faqs').checked,
blacklist: prepare(document.getElementById('blacklist').value),
whitelist: prepare(document.getElementById('whitelist').value),
mode: document.getElementById('mode-blacklist').checked ? 'blacklist' : 'whitelist'
custom,
mode: document.querySelector('[name="mode"]:checked').value
}, () => {
restore();
// Update status to let user know options were saved.
const status = document.getElementById('status');
status.textContent = 'Options saved.';
setTimeout(() => status.textContent = '', 750);
notify('Options saved.');
});
}
@ -26,14 +41,28 @@ function restore() {
faqs: true,
mode: 'blacklist',
whitelist: [],
blacklist: []
blacklist: [],
custom: {}
}, prefs => {
document.getElementById('faqs').checked = prefs.faqs;
document.getElementById('mode-blacklist').checked = prefs.mode === 'blacklist';
document.getElementById('mode-whitelist').checked = prefs.mode === 'whitelist';
document.querySelector(`[name="mode"][value="${prefs.mode}"`).checked = true;
document.getElementById('blacklist').value = prefs.blacklist.join(', ');
document.getElementById('whitelist').value = prefs.whitelist.join(', ');
document.getElementById('custom').value = JSON.stringify(prefs.custom, null, 2);
});
}
document.addEventListener('DOMContentLoaded', restore);
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'
});
});