Jac*_*ard 6 javascript google-chrome-extension
两种方法都Uncaught TypeError: Cannot read property 'query' of undefined
对我的内容脚本产生相同的错误...我已经看过如何使用javascript在chrome扩展程序中获取当前Tab的URL,以及如何使用chrome.tabs.getCurrent来获取页面对象。 Chrome扩展程序?尽管我仍然不确定自己做错了什么。
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"
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
inject.js
chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
console.log(tabs[0].id);
});
Run Code Online (Sandbox Code Playgroud)
要么
chrome.tabs.getCurrent(function (tab) {
console.log(tab.id);
});
Run Code Online (Sandbox Code Playgroud)
小智 7
无法chrome.tabs
在内容脚本中使用,您需要使用chrome.runtime
要了解内容脚本的选项卡ID,请先使用chrome.runtime.sendMessage
来发送消息,然后后台页面将其接收。
使用chrome.runtime.onMessage.addListener
,回调函数为
function(any message, MessageSender sender, function sendResponse) {...};
因此,您将通过 sender.id
归档时间: |
|
查看次数: |
10905 次 |
最近记录: |