Jquery 将消息从popup.html发送到内容脚本时出错
我正在练习谷歌浏览器扩展开发。我试图将消息从popup.js发送到content.js文件,但出现以下错误: “未捕获引用错误:未定义选项卡” 此错误出现在我的popup.js文件中 下面是我的代码文件 manifst.jsonJquery 将消息从popup.html发送到内容脚本时出错,jquery,html,google-chrome-extension,Jquery,Html,Google Chrome Extension,我正在练习谷歌浏览器扩展开发。我试图将消息从popup.js发送到content.js文件,但出现以下错误: “未捕获引用错误:未定义选项卡” 此错误出现在我的popup.js文件中 下面是我的代码文件 manifst.json { "manifest_version": 2, "name": "by-Surfers", "description": "This extension is for practice...", "version": "0.0.1", "browser_action
{
"manifest_version": 2,
"name": "by-Surfers",
"description": "This extension is for practice...",
"version": "0.0.1",
"browser_action": {
"default_icon": "icon.png",
"default_title": "Click to speak with other surfers..",
"default_popup": "popup.html"
},
"background": {
"scripts": ["event.js"],
"persistent": false
},
"permissions": [
"tabs"
],
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["content.js"]
}
]
}
Popup.html
<!DOCTYPE html>
<html>
<head>
<script src="jquery-2.1.1.min.js"></script>
<script src="popup.js"></script>
<style>
body {
min-width: 300px;
overflow-x: hidden;
}
</style>
</head>
<body>
It will start now....
<button id='btn'>click me</button>
</body>
</html>
event.js
$(document).ready(function(){
$('#btn').click(function(){
StartTab();
});
});
function StartTab(){
chrome.tabs.sendMessage(tabs[0].id, {greeting: "OpenDialog"}, function(response) {
// console.log(response.farewell);
});
}
chrome.browserAction.setBadgeText({text: "CET"});
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
switch(request.type) {
case "dom-loaded":
alert(request.data.myProperty);
break;
}
return true;
});
function OpenContentScript(){
}
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "OpenDialog"){
RunIt();
}
});
function Runit(){
alert("it has started...");
}
content.js
$(document).ready(function(){
$('#btn').click(function(){
StartTab();
});
});
function StartTab(){
chrome.tabs.sendMessage(tabs[0].id, {greeting: "OpenDialog"}, function(response) {
// console.log(response.farewell);
});
}
chrome.browserAction.setBadgeText({text: "CET"});
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
switch(request.type) {
case "dom-loaded":
alert(request.data.myProperty);
break;
}
return true;
});
function OpenContentScript(){
}
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "OpenDialog"){
RunIt();
}
});
function Runit(){
alert("it has started...");
}
请帮忙 问题是
选项卡[0]。id
,因为选项卡
没有在任何地方定义
如果您试图发送到当前活动的选项卡,可以尝试以下操作:
function StartTab(){
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
chrome.tabs.sendMessage(
tabs[0].id,
{greeting: "OpenDialog"},
function(response) {
// console.log(response.farewell);
}
);
});
}
我想您实际上是从一个类似的示例中复制了这段代码,并且忘记了query
包装器