这两种方法都为我的内容脚本生成了相同的错误Uncaught TypeError: Cannot read property 'query' of undefined ...我已经看过How to fetch URL of current Tab in my chrome extension using javascript和How do you use chrome.tabs.getCurrent to get the page object in a Chrome extension?了,尽管我仍然不确定我做错了什么。
manifest.json
{
"name": "Extension Tester",
"version": "0.0.1",
"manifest_version": 2,
"description": "Tester",
"permissions": [
"tabs"
],
"content_scripts": [
{
"matches": ["https://www.google.com/"],
"js": [
"inject.js"
]
}
]
}inject.js
chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
console.log(tabs[0].id);
});或
chrome.tabs.getCurrent(function (tab) {
console.log(tab.id);
});发布于 2016-02-20 20:45:05
无法在内容脚本中使用chrome.tabs,您需要使用chrome.runtime来了解内容脚本的标签id,首先使用chrome.runtime.sendMessage发送消息,后台页面接收消息。
使用chrome.runtime.onMessage.addListener时,回调函数为
function(any message, MessageSender sender, function sendResponse) {...};
因此,您可以通过sender.id知道选项卡id
https://stackoverflow.com/questions/26415925
复制相似问题