building the UA objects from multiple JSON lists to improve popup's startup performance
373
extension/LICENSE
Normal file
|
@ -0,0 +1,373 @@
|
|||
Mozilla Public License Version 2.0
|
||||
==================================
|
||||
|
||||
1. Definitions
|
||||
--------------
|
||||
|
||||
1.1. "Contributor"
|
||||
means each individual or legal entity that creates, contributes to
|
||||
the creation of, or owns Covered Software.
|
||||
|
||||
1.2. "Contributor Version"
|
||||
means the combination of the Contributions of others (if any) used
|
||||
by a Contributor and that particular Contributor's Contribution.
|
||||
|
||||
1.3. "Contribution"
|
||||
means Covered Software of a particular Contributor.
|
||||
|
||||
1.4. "Covered Software"
|
||||
means Source Code Form to which the initial Contributor has attached
|
||||
the notice in Exhibit A, the Executable Form of such Source Code
|
||||
Form, and Modifications of such Source Code Form, in each case
|
||||
including portions thereof.
|
||||
|
||||
1.5. "Incompatible With Secondary Licenses"
|
||||
means
|
||||
|
||||
(a) that the initial Contributor has attached the notice described
|
||||
in Exhibit B to the Covered Software; or
|
||||
|
||||
(b) that the Covered Software was made available under the terms of
|
||||
version 1.1 or earlier of the License, but not also under the
|
||||
terms of a Secondary License.
|
||||
|
||||
1.6. "Executable Form"
|
||||
means any form of the work other than Source Code Form.
|
||||
|
||||
1.7. "Larger Work"
|
||||
means a work that combines Covered Software with other material, in
|
||||
a separate file or files, that is not Covered Software.
|
||||
|
||||
1.8. "License"
|
||||
means this document.
|
||||
|
||||
1.9. "Licensable"
|
||||
means having the right to grant, to the maximum extent possible,
|
||||
whether at the time of the initial grant or subsequently, any and
|
||||
all of the rights conveyed by this License.
|
||||
|
||||
1.10. "Modifications"
|
||||
means any of the following:
|
||||
|
||||
(a) any file in Source Code Form that results from an addition to,
|
||||
deletion from, or modification of the contents of Covered
|
||||
Software; or
|
||||
|
||||
(b) any new file in Source Code Form that contains any Covered
|
||||
Software.
|
||||
|
||||
1.11. "Patent Claims" of a Contributor
|
||||
means any patent claim(s), including without limitation, method,
|
||||
process, and apparatus claims, in any patent Licensable by such
|
||||
Contributor that would be infringed, but for the grant of the
|
||||
License, by the making, using, selling, offering for sale, having
|
||||
made, import, or transfer of either its Contributions or its
|
||||
Contributor Version.
|
||||
|
||||
1.12. "Secondary License"
|
||||
means either the GNU General Public License, Version 2.0, the GNU
|
||||
Lesser General Public License, Version 2.1, the GNU Affero General
|
||||
Public License, Version 3.0, or any later versions of those
|
||||
licenses.
|
||||
|
||||
1.13. "Source Code Form"
|
||||
means the form of the work preferred for making modifications.
|
||||
|
||||
1.14. "You" (or "Your")
|
||||
means an individual or a legal entity exercising rights under this
|
||||
License. For legal entities, "You" includes any entity that
|
||||
controls, is controlled by, or is under common control with You. For
|
||||
purposes of this definition, "control" means (a) the power, direct
|
||||
or indirect, to cause the direction or management of such entity,
|
||||
whether by contract or otherwise, or (b) ownership of more than
|
||||
fifty percent (50%) of the outstanding shares or beneficial
|
||||
ownership of such entity.
|
||||
|
||||
2. License Grants and Conditions
|
||||
--------------------------------
|
||||
|
||||
2.1. Grants
|
||||
|
||||
Each Contributor hereby grants You a world-wide, royalty-free,
|
||||
non-exclusive license:
|
||||
|
||||
(a) under intellectual property rights (other than patent or trademark)
|
||||
Licensable by such Contributor to use, reproduce, make available,
|
||||
modify, display, perform, distribute, and otherwise exploit its
|
||||
Contributions, either on an unmodified basis, with Modifications, or
|
||||
as part of a Larger Work; and
|
||||
|
||||
(b) under Patent Claims of such Contributor to make, use, sell, offer
|
||||
for sale, have made, import, and otherwise transfer either its
|
||||
Contributions or its Contributor Version.
|
||||
|
||||
2.2. Effective Date
|
||||
|
||||
The licenses granted in Section 2.1 with respect to any Contribution
|
||||
become effective for each Contribution on the date the Contributor first
|
||||
distributes such Contribution.
|
||||
|
||||
2.3. Limitations on Grant Scope
|
||||
|
||||
The licenses granted in this Section 2 are the only rights granted under
|
||||
this License. No additional rights or licenses will be implied from the
|
||||
distribution or licensing of Covered Software under this License.
|
||||
Notwithstanding Section 2.1(b) above, no patent license is granted by a
|
||||
Contributor:
|
||||
|
||||
(a) for any code that a Contributor has removed from Covered Software;
|
||||
or
|
||||
|
||||
(b) for infringements caused by: (i) Your and any other third party's
|
||||
modifications of Covered Software, or (ii) the combination of its
|
||||
Contributions with other software (except as part of its Contributor
|
||||
Version); or
|
||||
|
||||
(c) under Patent Claims infringed by Covered Software in the absence of
|
||||
its Contributions.
|
||||
|
||||
This License does not grant any rights in the trademarks, service marks,
|
||||
or logos of any Contributor (except as may be necessary to comply with
|
||||
the notice requirements in Section 3.4).
|
||||
|
||||
2.4. Subsequent Licenses
|
||||
|
||||
No Contributor makes additional grants as a result of Your choice to
|
||||
distribute the Covered Software under a subsequent version of this
|
||||
License (see Section 10.2) or under the terms of a Secondary License (if
|
||||
permitted under the terms of Section 3.3).
|
||||
|
||||
2.5. Representation
|
||||
|
||||
Each Contributor represents that the Contributor believes its
|
||||
Contributions are its original creation(s) or it has sufficient rights
|
||||
to grant the rights to its Contributions conveyed by this License.
|
||||
|
||||
2.6. Fair Use
|
||||
|
||||
This License is not intended to limit any rights You have under
|
||||
applicable copyright doctrines of fair use, fair dealing, or other
|
||||
equivalents.
|
||||
|
||||
2.7. Conditions
|
||||
|
||||
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted
|
||||
in Section 2.1.
|
||||
|
||||
3. Responsibilities
|
||||
-------------------
|
||||
|
||||
3.1. Distribution of Source Form
|
||||
|
||||
All distribution of Covered Software in Source Code Form, including any
|
||||
Modifications that You create or to which You contribute, must be under
|
||||
the terms of this License. You must inform recipients that the Source
|
||||
Code Form of the Covered Software is governed by the terms of this
|
||||
License, and how they can obtain a copy of this License. You may not
|
||||
attempt to alter or restrict the recipients' rights in the Source Code
|
||||
Form.
|
||||
|
||||
3.2. Distribution of Executable Form
|
||||
|
||||
If You distribute Covered Software in Executable Form then:
|
||||
|
||||
(a) such Covered Software must also be made available in Source Code
|
||||
Form, as described in Section 3.1, and You must inform recipients of
|
||||
the Executable Form how they can obtain a copy of such Source Code
|
||||
Form by reasonable means in a timely manner, at a charge no more
|
||||
than the cost of distribution to the recipient; and
|
||||
|
||||
(b) You may distribute such Executable Form under the terms of this
|
||||
License, or sublicense it under different terms, provided that the
|
||||
license for the Executable Form does not attempt to limit or alter
|
||||
the recipients' rights in the Source Code Form under this License.
|
||||
|
||||
3.3. Distribution of a Larger Work
|
||||
|
||||
You may create and distribute a Larger Work under terms of Your choice,
|
||||
provided that You also comply with the requirements of this License for
|
||||
the Covered Software. If the Larger Work is a combination of Covered
|
||||
Software with a work governed by one or more Secondary Licenses, and the
|
||||
Covered Software is not Incompatible With Secondary Licenses, this
|
||||
License permits You to additionally distribute such Covered Software
|
||||
under the terms of such Secondary License(s), so that the recipient of
|
||||
the Larger Work may, at their option, further distribute the Covered
|
||||
Software under the terms of either this License or such Secondary
|
||||
License(s).
|
||||
|
||||
3.4. Notices
|
||||
|
||||
You may not remove or alter the substance of any license notices
|
||||
(including copyright notices, patent notices, disclaimers of warranty,
|
||||
or limitations of liability) contained within the Source Code Form of
|
||||
the Covered Software, except that You may alter any license notices to
|
||||
the extent required to remedy known factual inaccuracies.
|
||||
|
||||
3.5. Application of Additional Terms
|
||||
|
||||
You may choose to offer, and to charge a fee for, warranty, support,
|
||||
indemnity or liability obligations to one or more recipients of Covered
|
||||
Software. However, You may do so only on Your own behalf, and not on
|
||||
behalf of any Contributor. You must make it absolutely clear that any
|
||||
such warranty, support, indemnity, or liability obligation is offered by
|
||||
You alone, and You hereby agree to indemnify every Contributor for any
|
||||
liability incurred by such Contributor as a result of warranty, support,
|
||||
indemnity or liability terms You offer. You may include additional
|
||||
disclaimers of warranty and limitations of liability specific to any
|
||||
jurisdiction.
|
||||
|
||||
4. Inability to Comply Due to Statute or Regulation
|
||||
---------------------------------------------------
|
||||
|
||||
If it is impossible for You to comply with any of the terms of this
|
||||
License with respect to some or all of the Covered Software due to
|
||||
statute, judicial order, or regulation then You must: (a) comply with
|
||||
the terms of this License to the maximum extent possible; and (b)
|
||||
describe the limitations and the code they affect. Such description must
|
||||
be placed in a text file included with all distributions of the Covered
|
||||
Software under this License. Except to the extent prohibited by statute
|
||||
or regulation, such description must be sufficiently detailed for a
|
||||
recipient of ordinary skill to be able to understand it.
|
||||
|
||||
5. Termination
|
||||
--------------
|
||||
|
||||
5.1. The rights granted under this License will terminate automatically
|
||||
if You fail to comply with any of its terms. However, if You become
|
||||
compliant, then the rights granted under this License from a particular
|
||||
Contributor are reinstated (a) provisionally, unless and until such
|
||||
Contributor explicitly and finally terminates Your grants, and (b) on an
|
||||
ongoing basis, if such Contributor fails to notify You of the
|
||||
non-compliance by some reasonable means prior to 60 days after You have
|
||||
come back into compliance. Moreover, Your grants from a particular
|
||||
Contributor are reinstated on an ongoing basis if such Contributor
|
||||
notifies You of the non-compliance by some reasonable means, this is the
|
||||
first time You have received notice of non-compliance with this License
|
||||
from such Contributor, and You become compliant prior to 30 days after
|
||||
Your receipt of the notice.
|
||||
|
||||
5.2. If You initiate litigation against any entity by asserting a patent
|
||||
infringement claim (excluding declaratory judgment actions,
|
||||
counter-claims, and cross-claims) alleging that a Contributor Version
|
||||
directly or indirectly infringes any patent, then the rights granted to
|
||||
You by any and all Contributors for the Covered Software under Section
|
||||
2.1 of this License shall terminate.
|
||||
|
||||
5.3. In the event of termination under Sections 5.1 or 5.2 above, all
|
||||
end user license agreements (excluding distributors and resellers) which
|
||||
have been validly granted by You or Your distributors under this License
|
||||
prior to termination shall survive termination.
|
||||
|
||||
************************************************************************
|
||||
* *
|
||||
* 6. Disclaimer of Warranty *
|
||||
* ------------------------- *
|
||||
* *
|
||||
* Covered Software is provided under this License on an "as is" *
|
||||
* basis, without warranty of any kind, either expressed, implied, or *
|
||||
* statutory, including, without limitation, warranties that the *
|
||||
* Covered Software is free of defects, merchantable, fit for a *
|
||||
* particular purpose or non-infringing. The entire risk as to the *
|
||||
* quality and performance of the Covered Software is with You. *
|
||||
* Should any Covered Software prove defective in any respect, You *
|
||||
* (not any Contributor) assume the cost of any necessary servicing, *
|
||||
* repair, or correction. This disclaimer of warranty constitutes an *
|
||||
* essential part of this License. No use of any Covered Software is *
|
||||
* authorized under this License except under this disclaimer. *
|
||||
* *
|
||||
************************************************************************
|
||||
|
||||
************************************************************************
|
||||
* *
|
||||
* 7. Limitation of Liability *
|
||||
* -------------------------- *
|
||||
* *
|
||||
* Under no circumstances and under no legal theory, whether tort *
|
||||
* (including negligence), contract, or otherwise, shall any *
|
||||
* Contributor, or anyone who distributes Covered Software as *
|
||||
* permitted above, be liable to You for any direct, indirect, *
|
||||
* special, incidental, or consequential damages of any character *
|
||||
* including, without limitation, damages for lost profits, loss of *
|
||||
* goodwill, work stoppage, computer failure or malfunction, or any *
|
||||
* and all other commercial damages or losses, even if such party *
|
||||
* shall have been informed of the possibility of such damages. This *
|
||||
* limitation of liability shall not apply to liability for death or *
|
||||
* personal injury resulting from such party's negligence to the *
|
||||
* extent applicable law prohibits such limitation. Some *
|
||||
* jurisdictions do not allow the exclusion or limitation of *
|
||||
* incidental or consequential damages, so this exclusion and *
|
||||
* limitation may not apply to You. *
|
||||
* *
|
||||
************************************************************************
|
||||
|
||||
8. Litigation
|
||||
-------------
|
||||
|
||||
Any litigation relating to this License may be brought only in the
|
||||
courts of a jurisdiction where the defendant maintains its principal
|
||||
place of business and such litigation shall be governed by laws of that
|
||||
jurisdiction, without reference to its conflict-of-law provisions.
|
||||
Nothing in this Section shall prevent a party's ability to bring
|
||||
cross-claims or counter-claims.
|
||||
|
||||
9. Miscellaneous
|
||||
----------------
|
||||
|
||||
This License represents the complete agreement concerning the subject
|
||||
matter hereof. If any provision of this License is held to be
|
||||
unenforceable, such provision shall be reformed only to the extent
|
||||
necessary to make it enforceable. Any law or regulation which provides
|
||||
that the language of a contract shall be construed against the drafter
|
||||
shall not be used to construe this License against a Contributor.
|
||||
|
||||
10. Versions of the License
|
||||
---------------------------
|
||||
|
||||
10.1. New Versions
|
||||
|
||||
Mozilla Foundation is the license steward. Except as provided in Section
|
||||
10.3, no one other than the license steward has the right to modify or
|
||||
publish new versions of this License. Each version will be given a
|
||||
distinguishing version number.
|
||||
|
||||
10.2. Effect of New Versions
|
||||
|
||||
You may distribute the Covered Software under the terms of the version
|
||||
of the License under which You originally received the Covered Software,
|
||||
or under the terms of any subsequent version published by the license
|
||||
steward.
|
||||
|
||||
10.3. Modified Versions
|
||||
|
||||
If you create software not governed by this License, and you want to
|
||||
create a new license for such software, you may create and use a
|
||||
modified version of this License if you rename the license and remove
|
||||
any references to the name of the license steward (except to note that
|
||||
such modified license differs from this License).
|
||||
|
||||
10.4. Distributing Source Code Form that is Incompatible With Secondary
|
||||
Licenses
|
||||
|
||||
If You choose to distribute Source Code Form that is Incompatible With
|
||||
Secondary Licenses under the terms of this version of the License, the
|
||||
notice described in Exhibit B of this License must be attached.
|
||||
|
||||
Exhibit A - Source Code Form License Notice
|
||||
-------------------------------------------
|
||||
|
||||
This Source Code Form is subject to the terms of the Mozilla Public
|
||||
License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
If it is not possible or desirable to put the notice in a particular
|
||||
file, then You may include the notice in a location (such as a LICENSE
|
||||
file in a relevant directory) where a recipient would be likely to look
|
||||
for such a notice.
|
||||
|
||||
You may add additional accurate notices of copyright ownership.
|
||||
|
||||
Exhibit B - "Incompatible With Secondary Licenses" Notice
|
||||
---------------------------------------------------------
|
||||
|
||||
This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||
defined by the Mozilla Public License, v. 2.0.
|
2
extension/ReadMe.txt
Normal file
|
@ -0,0 +1,2 @@
|
|||
ua-parser.min.js:
|
||||
https://github.com/faisalman/ua-parser-js/releases/tag/0.7.19
|
327
extension/common.js
Normal file
|
@ -0,0 +1,327 @@
|
|||
/* globals UAParser*/
|
||||
|
||||
'use strict';
|
||||
|
||||
var cache = {};
|
||||
var tabs = {};
|
||||
chrome.tabs.onRemoved.addListener(id => delete cache[id]);
|
||||
chrome.tabs.onCreated.addListener(tab => tabs[tab.id] = tab.windowId);
|
||||
|
||||
var prefs = {
|
||||
ua: '',
|
||||
blacklist: [],
|
||||
whitelist: [],
|
||||
custom: {},
|
||||
mode: 'blacklist',
|
||||
color: '#ffa643',
|
||||
cache: true,
|
||||
exactMatch: false
|
||||
};
|
||||
chrome.storage.local.get(prefs, ps => {
|
||||
Object.assign(prefs, ps);
|
||||
chrome.tabs.query({}, ts => {
|
||||
ts.forEach(t => tabs[t.id] = t.windowId);
|
||||
ua.update();
|
||||
});
|
||||
if (chrome.browserAction.setBadgeBackgroundColor) { // FF for Android
|
||||
chrome.browserAction.setBadgeBackgroundColor({
|
||||
color: prefs.color
|
||||
});
|
||||
}
|
||||
// context menu
|
||||
chrome.contextMenus.create({
|
||||
id: 'blacklist',
|
||||
title: 'Switch to "black-list" mode',
|
||||
contexts: ['browser_action'],
|
||||
type: 'radio',
|
||||
checked: prefs.mode === 'blacklist'
|
||||
});
|
||||
chrome.contextMenus.create({
|
||||
id: 'whitelist',
|
||||
title: 'Switch to "white-list" mode',
|
||||
contexts: ['browser_action'],
|
||||
type: 'radio',
|
||||
checked: prefs.mode === 'whitelist'
|
||||
});
|
||||
chrome.contextMenus.create({
|
||||
id: 'custom',
|
||||
title: 'Switch to "custom" mode',
|
||||
contexts: ['browser_action'],
|
||||
type: 'radio',
|
||||
checked: prefs.mode === 'custom'
|
||||
});
|
||||
});
|
||||
chrome.storage.onChanged.addListener(ps => {
|
||||
Object.keys(ps).forEach(key => prefs[key] = ps[key].newValue);
|
||||
if (ps.ua || ps.mode) {
|
||||
ua.update();
|
||||
}
|
||||
});
|
||||
|
||||
var ua = {
|
||||
_obj: {
|
||||
'global': {}
|
||||
},
|
||||
diff(tabId) { // returns true if there is per window object
|
||||
const windowId = tabs[tabId];
|
||||
return windowId in this._obj;
|
||||
},
|
||||
get windows() {
|
||||
return Object.keys(this._obj).filter(id => id !== 'global').map(s => Number(s));
|
||||
},
|
||||
parse: s => {
|
||||
const o = {};
|
||||
o.userAgent = s;
|
||||
o.appVersion = s
|
||||
.replace(/^Mozilla\//, '')
|
||||
.replace(/^Opera\//, '');
|
||||
const p = new UAParser(s);
|
||||
o.platform = p.getOS().name || '';
|
||||
o.vendor = p.getDevice().vendor || '';
|
||||
|
||||
return o;
|
||||
},
|
||||
object(tabId, windowId) {
|
||||
windowId = windowId || (tabId ? tabs[tabId] : 'global');
|
||||
return this._obj[windowId] || this._obj.global;
|
||||
},
|
||||
string(str, windowId) {
|
||||
if (str) {
|
||||
this._obj[windowId] = this.parse(str);
|
||||
}
|
||||
else {
|
||||
this._obj[windowId] = {};
|
||||
}
|
||||
},
|
||||
toolbar: ({windowId, tabId, str = ua.object(tabId, windowId).userAgent}) => {
|
||||
const icon = {
|
||||
path: {
|
||||
16: 'data/icons/' + (str ? 'active/' : '') + '16.png',
|
||||
32: 'data/icons/' + (str ? 'active/' : '') + '32.png',
|
||||
48: 'data/icons/' + (str ? 'active/' : '') + '48.png',
|
||||
64: 'data/icons/' + (str ? 'active/' : '') + '64.png'
|
||||
}
|
||||
};
|
||||
const custom = 'Mapped from user\'s JSON object if found, otherwise uses "' + (str || navigator.userAgent) + '"';
|
||||
const title = {
|
||||
title: `UserAgent Switcher (${str ? 'enabled' : 'disabled'})
|
||||
|
||||
User-Agent String: ${prefs.mode === 'custom' ? custom : str || navigator.userAgent}`
|
||||
};
|
||||
if (windowId) {
|
||||
chrome.tabs.query({
|
||||
windowId
|
||||
}, tabs => tabs.forEach(tab => {
|
||||
const tabId = tab.id;
|
||||
chrome.browserAction.setTitle(Object.assign({tabId}, title));
|
||||
chrome.browserAction.setBadgeText({
|
||||
tabId,
|
||||
text: ua.object(null, windowId).platform.substr(0, 3)
|
||||
});
|
||||
}));
|
||||
}
|
||||
else if (tabId) {
|
||||
chrome.browserAction.setTitle(Object.assign({tabId}, title));
|
||||
chrome.browserAction.setBadgeText({
|
||||
tabId,
|
||||
text: ua.object(tabId).platform.substr(0, 3)
|
||||
});
|
||||
}
|
||||
else {
|
||||
chrome.browserAction.setIcon(icon);
|
||||
chrome.browserAction.setTitle(title);
|
||||
}
|
||||
},
|
||||
update(str = prefs.ua, windowId = 'global') {
|
||||
chrome.webRequest.onBeforeSendHeaders.removeListener(onBeforeSendHeaders);
|
||||
chrome.webNavigation.onCommitted.removeListener(onCommitted);
|
||||
|
||||
if (str || prefs.mode === 'custom' || this.windows.length) {
|
||||
ua.string(str, windowId);
|
||||
chrome.webRequest.onBeforeSendHeaders.addListener(onBeforeSendHeaders, {
|
||||
'urls': ['*://*/*']
|
||||
}, ['blocking', 'requestHeaders']);
|
||||
chrome.webNavigation.onCommitted.addListener(onCommitted);
|
||||
}
|
||||
if (windowId === 'global') {
|
||||
this.toolbar({str});
|
||||
}
|
||||
// update per window
|
||||
else {
|
||||
this.windows.forEach(windowId => this.toolbar({windowId}));
|
||||
}
|
||||
}
|
||||
};
|
||||
// make sure to clean on window removal
|
||||
if (chrome.windows) { // FF on Android
|
||||
chrome.windows.onRemoved.addListener(windowId => delete ua._obj[windowId]);
|
||||
}
|
||||
|
||||
function hostname(url) {
|
||||
const s = url.indexOf('//') + 2;
|
||||
if (s > 1) {
|
||||
let o = url.indexOf('/', s);
|
||||
if (o > 0) {
|
||||
return url.substring(s, o);
|
||||
}
|
||||
else {
|
||||
o = url.indexOf('?', s);
|
||||
if (o > 0) {
|
||||
return url.substring(s, o);
|
||||
}
|
||||
else {
|
||||
return url.substring(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
return url;
|
||||
}
|
||||
}
|
||||
// returns true, false or an object; true: ignore, false: use from ua object.
|
||||
function match({url, tabId}) {
|
||||
if (prefs.mode === 'blacklist') {
|
||||
if (prefs.blacklist.length) {
|
||||
const h = hostname(url);
|
||||
return prefs.blacklist.some(s => () => {
|
||||
if (s === h) {
|
||||
return true;
|
||||
}
|
||||
else if (prefs.exactMatch === false) {
|
||||
return s.endsWith(h) || h.endsWith(s);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
else if (prefs.mode === 'whitelist') {
|
||||
if (prefs.whitelist.length) {
|
||||
const h = hostname(url);
|
||||
return prefs.whitelist.some(s => {
|
||||
if (s === h) {
|
||||
return true;
|
||||
}
|
||||
else if (prefs.exactMatch === false) {
|
||||
return s.endsWith(h) || h.endsWith(s);
|
||||
}
|
||||
}) === false;
|
||||
}
|
||||
else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
const h = hostname(url);
|
||||
const key = Object.keys(prefs.custom).filter(s => {
|
||||
if (s === h) {
|
||||
return true;
|
||||
}
|
||||
else if (prefs.exactMatch === false) {
|
||||
return s.endsWith(h) || h.endsWith(s);
|
||||
}
|
||||
}).shift();
|
||||
let s = prefs.custom[key] || prefs.custom['*'];
|
||||
// if s is an array select a random string
|
||||
if (Array.isArray(s)) {
|
||||
s = s[Math.floor(Math.random() * s.length)];
|
||||
}
|
||||
if (s) {
|
||||
return ua.parse(s);
|
||||
}
|
||||
else {
|
||||
return !ua.object(tabId).userAgent;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var onBeforeSendHeaders = ({tabId, url, requestHeaders, type}) => {
|
||||
if (type === 'main_frame' || prefs.cache === false) {
|
||||
cache[tabId] = match({url, tabId});
|
||||
}
|
||||
if (cache[tabId] === true) {
|
||||
return;
|
||||
}
|
||||
const str = (cache[tabId] || ua.object(tabId)).userAgent;
|
||||
if (str) {
|
||||
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 = str === 'empty' ? '' : str;
|
||||
return {
|
||||
requestHeaders
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var onCommitted = ({frameId, url, tabId}) => {
|
||||
if (url && (url.startsWith('http') || url.startsWith('ftp')) || url === 'about:blank') {
|
||||
if (cache[tabId] === true) {
|
||||
return;
|
||||
}
|
||||
const o = cache[tabId] || ua.object(tabId);
|
||||
if (o.userAgent) {
|
||||
let {userAgent, appVersion, platform, vendor} = o;
|
||||
if (o.userAgent === 'empty') {
|
||||
userAgent = appVersion = platform = vendor = '';
|
||||
}
|
||||
chrome.tabs.executeScript(tabId, {
|
||||
runAt: 'document_start',
|
||||
frameId,
|
||||
code: `{
|
||||
const script = document.createElement('script');
|
||||
script.textContent = \`{
|
||||
navigator.__defineGetter__('userAgent', () => '${userAgent}');
|
||||
navigator.__defineGetter__('appVersion', () => '${appVersion}');
|
||||
navigator.__defineGetter__('platform', () => '${platform}');
|
||||
navigator.__defineGetter__('vendor', () => '${vendor}');
|
||||
}\`;
|
||||
document.documentElement.appendChild(script);
|
||||
script.remove();
|
||||
}`
|
||||
}, () => chrome.runtime.lastError);
|
||||
}
|
||||
}
|
||||
// change the toolbar icon if there is a per window UA setting
|
||||
if (frameId === 0 && ua.diff(tabId)) {
|
||||
ua.toolbar({tabId});
|
||||
}
|
||||
};
|
||||
// context menu
|
||||
chrome.contextMenus.onClicked.addListener(info => chrome.storage.local.set({
|
||||
mode: info.menuItemId
|
||||
}));
|
||||
|
||||
// FAQs & Feedback
|
||||
chrome.storage.local.get({
|
||||
'version': null,
|
||||
'faqs': false,
|
||||
'last-update': 0
|
||||
}, prefs => {
|
||||
const version = chrome.runtime.getManifest().version;
|
||||
|
||||
if (prefs.version ? (prefs.faqs && prefs.version !== version) : true) {
|
||||
const now = Date.now();
|
||||
const doUpdate = (now - prefs['last-update']) / 1000 / 60 / 60 / 24 > 45;
|
||||
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 45 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(
|
||||
chrome.runtime.getManifest().homepage_url + '?rd=feedback&name=' + name + '&version=' + version
|
||||
);
|
||||
}
|
BIN
extension/data/icons/128.png
Normal file
After Width: | Height: | Size: 7.2 KiB |
BIN
extension/data/icons/16.png
Normal file
After Width: | Height: | Size: 1 KiB |
BIN
extension/data/icons/18.png
Normal file
After Width: | Height: | Size: 1.1 KiB |
BIN
extension/data/icons/19.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
BIN
extension/data/icons/256.png
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
extension/data/icons/32.png
Normal file
After Width: | Height: | Size: 2 KiB |
BIN
extension/data/icons/36.png
Normal file
After Width: | Height: | Size: 2.3 KiB |
BIN
extension/data/icons/38.png
Normal file
After Width: | Height: | Size: 2.3 KiB |
BIN
extension/data/icons/48.png
Normal file
After Width: | Height: | Size: 3.1 KiB |
BIN
extension/data/icons/512.png
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
extension/data/icons/64.png
Normal file
After Width: | Height: | Size: 3.9 KiB |
BIN
extension/data/icons/active/128.png
Normal file
After Width: | Height: | Size: 8.2 KiB |
BIN
extension/data/icons/active/16.png
Normal file
After Width: | Height: | Size: 1.1 KiB |
BIN
extension/data/icons/active/18.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
extension/data/icons/active/19.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
extension/data/icons/active/256.png
Normal file
After Width: | Height: | Size: 17 KiB |
BIN
extension/data/icons/active/32.png
Normal file
After Width: | Height: | Size: 2.2 KiB |
BIN
extension/data/icons/active/36.png
Normal file
After Width: | Height: | Size: 2.5 KiB |
BIN
extension/data/icons/active/38.png
Normal file
After Width: | Height: | Size: 2.7 KiB |
BIN
extension/data/icons/active/48.png
Normal file
After Width: | Height: | Size: 3.5 KiB |
BIN
extension/data/icons/active/512.png
Normal file
After Width: | Height: | Size: 15 KiB |
BIN
extension/data/icons/active/64.png
Normal file
After Width: | Height: | Size: 4.5 KiB |
47
extension/data/inject.js
Normal file
|
@ -0,0 +1,47 @@
|
|||
'use strict';
|
||||
|
||||
// iframe.contentWindow
|
||||
if (
|
||||
window !== top &&
|
||||
location.href === 'about:blank'
|
||||
) {
|
||||
try {
|
||||
top.document; // are we on the same frame?
|
||||
|
||||
const script = document.createElement('script');
|
||||
script.textContent = `{
|
||||
const nav = top.navigator;
|
||||
|
||||
navigator.__defineGetter__('userAgent', () => nav.userAgent);
|
||||
navigator.__defineGetter__('appVersion', () => nav.appVersion);
|
||||
navigator.__defineGetter__('platform', () => nav.platform);
|
||||
navigator.__defineGetter__('vendor', () => nav.vendor);
|
||||
|
||||
document.documentElement.dataset.fgdvcre = true;
|
||||
}`;
|
||||
document.documentElement.appendChild(script);
|
||||
script.remove();
|
||||
// make sure the script is injected
|
||||
if (document.documentElement.dataset.fgdvcre !== 'true') {
|
||||
document.documentElement.dataset.fgdvcre = true;
|
||||
const script = document.createElement('script');
|
||||
Object.assign(script, {
|
||||
textContent: `
|
||||
[...document.querySelectorAll('iframe[sandbox]')]
|
||||
.filter(i => i.contentDocument.documentElement.dataset.fgdvcre === 'true')
|
||||
.forEach(i => {
|
||||
const nav = i.contentWindow.navigator;
|
||||
nav.__defineGetter__('userAgent', () => navigator.userAgent);
|
||||
nav.__defineGetter__('appVersion', () => navigator.appVersion);
|
||||
nav.__defineGetter__('platform', () => navigator.platform);
|
||||
nav.__defineGetter__('vendor', () => navigator.vendor);
|
||||
});
|
||||
`
|
||||
});
|
||||
top.document.documentElement.appendChild(script);
|
||||
script.remove();
|
||||
}
|
||||
delete document.documentElement.dataset.fgdvcre;
|
||||
}
|
||||
catch (e) {}
|
||||
}
|
64
extension/data/options/index.html
Normal file
|
@ -0,0 +1,64 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>My Test Extension Options</title>
|
||||
<style>
|
||||
body {
|
||||
min-width: 600px;
|
||||
}
|
||||
textarea {
|
||||
width: 100%;
|
||||
}
|
||||
.h {
|
||||
text-decoration: underline;
|
||||
text-decoration-style: dashed;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<table width=100%>
|
||||
<tr>
|
||||
<td>
|
||||
<label><input type="radio" name="mode" value="blacklist" id="mode-blacklist"> <span class="h">Black-list mode</span>: Apply the custom user-agent string to all tabs except the tabs with the following top-level hostnames (comma-separated list of hostnames). Note that even if a window-based user-agent string is set from the toolbar popup, your browser's default user-agent string is used.</label>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><textarea id="blacklist" rows="3" placeholder="e.g.: www.google.com, www.bing.com"></textarea></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<label><input type="radio" name="mode" value="whitelist" id="mode-whitelist"> <span class="h">White-list mode</span>: Only apply the custom user-agent string to the tabs with following top-level hostnames. Note that if a window-based user-agent string is set from the toolbar popup, this user-agent will overwrite the global one.</label>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<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"> <span class="h">Custom mode</span>: Try to resolve the user-agent string from a JSON object; otherwise either use the default user-agent string or use the one that user is set from the popup. Use "*" as the hostname to match all domains. You can randomly select from multiple user-agent strings by providing an array instead of a string.</label> <a href="#" id="sample">Insert</a> a sample JSON object.
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><textarea id="custom" rows="5" wrap="off"></textarea></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><label><input type="checkbox" id="cache"> Use caching to improve performance (recommended value is true). Uncheck this option only if you are using the custom mode and also you need the user-agent string to be altered from the provided list on every single request.</label></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><label><input type="checkbox" id="exactMatch"> Use exact matching (if checked, you will need to insert all sub-domains in the white-list and black-list modes to be considered. If unchecked, all the sub-domains are passing the matching condition (e.g: www.google.com passes the matching if google.com is in the list))</label></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="reset">Reset</button>
|
||||
<button id="save">Save</button>
|
||||
<span id="status"></span>
|
||||
</p>
|
||||
|
||||
<script src="index.js"></script>
|
||||
</body>
|
||||
</html>
|
95
extension/data/options/index.js
Normal file
|
@ -0,0 +1,95 @@
|
|||
'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);
|
||||
}
|
||||
|
||||
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,
|
||||
mode: document.querySelector('[name="mode"]:checked').value
|
||||
}, () => {
|
||||
restore();
|
||||
notify('Options saved.');
|
||||
});
|
||||
}
|
||||
|
||||
function restore() {
|
||||
chrome.storage.local.get({
|
||||
exactMatch: false,
|
||||
faqs: true,
|
||||
cache: true,
|
||||
mode: 'blacklist',
|
||||
whitelist: [],
|
||||
blacklist: [],
|
||||
custom: {}
|
||||
}, 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.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': '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('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();
|
||||
});
|
||||
}
|
||||
});
|
1
extension/data/popup/browsers
Symbolic link
|
@ -0,0 +1 @@
|
|||
../../../node/browsers/
|
194
extension/data/popup/index.css
Normal file
|
@ -0,0 +1,194 @@
|
|||
[hbox] {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
}
|
||||
[vbox] {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
[flex="1"] {
|
||||
flex: 1;
|
||||
}
|
||||
[pack=center] {
|
||||
justify-content: center;
|
||||
}
|
||||
[align=center] {
|
||||
align-items: center;
|
||||
}
|
||||
[pack=end] {
|
||||
justify-content: flex-end;
|
||||
}
|
||||
[align=end] {
|
||||
align-items: flex-end;
|
||||
}
|
||||
|
||||
body {
|
||||
background-color: #fff;
|
||||
font-family: "Helvetica Neue",Helvetica,sans-serif;
|
||||
font-size: 13px;
|
||||
width: 600px;
|
||||
}
|
||||
table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
fieldset {
|
||||
border: solid 1px #ccc;
|
||||
}
|
||||
input[type=search],
|
||||
input[type=text] {
|
||||
width: 100%;
|
||||
margin-right: 2px;
|
||||
text-indent: 5px;
|
||||
padding-right: 5px;
|
||||
}
|
||||
input {
|
||||
outline: none;
|
||||
background-color: #fff;
|
||||
color: #000;
|
||||
border: solid 1px #e7e7e7;
|
||||
box-sizing: border-box;
|
||||
height: 24px;
|
||||
border-radius: 0;
|
||||
font-size: 11px;
|
||||
}
|
||||
input[type=button] {
|
||||
cursor: pointer;
|
||||
min-width: 100px;
|
||||
}
|
||||
input[type=button]:active {
|
||||
opacity: 0.5;
|
||||
}
|
||||
input[type=button]:disabled {
|
||||
opacity: 0.2;
|
||||
cursor: default;
|
||||
}
|
||||
select {
|
||||
-webkit-appearance: none;
|
||||
-moz-appearance: none;
|
||||
border: none;
|
||||
user-select: none;
|
||||
outline: none;
|
||||
color: #000;
|
||||
background: rgba(255,255,255,.5) url(list.svg) no-repeat center right 4px;
|
||||
background-size: 8px;
|
||||
font-size: 13px;
|
||||
border-radius: 0;
|
||||
padding: 2px 16px 2px 4px;
|
||||
}
|
||||
#list {
|
||||
overflow: auto;
|
||||
height: 300px;
|
||||
margin-bottom: 10px;
|
||||
color: #000;
|
||||
background-color: #fdfafa;
|
||||
background-position: top 120px center;
|
||||
background-repeat: no-repeat;
|
||||
}
|
||||
#list[data-loading=true] {
|
||||
background-image: url(loading.gif);
|
||||
background-size: 64px;
|
||||
}
|
||||
#list table {
|
||||
table-layout: fixed;
|
||||
}
|
||||
#list th {
|
||||
height: 30px;
|
||||
color: #000;
|
||||
background-color: #e7e7e7;
|
||||
white-space: nowrap;
|
||||
padding-left: 10px;
|
||||
}
|
||||
#list tr {
|
||||
cursor: pointer;
|
||||
}
|
||||
#list tr[data-matched=false] {
|
||||
opacity: 0.5;
|
||||
}
|
||||
#list tbody {
|
||||
position: relative;
|
||||
}
|
||||
#list tbody tr:nth-child(odd) {
|
||||
color: #000;
|
||||
background-color: #fff;
|
||||
}
|
||||
#list tbody tr:nth-child(even) {
|
||||
color: #000;
|
||||
background-color: #f5f5f5;
|
||||
}
|
||||
#list[data-loading=false] tbody:empty::before {
|
||||
content: 'no user-agent string for this query!';
|
||||
display: block;
|
||||
position: absolute;
|
||||
top: 50px;
|
||||
left: 10px;
|
||||
}
|
||||
#list td:nth-child(1) {
|
||||
text-align: center;
|
||||
}
|
||||
#list td {
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
padding: 3px 5px;
|
||||
}
|
||||
|
||||
#tools input {
|
||||
width: 100px;
|
||||
margin: 3px 0 0 5px;
|
||||
}
|
||||
|
||||
#filter td:first-child {
|
||||
width: 100px;
|
||||
}
|
||||
#filter th:last-of-type {
|
||||
text-align: right;
|
||||
padding-right: 10px;
|
||||
}
|
||||
|
||||
#agent {
|
||||
padding: 10px 0;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
#info {
|
||||
padding: 0 5px;
|
||||
}
|
||||
#info:empty {
|
||||
display: none;
|
||||
}
|
||||
|
||||
[data-cmd="refresh"] {
|
||||
margin-left: 2px;
|
||||
margin-right: 2px;
|
||||
}
|
||||
[data-cmd="window"],
|
||||
[data-cmd="apply"] {
|
||||
color: #fff;
|
||||
background-color: #3c923c;
|
||||
border: solid 1px #327932;
|
||||
margin-right: 2px;
|
||||
margin-left: 2px;
|
||||
}
|
||||
|
||||
[data-cmd="reset"] {
|
||||
color: #fff;
|
||||
background-color: #eea345;
|
||||
border: solid 1px #ec9730;
|
||||
}
|
||||
|
||||
[data-cmd="reload"],
|
||||
[data-cmd="options"],
|
||||
[data-cmd="refresh"] {
|
||||
color: #000;
|
||||
background-color: #f5f5f5;
|
||||
}
|
||||
|
||||
#explore:not([data-loaded="true"]) {
|
||||
height: 16px;
|
||||
}
|
||||
|
||||
body[data-android="true"] [data-cmd="reload"],
|
||||
body[data-android="true"] [data-cmd="window"] {
|
||||
display: none;
|
||||
}
|
83
extension/data/popup/index.html
Normal file
|
@ -0,0 +1,83 @@
|
|||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
|
||||
<link rel="stylesheet" href="index.css">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
</head>
|
||||
<body>
|
||||
<div id="list" data-loading=true>
|
||||
<table>
|
||||
<colgroup>
|
||||
<col width="40">
|
||||
<col width="150">
|
||||
<col width="100">
|
||||
<col>
|
||||
</colgroup>
|
||||
<thead id="filter">
|
||||
<tr>
|
||||
<th colspan="3">
|
||||
<select id="browser">
|
||||
<optgroup label="Populars">
|
||||
<option value="Internet Explorer">Internet Explorer</option>
|
||||
<option value="Safari">Safari</option>
|
||||
<option value="Chrome">Chrome</option>
|
||||
<option value="Firefox">Firefox</option>
|
||||
<option value="Opera">Opera</option>
|
||||
<option value="Edge">Edge</option>
|
||||
</optgroup>
|
||||
<optgroup label="Others"></optgroup>
|
||||
</select>
|
||||
<select id="os">
|
||||
<optgroup label="Populars">
|
||||
<option value="Windows">Windows</option>
|
||||
<option value="Mac OS">Mac OS</option>
|
||||
<option value="Linux">Linux</option>
|
||||
<option value="Chromium OS">Chromium OS</option>
|
||||
<option value="Ubuntu">Ubuntu</option>
|
||||
<option value="Debian">Debian</option>
|
||||
<option value="Android">Android</option>
|
||||
<option value="iOS">iOS</option>
|
||||
</optgroup>
|
||||
<optgroup label="Others"></optgroup>
|
||||
</select>
|
||||
</th>
|
||||
<th>
|
||||
User-Agent
|
||||
<select id="sort">
|
||||
<option value="true">descending</option>
|
||||
<option value="false">ascending</option>
|
||||
</select>
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<template>
|
||||
<tr>
|
||||
<td><input type="radio" name="select"></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
</tr>
|
||||
</template>
|
||||
<tbody></tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div hbox>
|
||||
<input type="search" id="custom" placeholder="Filter items">
|
||||
<input type="button" value="Options" title="Open options page" style="margin-left: 2px;" data-cmd="options">
|
||||
<input type="button" value="Refresh Tab" title="Refresh the current page" data-cmd="refresh">
|
||||
<input type="button" value="Restart" title="Click to reload the extension. This will cause all the window-based user-agent strings to be cleared" data-cmd="reload">
|
||||
</div>
|
||||
<div hbox id="agent" pack="center" align="center">
|
||||
<span id="info">User-Agent String:</span>
|
||||
<span flex="1"></span>
|
||||
<input type="button" value="Apply" title="Set this string as the browser's User-Agent string" data-cmd="apply">
|
||||
<input type="button" value="Window" title="Set this string as this window's User-Agent string" data-cmd="window">
|
||||
<input type="button" value="Reset" title="Reset User-Agent string to the default one. This will not reset window-based UA strings. To reset them, use the 'Restart' button" style="margin-left: 2px;" data-cmd="reset">
|
||||
</div>
|
||||
<input id="ua" type="text" placeholder="Your preferred user-agent string" title="To set blank user-agent string, use 'empty' keyword">
|
||||
<div id="explore"></div>
|
||||
<script src="index.js"></script>
|
||||
<script async src="matched.js"></script>
|
||||
</body>
|
||||
</html>
|
195
extension/data/popup/index.js
Normal file
|
@ -0,0 +1,195 @@
|
|||
'use strict';
|
||||
|
||||
document.body.dataset.android = navigator.userAgent.indexOf('Android') !== -1;
|
||||
|
||||
var map = {};
|
||||
|
||||
function sort(arr) {
|
||||
function sort(a = '', b = '') {
|
||||
const pa = a.split('.');
|
||||
const pb = b.split('.');
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const na = Number(pa[i]);
|
||||
const nb = Number(pb[i]);
|
||||
if (na > nb) {
|
||||
return 1;
|
||||
}
|
||||
if (nb > na) {
|
||||
return -1;
|
||||
}
|
||||
if (!isNaN(na) && isNaN(nb)) {
|
||||
return 1;
|
||||
}
|
||||
if (isNaN(na) && !isNaN(nb)) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
const list = arr.sort((a, b) => sort(a.browser.version, b.browser.version));
|
||||
if (document.getElementById('sort').value === 'true') {
|
||||
return list.reverse();
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
function update() {
|
||||
const browser = document.getElementById('browser').value;
|
||||
const os = document.getElementById('os').value;
|
||||
|
||||
const t = document.querySelector('template');
|
||||
const parent = document.getElementById('list');
|
||||
const tbody = parent.querySelector('tbody');
|
||||
tbody.textContent = '';
|
||||
|
||||
parent.dataset.loading = true;
|
||||
fetch('browsers/' + browser + '-' + os.replace(/\//g, '-') + '.json').then(r => r.json()).catch(() => []).then(list => {
|
||||
if (list) {
|
||||
const fragment = document.createDocumentFragment();
|
||||
for (const o of sort(list)) {
|
||||
const clone = document.importNode(t.content, true);
|
||||
const second = clone.querySelector('td:nth-child(2)');
|
||||
second.title = second.textContent = o.browser.name + ' ' + (o.browser.version || ' ');
|
||||
const third = clone.querySelector('td:nth-child(3)');
|
||||
third.title = third.textContent = o.os.name + ' ' + (o.os.version || ' ');
|
||||
const forth = clone.querySelector('td:nth-child(4)');
|
||||
forth.title = forth.textContent = o.ua;
|
||||
fragment.appendChild(clone);
|
||||
}
|
||||
tbody.appendChild(fragment);
|
||||
document.getElementById('custom').placeholder = `Filter among ${list.length} "User-Agent" strings`;
|
||||
[...document.getElementById('os').querySelectorAll('option')].forEach(option => {
|
||||
option.disabled = map[browser][option.value] !== true;
|
||||
});
|
||||
}
|
||||
else {
|
||||
throw Error('OS is not found');
|
||||
}
|
||||
}).finally(() => {
|
||||
parent.dataset.loading = false;
|
||||
});
|
||||
}
|
||||
|
||||
document.addEventListener('change', ({target}) => {
|
||||
if (target.closest('#filter')) {
|
||||
localStorage.setItem(target.id, target.value);
|
||||
update();
|
||||
}
|
||||
if (target.type === 'radio') {
|
||||
document.getElementById('ua').value = target.closest('tr').querySelector('td:nth-child(4)').textContent;
|
||||
document.getElementById('ua').dispatchEvent(new Event('input'));
|
||||
}
|
||||
});
|
||||
document.addEventListener('DOMContentLoaded', () => fetch('./map.json').then(r => r.json())
|
||||
.then(o => {
|
||||
Object.assign(map, o);
|
||||
const OSs = new Set();
|
||||
const f1 = document.createDocumentFragment();
|
||||
const f2 = document.createDocumentFragment();
|
||||
Object.keys(map).sort().forEach(s => {
|
||||
Object.keys(map[s]).forEach(s => OSs.add(s));
|
||||
const option = document.createElement('option');
|
||||
option.value = option.textContent = s;
|
||||
f1.appendChild(option);
|
||||
});
|
||||
document.querySelector('#browser optgroup:last-of-type').appendChild(f1);
|
||||
document.getElementById('browser').value = localStorage.getItem('browser') || 'Chrome';
|
||||
for (const os of Array.from(OSs).sort()) {
|
||||
const option = document.createElement('option');
|
||||
option.value = option.textContent = os;
|
||||
f2.appendChild(option);
|
||||
}
|
||||
document.querySelector('#os optgroup:last-of-type').appendChild(f2);
|
||||
document.getElementById('os').value = localStorage.getItem('os') || 'Windows';
|
||||
update();
|
||||
}));
|
||||
|
||||
document.getElementById('list').addEventListener('click', ({target}) => {
|
||||
const tr = target.closest('tr');
|
||||
if (tr) {
|
||||
const input = tr.querySelector('input');
|
||||
if (input && input !== target) {
|
||||
input.checked = !input.checked;
|
||||
input.dispatchEvent(new Event('change', {
|
||||
bubbles: true
|
||||
}));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
document.getElementById('custom').addEventListener('keyup', ({target}) => {
|
||||
const value = target.value;
|
||||
[...document.querySelectorAll('#list tr')]
|
||||
.forEach(tr => tr.dataset.matched = tr.textContent.toLowerCase().indexOf(value.toLowerCase()) !== -1);
|
||||
});
|
||||
|
||||
chrome.storage.local.get({
|
||||
ua: ''
|
||||
}, prefs => document.getElementById('ua').value = prefs.ua || navigator.userAgent);
|
||||
chrome.storage.onChanged.addListener(prefs => {
|
||||
if (prefs.ua) {
|
||||
document.getElementById('ua').value = prefs.ua.newValue || navigator.userAgent;
|
||||
document.getElementById('ua').dispatchEvent(new Event('input'));
|
||||
}
|
||||
});
|
||||
|
||||
function msg(msg) {
|
||||
const info = document.getElementById('info');
|
||||
info.textContent = msg;
|
||||
window.setTimeout(() => info.textContent = 'User-Agent String:', 2000);
|
||||
}
|
||||
|
||||
// commands
|
||||
document.addEventListener('click', ({target}) => {
|
||||
const cmd = target.dataset.cmd;
|
||||
if (cmd) {
|
||||
if (cmd === 'apply') {
|
||||
const value = document.getElementById('ua').value;
|
||||
if (value === navigator.userAgent) {
|
||||
msg('Default UA, press the reset button instead');
|
||||
}
|
||||
else {
|
||||
msg('user-agent is set');
|
||||
}
|
||||
chrome.storage.local.set({
|
||||
ua: value === navigator.userAgent ? '' : value
|
||||
});
|
||||
}
|
||||
else if (cmd === 'window') {
|
||||
const value = document.getElementById('ua').value;
|
||||
chrome.tabs.query({
|
||||
active: true,
|
||||
currentWindow: true
|
||||
}, ([tab]) => chrome.runtime.getBackgroundPage(bg => bg.ua.update(value, tab.windowId)));
|
||||
}
|
||||
else if (cmd === 'reset') {
|
||||
const input = document.querySelector('#list :checked');
|
||||
if (input) {
|
||||
input.checked = false;
|
||||
}
|
||||
chrome.storage.local.set({
|
||||
ua: ''
|
||||
});
|
||||
msg('reset to default');
|
||||
}
|
||||
else if (cmd === 'refresh') {
|
||||
chrome.tabs.query({
|
||||
active: true,
|
||||
currentWindow: true
|
||||
}, ([tab]) => chrome.tabs.reload(tab.id, {
|
||||
bypassCache: true
|
||||
}));
|
||||
}
|
||||
else if (cmd === 'options') {
|
||||
chrome.runtime.openOptionsPage();
|
||||
}
|
||||
else if (cmd === 'reload') {
|
||||
chrome.runtime.reload();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
document.getElementById('ua').addEventListener('input', e => {
|
||||
document.querySelector('[data-cmd=apply]').disabled = e.target.value === '';
|
||||
document.querySelector('[data-cmd=window]').disabled = e.target.value === '';
|
||||
});
|
1
extension/data/popup/list.svg
Normal file
|
@ -0,0 +1 @@
|
|||
<?xml version="1.0" ?><!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'><svg enable-background="new 0 0 48 48" height="48px" id="Layer_3" version="1.1" viewBox="0 0 48 48" width="48px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><polygon fill="#241F20" points="0,12.438 48,12.438 24,35.562 "/></svg>
|
After Width: | Height: | Size: 404 B |
BIN
extension/data/popup/loading.gif
Normal file
After Width: | Height: | Size: 162 KiB |
1
extension/data/popup/map.json
Symbolic link
|
@ -0,0 +1 @@
|
|||
../../../node/map.json
|
169
extension/data/popup/matched.js
Normal file
|
@ -0,0 +1,169 @@
|
|||
'use strict';
|
||||
|
||||
{
|
||||
const shuffle = array => {
|
||||
for (let i = array.length - 1; i > 0; i -= 1) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
[array[i], array[j]] = [array[j], array[i]];
|
||||
}
|
||||
|
||||
return array;
|
||||
};
|
||||
|
||||
const root = document.getElementById('explore');
|
||||
|
||||
const INC = Number(root.dataset.inc || 100);
|
||||
const count = Number(localStorage.getItem('explore-count') || INC - 5);
|
||||
|
||||
const style = document.createElement('style');
|
||||
style.textContent = `
|
||||
#explore {
|
||||
background-color: #fff;
|
||||
position: relative;
|
||||
color: #969696;
|
||||
user-select: none;
|
||||
}
|
||||
#explore[data-loaded=true] {
|
||||
margin: 4px;
|
||||
padding: 5px;
|
||||
box-shadow: 0 0 1px #ccc;
|
||||
border: solid 1px #ccc;
|
||||
}
|
||||
#explore .close {
|
||||
position: absolute;
|
||||
right: 6px;
|
||||
top: 4px;
|
||||
cursor: pointer;
|
||||
}
|
||||
#explore>table {
|
||||
margin-top: 10px;
|
||||
table-layout: fixed;
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
#explore a {
|
||||
text-decoration: none;
|
||||
color: #000;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
#explore td:first-child a {
|
||||
justify-content: flex-start;
|
||||
}
|
||||
#explore td:last-child a {
|
||||
justify-content: flex-end;
|
||||
}
|
||||
#explore .title {
|
||||
border-left: solid 1px #ccc;
|
||||
display: inline-block;
|
||||
align-items: center;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
padding-left: 5px;
|
||||
}
|
||||
#explore .icon {
|
||||
min-width: 28px;
|
||||
height: 28px;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
border-radius: 50%;
|
||||
color: #fff;
|
||||
margin-right: 5px;
|
||||
font-size: 10px;
|
||||
font-weight: 100;
|
||||
}
|
||||
#explore .explore {
|
||||
position: absolute;
|
||||
right: 10px;
|
||||
z-index: 1000000;
|
||||
cursor: pointer;
|
||||
font-size: 15px;
|
||||
}`;
|
||||
document.documentElement.appendChild(style);
|
||||
|
||||
const cload = () => fetch('matched.json').then(r => r.json()).then(build);
|
||||
const explore = () => {
|
||||
const span = document.createElement('span');
|
||||
span.textContent = '↯';
|
||||
span.title = 'Explore more';
|
||||
span.classList.add('explore');
|
||||
root.appendChild(span);
|
||||
span.onclick = () => {
|
||||
root.textContent = '';
|
||||
localStorage.setItem('explore-count', INC);
|
||||
cload();
|
||||
};
|
||||
};
|
||||
const build = json => {
|
||||
if (json.length === 0) {
|
||||
return;
|
||||
}
|
||||
root.dataset.loaded = true;
|
||||
root.textContent = 'Explore more';
|
||||
const table = document.createElement('table');
|
||||
const tr = document.createElement('tr');
|
||||
const span = document.createElement('span');
|
||||
span.classList.add('close');
|
||||
span.textContent = '✕';
|
||||
span.onclick = () => {
|
||||
root.textContent = '';
|
||||
root.dataset.loaded = false;
|
||||
localStorage.setItem('explore-count', 0);
|
||||
explore();
|
||||
};
|
||||
root.appendChild(span);
|
||||
|
||||
const {homepage_url} = chrome.runtime.getManifest();
|
||||
const origin = homepage_url.split('/').slice(0, -1).join('/');
|
||||
const colors = shuffle(
|
||||
['524c84', '606470', '755da3', 'c06c84', '393e46', '446e5c', '693e52', '1d566e', '693e52', 'd95858', 'f27370']
|
||||
);
|
||||
shuffle(Object.entries(json)).slice(0, 3).forEach(([id, {name}], i) => {
|
||||
const td = document.createElement('td');
|
||||
const a = Object.assign(document.createElement('a'), {
|
||||
target: '_blank',
|
||||
title: 'Click to browse',
|
||||
href: origin + '/' + id + '.html?context=explore'
|
||||
});
|
||||
|
||||
const icon = document.createElement('span');
|
||||
icon.textContent = name.split(' ').slice(0, 2).map(s => s[0]).join('').toUpperCase();
|
||||
icon.classList.add('icon');
|
||||
icon.style['background-color'] = '#' + colors[i];
|
||||
a.appendChild(icon);
|
||||
|
||||
const span = document.createElement('span');
|
||||
span.classList.add('title');
|
||||
span.textContent = name;
|
||||
a.appendChild(span);
|
||||
td.appendChild(a);
|
||||
tr.appendChild(td);
|
||||
});
|
||||
table.appendChild(tr);
|
||||
root.appendChild(table);
|
||||
};
|
||||
const init = () => {
|
||||
if (count >= INC) {
|
||||
if (count < INC + 3) {
|
||||
cload();
|
||||
}
|
||||
else {
|
||||
explore();
|
||||
}
|
||||
if (count > INC + 5) {
|
||||
localStorage.setItem('explore-count', INC - 6);
|
||||
}
|
||||
else {
|
||||
localStorage.setItem('explore-count', count + 1);
|
||||
}
|
||||
}
|
||||
else {
|
||||
explore();
|
||||
localStorage.setItem('explore-count', count + 1);
|
||||
}
|
||||
};
|
||||
init();
|
||||
}
|
32
extension/data/popup/matched.json
Normal file
|
@ -0,0 +1,32 @@
|
|||
{
|
||||
"country-flags": {
|
||||
"name": "Country Flags & IP WHOIS"
|
||||
},
|
||||
"useragent-switcher": {
|
||||
"name": "User Agent Switcher"
|
||||
},
|
||||
"work-offline": {
|
||||
"name": "Work Offline"
|
||||
},
|
||||
"local-cdn": {
|
||||
"name": "Local CDN"
|
||||
},
|
||||
"media-converter": {
|
||||
"name": "Tor Control (Anonymity Layer)"
|
||||
},
|
||||
"save-images": {
|
||||
"name": "Download All Images"
|
||||
},
|
||||
"privacy-settings": {
|
||||
"name": "Privacy Settings"
|
||||
},
|
||||
"media-player": {
|
||||
"name": "YouTube Media Player"
|
||||
},
|
||||
"tab-discard": {
|
||||
"name": "Auto Tab Discard"
|
||||
},
|
||||
"send-to": {
|
||||
"name": "Send to VLC"
|
||||
}
|
||||
}
|
64
extension/manifest.json
Executable file
|
@ -0,0 +1,64 @@
|
|||
{
|
||||
"manifest_version": 2,
|
||||
"name": "User-Agent Switcher and Manager",
|
||||
"short_name": "useragent-switcher",
|
||||
"version": "0.2.7",
|
||||
|
||||
"description": "Spoofs User-Agent strings of your browser with a new one globally, randomly or per hostname",
|
||||
|
||||
"permissions": [
|
||||
"tabs",
|
||||
"storage",
|
||||
"<all_urls>",
|
||||
"webNavigation",
|
||||
"webRequest",
|
||||
"webRequestBlocking",
|
||||
"contextMenus"
|
||||
],
|
||||
|
||||
"icons": {
|
||||
"16": "data/icons/active/16.png",
|
||||
"18": "data/icons/active/18.png",
|
||||
"19": "data/icons/active/19.png",
|
||||
"32": "data/icons/active/32.png",
|
||||
"36": "data/icons/active/36.png",
|
||||
"38": "data/icons/active/38.png",
|
||||
"48": "data/icons/active/48.png",
|
||||
"64": "data/icons/active/64.png",
|
||||
"128": "data/icons/active/128.png",
|
||||
"256": "data/icons/active/256.png"
|
||||
},
|
||||
"background":{
|
||||
"scripts":[
|
||||
"ua-parser.min.js",
|
||||
"common.js"
|
||||
]
|
||||
},
|
||||
"browser_action":{
|
||||
"default_icon": {
|
||||
"16": "data/icons/16.png",
|
||||
"32": "data/icons/32.png",
|
||||
"48": "data/icons/48.png",
|
||||
"64": "data/icons/64.png"
|
||||
},
|
||||
"default_popup": "data/popup/index.html"
|
||||
},
|
||||
"homepage_url": "http://add0n.com/useragent-switcher.html",
|
||||
"options_ui": {
|
||||
"page": "data/options/index.html",
|
||||
"chrome_style": true
|
||||
},
|
||||
"content_scripts": [{
|
||||
"all_frames": true,
|
||||
"run_at": "document_start",
|
||||
"match_about_blank": true,
|
||||
"matches": ["*://*/*"],
|
||||
"js": ["data/inject.js"]
|
||||
}],
|
||||
"applications": {
|
||||
"gecko": {
|
||||
"id": "{a6c4a591-f1b2-4f03-b3ff-767e5bedf4e7}",
|
||||
"strict_min_version": "57.0"
|
||||
}
|
||||
}
|
||||
}
|