allow setting blank UA using 'empty' keyword

This commit is contained in:
Ray Lothian 2018-11-04 10:18:40 +01:00
parent 166e756a45
commit e51d174079
2 changed files with 10 additions and 6 deletions

View file

@ -199,7 +199,7 @@ var onBeforeSendHeaders = ({tabId, url, requestHeaders, type}) => {
if (str) { if (str) {
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 = str; requestHeaders[i].value = str === 'empty' ? '' : str;
return { return {
requestHeaders requestHeaders
}; };
@ -215,16 +215,20 @@ var onCommitted = ({frameId, url, tabId}) => {
} }
const o = cache[tabId] || ua.object(tabId); const o = cache[tabId] || ua.object(tabId);
if (o.userAgent) { if (o.userAgent) {
let {userAgent, appVersion, platform, vendor} = o;
if (o.userAgent === 'empty') {
userAgent = appVersion = platform = vendor = '';
}
chrome.tabs.executeScript(tabId, { chrome.tabs.executeScript(tabId, {
runAt: 'document_start', runAt: 'document_start',
frameId, frameId,
code: `{ code: `{
const script = document.createElement('script'); const script = document.createElement('script');
script.textContent = \`{ script.textContent = \`{
navigator.__defineGetter__('userAgent', () => '${o.userAgent}'); navigator.__defineGetter__('userAgent', () => '${userAgent}');
navigator.__defineGetter__('appVersion', () => '${o.appVersion}'); navigator.__defineGetter__('appVersion', () => '${appVersion}');
navigator.__defineGetter__('platform', () => '${o.platform}'); navigator.__defineGetter__('platform', () => '${platform}');
navigator.__defineGetter__('vendor', () => '${o.vendor}'); navigator.__defineGetter__('vendor', () => '${vendor}');
}\`; }\`;
document.documentElement.appendChild(script); document.documentElement.appendChild(script);
script.remove(); script.remove();

View file

@ -158,7 +158,7 @@
<input type="button" value="Window" title="Set this string as this window's User-Agent string" data-cmd="window"> <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"> <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> </div>
<input id="ua" type="text" placeholder="Your preferred user-agent string"> <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> <div id="explore"></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>