talkdesk-useragent-switcher/extension/data/options/index.js
2019-11-05 14:04:18 +00:00

137 lines
4.3 KiB
JavaScript

'use strict';
function notify(msg, period = 750) {
// 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 = '', period);
}
function prepare(str) {
return str.split(/\s*,\s*/)
.map(s => s.replace('http://', '')
.replace('https://', '').split('/')[0].trim())
.filter((h, i, l) => h && l.indexOf(h) === i);
}
function save() {
let custom = {};
const c = document.getElementById('custom').value;
try {
custom = JSON.parse(c);
}
catch (e) {
window.setTimeout(() => {
notify('Custom JSON error: ' + e.message, 5000);
document.getElementById('custom').value = c;
}, 1000);
}
let parser = {};
const p = document.getElementById('parser').value;
try {
parser = JSON.parse(p);
}
catch (e) {
window.setTimeout(() => {
notify('Parser JSON error: ' + e.message, 5000);
document.getElementById('parser').value = c;
}, 1000);
}
chrome.storage.local.set({
exactMatch: document.getElementById('exactMatch').checked,
faqs: document.getElementById('faqs').checked,
cache: document.getElementById('cache').checked,
blacklist: prepare(document.getElementById('blacklist').value),
whitelist: prepare(document.getElementById('whitelist').value),
custom,
parser,
mode: document.querySelector('[name="mode"]:checked').value,
protected: document.getElementById('protected').value.split(/\s*,\s*/).filter(s => s.length > 4)
}, () => {
restore();
notify('Options saved.');
chrome.contextMenus.update(document.querySelector('[name="mode"]:checked').value, {
checked: true
});
});
}
function restore() {
chrome.storage.local.get({
exactMatch: false,
faqs: true,
cache: true,
mode: 'blacklist',
whitelist: [],
blacklist: [],
custom: {},
parser: {},
protected: ['google.com/recaptcha', 'gstatic.com/recaptcha']
}, prefs => {
document.getElementById('exactMatch').checked = prefs.exactMatch;
document.getElementById('faqs').checked = prefs.faqs;
document.getElementById('cache').checked = prefs.cache;
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.getElementById('parser').value = JSON.stringify(prefs.parser, null, 2);
document.getElementById('protected').value = prefs.protected.join(', ');
});
}
document.addEventListener('DOMContentLoaded', restore);
document.getElementById('save').addEventListener('click', save);
document.getElementById('sample').addEventListener('click', e => {
e.preventDefault();
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, 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'],
'*': 'useragent-for-all-hostnames'
}, null, 2);
});
document.getElementById('sample-2').addEventListener('click', e => {
e.preventDefault();
document.getElementById('parser').value = JSON.stringify({
'my-custom-useragent': {
'appVersion': 'custom app version',
'platform': 'custom platform',
'vendor': '[delete]',
'product': 'custom product',
'oscpu': 'custom oscpu',
'custom-variable': 'this is a custom variable'
}
}, null, 2);
});
document.getElementById('donate').addEventListener('click', () => {
chrome.tabs.create({
url: chrome.runtime.getManifest().homepage_url + '?rd=donate'
});
});
document.getElementById('reset').addEventListener('click', e => {
if (e.detail === 1) {
notify('Double-click to reset!');
}
else {
localStorage.clear();
chrome.storage.local.clear(() => {
chrome.runtime.reload();
window.close();
});
}
});
document.getElementById('help').addEventListener('click', () => {
chrome.tabs.create({
url: chrome.runtime.getManifest().homepage_url
});
});