I have been trying to switch the user agent only for the Chrome extension I am developing. The extension needs to change the user agent so it can scan Dropbox and needs a specific user agent to do so. I have been able to switch the user agent, but either it is for all tabs or for only one website. When I open Dropbox on another tab it is using the other user agent. But I only want the user agent to be switched in the tab of the extension and when I close the extension as it runs in the background.
I have tried this so far:
chrome.webRequest.onBeforeSendHeaders.addListener(function(info) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
// since only one tab should be active and in the current window at once
// the return variable should only have one entry
var activeTab = tabs[0];
var activeTabId = activeTab.id; // or do whatever you need
});
// Replace the User-Agent header
var headers = info.requestHeaders;
headers.forEach(function(header) {
if (header.name.toLowerCase() == 'user-agent') {
header.value = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1468.0 Safari/537.36';
}
});
hasSetUA = true;
return {requestHeaders: headers};
},
// Request filter
{
// Modify the headers for these pages
urls: [
"https://dropbox.com/*"
],
tabId: activeTabId
},
["blocking", "requestHeaders"]
);
Any help would be great. I have added webrequest
, webrequestblocking
, and tabs to my manifest permissions list.