'chrome.history and chrome.browsingData undefined even though permissions are added
I'm trying to build a simple chrome extension which deletes the history of a particular page when a message received from content script satisfies a requirement. I have made the content script to return the text and have also written the background script which clears the history from a given point of time using the chrome.history.deleteRange() method. The debugger shows chrome.history as undefined even though I have added it in the permissions list in the manifest. Can someone help me solve this issue?
Manifest.json ( other required parts are also present, this is the main region and hence provided )
"browser_action": {
"default_icon" : "internet.png" ,
"default_title" : "Mark II test phase ",
"default_popup" : "popup.html"
},
"permissions" : [
"history" ,
"activeTab" ,
"tabs" ,
"http://*/" ,
"https://*/" ],
"content_scripts" : [{
"matches": ["<all_urls>"],
"js": ["content_script.js"]
}] ,
"background" : {
"scripts" : ["background.js"],
"persistent" : true
}
background script :
chrome.runtime.onMessage.addListener ( async function ( message , sender , response ) {
// received message
// check for matches
var regex = /(\w|\s)*\w(?=")|\w+/g;
words = message.match(regex);
words.sort();
common_values = intersection ( words , values );
if ( common_values.length > THRESHOLD ) {
chrome.tabs.query({active: true,currentWindow: true}, function (tabs) {
var tab = tabs[0];
var url = tab.url;
console.log ( "Current url : " , url );
/*
// time == 1 min before starting this query
var start_time = (new Date()).getTime() - 1 * 60 * 1000;
var end_time = ( new Date()).getTime();
var range = new Object();
range.startTime = start_time ;
range.endTime = end_time ;
console.log ( "History type " , typeof (chrome.history) )
chrome.history.deleteRange ( range );
console.log("History cleared");
*/
delete_history();
});
}
});
The debugger shows the typeof(chrome.history) as undefined from the background page : Debugger output
Solution 1:[1]
I finally solved this issue. The problem was that chrome was reloading all the parts of the manifest except the permissions and hence the value was being displayed as undefined. Just removing the extension and then loading the unpacked extensions again solved the problem.
Thanks.
Solution 2:[2]
I had a similar problem, "chrome.browsingData" was undefined even though permissions in manifest were correct. It took me some time to understand that there are 3 ways to write code in Chrome extension each with different permissions:
- Content
Under "content_scripts" in manifest you can include JavaScrit file that will be included in the site you are visiting. This option provides full access to the DOM but you are limited to the JavaScript permissions same as the site. It means no access to "chrome.browsingData" for example.
- Popup
Under "browser_action" set the "default_popup" with the HTML to be launched once the user clicks on the extension logo. In this case the permissions are based on the manifest.
- Background
Under "background" in the manifest set the JS files to run all the time in the background. These JS file have permissions based on the requested permission in the manifest.
My solution was to sent a message with parameters from the DOM (Chrome extension content script) to the background page to perform the action that required permissions (clean cookies).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | supra |
| Solution 2 | Guy Shahaf |
