我尝试使用chrome.tabs.query()
时收到此错误我的理解是popup.js文件以及后台页面(但不是内容页面)都能够使用chrome.tabs api。考虑到这一点,我在我的popup.js文件中使用api,它已经按预期工作了大约2年。
我最近发现chrome扩展不再有效,在检查之后错误来自chrome.tabs.query(),特别是chrome.tabs未定义,因此无法找到查询属性。
扩展程序在打破之前所做的是取所有打开的网址和标题,并将它们存储在一个文本文件中,然后按下弹出窗口中的按钮即可下载。
问题
我不确定是什么改变了或需要修复什么才能使扩展恢复正常工作。
错误
文件
popup.html
<!DOCTYPE html>
<html>
<head>
<title>Raincheck</title>
<!--jquery-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="popup.js"></script>
<link rel="stylesheet" type="text/css" href="popup.css">
</head>
<body>
<div>
<form id="form">
<input id="input" type="text"/>
<input id="btn" type="submit" value="Raincheck"/>
</form>
</div>
</body>
popup.js
document.addEventListener('DOMContentLoaded', function () {
//get tab urls
chrome.tabs.query({ currentWindow: true }, function (tabs) {
//set date for unique download identifier
var monthNames = [
"01", "02", "03",
"04", "05", "06", "07",
"08", "09", "10",
"11", "12"
];
var date = new Date();
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
var hour = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var name = "raincheck-"+year+"-"+day+"-"+monthNames[monthIndex]+"-"+hour+"-"+minutes+"-"+seconds;
//set chrome tabs api variables
var text = "";
var i = 0;
//instantiate custom name & buttons
var name_input = null;
var btn = document.getElementById("btn");
//get & store tab data
while (i < tabs.length){
if (i != tabs.length-1){
text += tabs[i].title + ": " + "\r\n" + tabs[i].url + "\r\n\r\n";
}
else{
text += tabs[i].title + ": " + "\r\n" + tabs[i].url;
}
i++;
}
//fix military time
if (day<10){day = "0" + day};
if (hour > 12){
hour = hour - 12;
}
//set download to start after user input validation & button press
btn.addEventListener("click", function(){
name_input = $("input").val();
if(name_input != null && /^[a-zA-Z0-9-_ ]*$/.test(name_input) != false && /\S/.test(name_input) != false){
name = name_input;
}
download(name,text);
});
});
//text file download function
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
});
的manifest.json
{
"name": "Raincheck",
"version": "1.2",
"manifest_version": 2,
"description": "save the urls of open tabs in a text file",
"browser_action": {
"default_icon": "raincheck.png",
"default_title": "Raincheck tab saver",
"default_popup": "popup.html"
},
"permissions": [
"tabs"
],
"content_security_policy": "script-src 'self' https://ajax.googleapis.com; object-src 'self'"
}