This commit is contained in:
Ray Lothian 2019-08-01 07:53:48 +01:00
parent 3e947f5f80
commit 5ef9d91871
13 changed files with 50 additions and 29 deletions

View file

@ -2,18 +2,18 @@
'use strict'; 'use strict';
var cache = {}; const cache = {};
var tabs = {}; const tabs = {};
chrome.tabs.onRemoved.addListener(id => delete cache[id]); chrome.tabs.onRemoved.addListener(id => delete cache[id]);
chrome.tabs.onCreated.addListener(tab => tabs[tab.id] = tab.windowId); chrome.tabs.onCreated.addListener(tab => tabs[tab.id] = tab.windowId);
var prefs = { const prefs = {
ua: '', ua: '',
blacklist: [], blacklist: [],
whitelist: [], whitelist: [],
custom: {}, custom: {},
mode: 'blacklist', mode: 'blacklist',
color: '#ffa643', color: '#777',
cache: true, cache: true,
exactMatch: false, exactMatch: false,
protected: ['google.com/recaptcha', 'gstatic.com/recaptcha'] protected: ['google.com/recaptcha', 'gstatic.com/recaptcha']
@ -59,7 +59,7 @@ chrome.storage.onChanged.addListener(ps => {
} }
}); });
var ua = { const ua = {
_obj: { _obj: {
'global': {} 'global': {}
}, },
@ -96,27 +96,32 @@ var ua = {
this._obj[windowId] = {}; this._obj[windowId] = {};
} }
}, },
toolbar: ({windowId, tabId, str = ua.object(tabId, windowId).userAgent}) => { tooltip(title, tabId) {
const icon = { chrome.browserAction.setTitle({
title,
tabId
});
},
icon(mode, tabId) {
chrome.browserAction.setIcon({
tabId,
path: { path: {
16: 'data/icons/' + (str ? 'active/' : '') + '16.png', '16': 'data/icons/' + (mode ? mode + '/' : '') + '16.png',
32: 'data/icons/' + (str ? 'active/' : '') + '32.png', '18': 'data/icons/' + (mode ? mode + '/' : '') + '18.png',
48: 'data/icons/' + (str ? 'active/' : '') + '48.png', '19': 'data/icons/' + (mode ? mode + '/' : '') + '19.png',
64: 'data/icons/' + (str ? 'active/' : '') + '64.png' '32': 'data/icons/' + (mode ? mode + '/' : '') + '32.png',
'36': 'data/icons/' + (mode ? mode + '/' : '') + '36.png',
'38': 'data/icons/' + (mode ? mode + '/' : '') + '38.png',
'48': 'data/icons/' + (mode ? mode + '/' : '') + '48.png'
} }
}; });
const custom = 'Mapped from user\'s JSON object if found, otherwise uses "' + (str || navigator.userAgent) + '"'; },
const title = { toolbar: ({windowId, tabId, str = ua.object(tabId, windowId).userAgent}) => {
title: `UserAgent Switcher (${str ? 'enabled' : 'set to default'})
User-Agent String: ${prefs.mode === 'custom' ? custom : str || navigator.userAgent}`
};
if (windowId) { if (windowId) {
chrome.tabs.query({ chrome.tabs.query({
windowId windowId
}, tabs => tabs.forEach(tab => { }, tabs => tabs.forEach(tab => {
const tabId = tab.id; const tabId = tab.id;
chrome.browserAction.setTitle(Object.assign({tabId}, title));
chrome.browserAction.setBadgeText({ chrome.browserAction.setBadgeText({
tabId, tabId,
text: ua.object(null, windowId).platform.substr(0, 3) text: ua.object(null, windowId).platform.substr(0, 3)
@ -124,16 +129,11 @@ User-Agent String: ${prefs.mode === 'custom' ? custom : str || navigator.userAge
})); }));
} }
else if (tabId) { else if (tabId) {
chrome.browserAction.setTitle(Object.assign({tabId}, title));
chrome.browserAction.setBadgeText({ chrome.browserAction.setBadgeText({
tabId, tabId,
text: ua.object(tabId).platform.substr(0, 3) text: ua.object(tabId).platform.substr(0, 3)
}); });
} }
else {
chrome.browserAction.setIcon(icon);
chrome.browserAction.setTitle(title);
}
}, },
update(str = prefs.ua, windowId = 'global') { update(str = prefs.ua, windowId = 'global') {
chrome.webRequest.onBeforeSendHeaders.removeListener(onBeforeSendHeaders); chrome.webRequest.onBeforeSendHeaders.removeListener(onBeforeSendHeaders);
@ -145,7 +145,14 @@ User-Agent String: ${prefs.mode === 'custom' ? custom : str || navigator.userAge
'urls': ['*://*/*'] 'urls': ['*://*/*']
}, ['blocking', 'requestHeaders']); }, ['blocking', 'requestHeaders']);
chrome.webNavigation.onCommitted.addListener(onCommitted); chrome.webNavigation.onCommitted.addListener(onCommitted);
ua.tooltip('[Default] ' + navigator.userAgent);
ua.icon('ignored');
} }
else {
ua.icon('');
ua.tooltip('[Disabled] to enable, use the popup window');
}
if (windowId === 'global') { if (windowId === 'global') {
this.toolbar({str}); this.toolbar({str});
} }
@ -155,6 +162,7 @@ User-Agent String: ${prefs.mode === 'custom' ? custom : str || navigator.userAge
} }
} }
}; };
window.ua = ua; // using from popup
// make sure to clean on window removal // make sure to clean on window removal
if (chrome.windows) { // FF on Android if (chrome.windows) { // FF on Android
chrome.windows.onRemoved.addListener(windowId => delete ua._obj[windowId]); chrome.windows.onRemoved.addListener(windowId => delete ua._obj[windowId]);
@ -250,12 +258,12 @@ function match({url, tabId}) {
} }
} }
var onBeforeSendHeaders = ({tabId, url, requestHeaders, type}) => { const onBeforeSendHeaders = ({tabId, url, requestHeaders, type}) => {
if (type === 'main_frame' || prefs.cache === false) { if (type === 'main_frame' || prefs.cache === false) {
cache[tabId] = match({url, tabId}); cache[tabId] = match({url, tabId});
} }
if (cache[tabId] === true) { if (cache[tabId] === true) {
return; return {};
} }
if (prefs.protected.some(s => url.indexOf(s) !== -1)) { if (prefs.protected.some(s => url.indexOf(s) !== -1)) {
return {}; return {};
@ -273,7 +281,7 @@ var onBeforeSendHeaders = ({tabId, url, requestHeaders, type}) => {
} }
}; };
var onCommitted = ({frameId, url, tabId}) => { const onCommitted = ({frameId, url, tabId}) => {
if (url && (url.startsWith('http') || url.startsWith('ftp')) || url === 'about:blank') { if (url && (url.startsWith('http') || url.startsWith('ftp')) || url === 'about:blank') {
if (cache[tabId] === true) { if (cache[tabId] === true) {
return; return;
@ -307,7 +315,18 @@ var onCommitted = ({frameId, url, tabId}) => {
document.documentElement.appendChild(script); document.documentElement.appendChild(script);
script.remove(); script.remove();
}` }`
}, () => chrome.runtime.lastError); }, () => {
if (chrome.runtime.lastError) {
if (frameId === 0) {
ua.tooltip('[Default] ' + navigator.userAgent);
ua.icon('ignored', tabId);
}
}
else if (frameId === 0) {
ua.tooltip('[Custom] ' + userAgent);
ua.icon('active', tabId);
}
});
} }
} }
// change the toolbar icon if there is a per window UA setting // change the toolbar icon if there is a per window UA setting

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5 KiB

View file

@ -87,7 +87,9 @@ function update(ua) {
document.addEventListener('change', ({target}) => { document.addEventListener('change', ({target}) => {
if (target.closest('#filter')) { if (target.closest('#filter')) {
localStorage.setItem(target.id, target.value); localStorage.setItem(target.id, target.value);
update(); chrome.storage.local.get({
ua: ''
}, prefs => update(prefs.ua || navigator.userAgent));
} }
if (target.type === 'radio') { if (target.type === 'radio') {
document.getElementById('ua').value = target.closest('tr').querySelector('td:nth-child(4)').textContent; document.getElementById('ua').value = target.closest('tr').querySelector('td:nth-child(4)').textContent;