added user-agent logic

This commit is contained in:
doma 2020-09-01 19:00:35 +02:00
parent 795173a45f
commit 75e10fb394
2 changed files with 59 additions and 12 deletions

View File

@ -1,25 +1,72 @@
'use strict'; 'use strict';
var browser = browser || chrome var browser = browser || chrome
var ua = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
var targetPage = "*://*/*";
var extension_status = 0;
browser.runtime.onInstalled.addListener(function() { browser.runtime.onInstalled.addListener(function() {
browser.storage.sync.set({number: 0}, function() { browser.storage.sync.set({extension_status: 0}, function() {
console.log('The number is set to 0.'); console.log('The extension_status is set to 0.');
}); });
}); });
function updateIcon() { function updateIcon() {
browser.storage.sync.get('number', function(data) { browser.storage.sync.get('extension_status', function(data) {
var current = data.number; extension_status = data.extension_status;
browser.browserAction.setIcon({path: 'icon' + current + '.png'});
current++; browser.browserAction.setIcon({path: 'icon' + extension_status + '.png'});
if (current > 1) extension_status++;
current = 0; if (extension_status > 1){
browser.storage.sync.set({number: current}, function() { extension_status = 0;
console.log('The number is set to ' + current); }
browser.storage.sync.set({extension_status: extension_status}, function() {
console.log('The extension_status is set to ' + extension_status);
}); });
}); });
}; };
browser.browserAction.onClicked.addListener(updateIcon); browser.browserAction.onClicked.addListener(updateIcon);
//updateIcon(); //updateIcon();
//
//var ua = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
var targetPage = "*://*/*";
/*
Rewrite the User-Agent header to "ua".
*/
function rewriteUserAgentHeader(e) {
browser.storage.sync.get('extension_status', function(data) {
extension_status = data.extension_status;
console.log('Browser http request: ' + extension_status);
});
console.log(extension_status);
if(extension_status == 0){
return {requestHeaders: e.requestHeaders};
}
for (var header of e.requestHeaders) {
if (header.name.toLowerCase() === "user-agent") {
header.value = ua;
}
}
return {requestHeaders: e.requestHeaders};
}
/*
Add rewriteUserAgentHeader as a listener to onBeforeSendHeaders,
only for the target page.
Make it "blocking" so we can modify the headers.
*/
browser.webRequest.onBeforeSendHeaders.addListener(rewriteUserAgentHeader,
{urls: [targetPage]},
["blocking", "requestHeaders"]);

View File

@ -13,8 +13,8 @@
"scripts": ["background.js"], "scripts": ["background.js"],
"persistent": false "persistent": false
}, },
"permissions": ["storage"], "permissions": ["storage", "webRequest", "webRequestBlocking", "*://*/*"],
"browser_action": { "browser_action": {
"name": "Click to change your browsers perspective", "name": "Click to change your browsers perspective",
"default_icon": "icon0.png" "default_icon": "icon0.png"
}, },