Ray Lothian 2017-11-19 13:02:13 +03:30
parent cd6e645458
commit 96b13e4b7f
7 changed files with 57 additions and 21 deletions

View file

@ -1,11 +1,18 @@
/* globals UAParser*/
'use strict'; 'use strict';
var ua = 'hi'; var ua = {
userAgent: '',
appVersion: '',
platform: '',
vendor: ''
};
var onBeforeSendHeaders = ({requestHeaders}) => { var onBeforeSendHeaders = ({requestHeaders}) => {
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; requestHeaders[i].value = ua.userAgent;
return { return {
requestHeaders requestHeaders
}; };
@ -21,7 +28,10 @@ var onCommitted = ({frameId, url, tabId}) => {
code: `{ code: `{
const script = document.createElement('script'); const script = document.createElement('script');
script.textContent = \`{ script.textContent = \`{
navigator.__defineGetter__('userAgent', () => '${ua}'); navigator.__defineGetter__('userAgent', () => '${ua.userAgent}');
navigator.__defineGetter__('appVersion', () => '${ua.appVersion}');
navigator.__defineGetter__('platform', () => '${ua.platform}');
navigator.__defineGetter__('vendor', () => '${ua.vendor}');
}\`; }\`;
document.documentElement.appendChild(script); document.documentElement.appendChild(script);
}` }`
@ -30,8 +40,15 @@ var onCommitted = ({frameId, url, tabId}) => {
}; };
function update(str) { function update(str) {
ua = str; ua.userAgent = str;
if (ua) { ua.appVersion = str
.replace(/^Mozilla\//, '')
.replace(/^Opera\//, '');
if (str) {
const p = new UAParser(str);
ua.platform = p.getOS().name || '';
ua.vendor = p.getDevice().vendor || '';
chrome.webRequest.onBeforeSendHeaders.addListener(onBeforeSendHeaders, { chrome.webRequest.onBeforeSendHeaders.addListener(onBeforeSendHeaders, {
'urls' : ['*://*/*'] 'urls' : ['*://*/*']
}, ['blocking', 'requestHeaders']); }, ['blocking', 'requestHeaders']);
@ -43,16 +60,16 @@ function update(str) {
} }
chrome.browserAction.setIcon({ chrome.browserAction.setIcon({
path: { path: {
16: 'data/icons/' + (ua ? 'active/' : '') + '16.png', 16: 'data/icons/' + (str ? 'active/' : '') + '16.png',
32: 'data/icons/' + (ua ? 'active/' : '') + '32.png', 32: 'data/icons/' + (str ? 'active/' : '') + '32.png',
48: 'data/icons/' + (ua ? 'active/' : '') + '48.png', 48: 'data/icons/' + (str ? 'active/' : '') + '48.png',
64: 'data/icons/' + (ua ? 'active/' : '') + '64.png' 64: 'data/icons/' + (str ? 'active/' : '') + '64.png'
} }
}); });
chrome.browserAction.setTitle({ chrome.browserAction.setTitle({
title: `UserAgent Switcher (${ua ? 'enabled' : 'disabled'}) title: `UserAgent Switcher (${str ? 'enabled' : 'disabled'})
User-Agent String: ${ua || navigator.userAgent}` User-Agent String: ${str || navigator.userAgent}`
}); });
} }

View file

@ -1,5 +1,7 @@
Generating the list: ua-parser.min.js:
https://github.com/faisalman/ua-parser-js/blob/master/dist/ua-parser.min.js
Generating the list:
Run Run
JSON.stringify([...document.querySelectorAll('li a')].map(a => a.textContent)) JSON.stringify([...document.querySelectorAll('li a')].map(a => a.textContent))
on on

View file

@ -51,6 +51,7 @@ input {
border: solid 1px #ccc; border: solid 1px #ccc;
box-sizing: border-box; box-sizing: border-box;
height: 24px; height: 24px;
border-radius: 0;
} }
input[type=button] { input[type=button] {
cursor: pointer; cursor: pointer;

View file

@ -147,12 +147,13 @@
<div hbox> <div hbox>
<input type="search" id="custom" placeholder="Filter items"> <input type="search" id="custom" placeholder="Filter items">
<input type="button" value="Update" disabled="true"> <input type="button" value="Update" disabled="true">
<input type="button" value="Refresh" title="Refresh the current page" style="margin-left: 2px;" data-cmd="refresh">
</div> </div>
<div hbox id="agent" pack="center" align="center"> <div hbox id="agent" pack="center" align="center">
User-Agent String:&nbsp; User-Agent String:&nbsp;
<input id="ua" type="text" name=""> <input id="ua" type="text" name="">
<input type="button" value="Apply" data-cmd="apply"> <input type="button" value="Apply" title="Set this string as the browser's User-Agent string" data-cmd="apply">
<input type="button" value="Reset" style="margin-left: 2px;" data-cmd="reset"> <input type="button" value="Reset" title="Reset User-Agent string to the default one" style="margin-left: 2px;" data-cmd="reset">
</div> </div>
<script src="ua-parser.min.js"></script> <script src="ua-parser.min.js"></script>
<script src="index.js"></script> <script src="index.js"></script>

View file

@ -160,5 +160,13 @@ document.addEventListener('click', ({target}) => {
ua: '' ua: ''
}); });
} }
else if (cmd === 'refresh') {
chrome.tabs.query({
active: true,
currentWindow: true
}, ([tab]) => chrome.tabs.reload(tab.id, {
bypassCache: true
}));
}
} }
}); });

File diff suppressed because one or more lines are too long

View file

@ -1,7 +1,7 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "User-Agent Switcher", "name": "User-Agent Switcher",
"version": "0.1.1", "version": "0.1.2",
"description": "Spoofs User-Agent strings of your browser", "description": "Spoofs User-Agent strings of your browser",
@ -24,6 +24,7 @@
}, },
"background":{ "background":{
"scripts":[ "scripts":[
"data/popup/ua-parser.min.js",
"common.js" "common.js"
] ]
}, },
@ -36,5 +37,11 @@
}, },
"default_popup": "data/popup/index.html" "default_popup": "data/popup/index.html"
}, },
"homepage_url": "http://add0n.com/useragent-switcher.html" "homepage_url": "http://add0n.com/useragent-switcher.html",
"applications": {
"gecko": {
"id": "{a6c4a591-f1b2-4f03-b3ff-767e5bedf4e7}",
"strict_min_version": "52.0"
}
}
} }