playing with status

This commit is contained in:
doma 2020-09-02 15:51:04 +02:00
parent ac3482e471
commit a3a05df908

View File

@ -1,72 +1,91 @@
'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 user_agent = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
var targetPage = "*://*/*"; var target_urls = "*://*/*";
var extension_status = 0; var extension_status = 0;
var rewrite = false;
var status = 0;
/*
browser.runtime.onInstalled.addListener(function() { browser.runtime.onInstalled.addListener(function() {
browser.storage.sync.set({extension_status: 0}, function() { browser.storage.sync.set({extension_status: 0}, function() {
console.log('The extension_status is set to 0.'); console.log('The extension_status is set to 0.');
}); });
}); });
function updateIcon() {
browser.storage.sync.get('extension_status', function(data) {
extension_status = data.extension_status;
browser.browserAction.setIcon({path: 'icon' + extension_status + '.png'});
extension_status++;
if (extension_status > 1){
extension_status = 0;
}
browser.storage.sync.set({extension_status: extension_status}, function() {
console.log('The extension_status is set to ' + extension_status);
});
});
};
browser.browserAction.onClicked.addListener(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) { function set_extension_status_ON() {
extension_status = data.extension_status; rewrite = true;
console.log('Browser http request: ' + extension_status); status = 1;
console.log('Setting status to ON');
browser.storage.sync.set({extension_status: 1}, function() {
console.log('Setting extension status to ON');
browser.browserAction.setIcon({path: 'icon1.png'});
});
}
function set_extension_status_OFF() {
rewrite = false;
status = 0;
console.log('Setting status to OFF');
browser.storage.sync.set({extension_status: 0}, function() {
console.log('Setting extension status to OFF');
browser.browserAction.setIcon({path: 'icon0.png'});
}); });
console.log(extension_status); }
if(extension_status == 0){
function get_extension_status(){
browser.storage.sync.get('extension_status', function(data) {
extension_status = data.extension_status;
});
console.log("extension status is: " + extension_status);
return extension_status
}
function update_icon(){
extension_status = get_extension_status();
if(status == 0) {
set_extension_status_ON();
}else{
set_extension_status_OFF();
}
}
function rewrite_user_agent_header(e){
console.log('Browser http request!');
extension_status = get_extension_status();
if(status == 0) {
console.log('Nothing to do');
return {requestHeaders: e.requestHeaders}; return {requestHeaders: e.requestHeaders};
} }
for (var header of e.requestHeaders) { for (var header of e.requestHeaders) {
if (header.name.toLowerCase() === "user-agent") { if (header.name.toLowerCase() === "user-agent") {
header.value = ua; header.value = user_agent;
} }
} }
console.log('Set user-agent header to: ' + user_agent);
return {requestHeaders: e.requestHeaders}; return {requestHeaders: e.requestHeaders};
} }
/* browser.runtime.onStartup.addListener(set_extension_status_OFF);
Add rewriteUserAgentHeader as a listener to onBeforeSendHeaders,
only for the target page.
Make it "blocking" so we can modify the headers. browser.browserAction.onClicked.addListener(update_icon);
*/
browser.webRequest.onBeforeSendHeaders.addListener(rewriteUserAgentHeader, browser.webRequest.onBeforeSendHeaders.addListener(
{urls: [targetPage]}, rewrite_user_agent_header,
["blocking", "requestHeaders"]); {urls: [target_urls]},
["blocking", "requestHeaders"]
);