我正在尝试构建一个简单的 chrome 扩展程序,它会在从内容脚本收到的消息满足要求时删除特定页面的历史记录。我制作了内容脚本来返回文本,还编写了后台脚本,使用 chrome.history.deleteRange() 方法清除给定时间点的历史记录。调试器将 chrome.history 显示为未定义,即使我已将其添加到 list 的权限列表中。有人可以帮我解决这个问题吗?
Manifest.json(其他必需的部分也存在,这是主要区域,因此提供)
"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
}
后台脚本:
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();
});
}
});
调试器在后台页面中显示 typeof(chrome.history) 未定义: Debugger output
最佳答案
我终于解决了这个问题。问题是 chrome 正在重新加载 list 的所有部分,但权限除外,因此该值显示为未定义。只需删除扩展,然后再次加载解压的扩展即可解决问题。
谢谢。
关于javascript - chrome.history 和 chrome.browsingData 未定义,即使添加了权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52823370/