Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法在背景页和内容页之间建立连接_Javascript_Html_Events_Dom_Google Chrome Extension - Fatal编程技术网

Javascript 无法在背景页和内容页之间建立连接

Javascript 无法在背景页和内容页之间建立连接,javascript,html,events,dom,google-chrome-extension,Javascript,Html,Events,Dom,Google Chrome Extension,我正在编写chrome扩展,并试图在背景页面和内容页面之间发送消息 问题是,连接从未建立。我甚至从谷歌文档中复制了代码,但都没有用 这是我的清单页面 { "name": "x", "description": "x", "version": "0.1", "permissions": ["contextMenus", "tabs", "notifications"], "content_scripts": [ { "matches": ["http://

我正在编写chrome扩展,并试图在背景页面和内容页面之间发送消息

问题是,连接从未建立。我甚至从谷歌文档中复制了代码,但都没有用

这是我的清单页面

{
  "name": "x",
  "description": "x",
  "version": "0.1",
  "permissions": ["contextMenus", "tabs", "notifications"],
   "content_scripts": [
    {
      "matches": ["http://*/*","https://*/*"],
      "js": ["jquery.js", "content_script.js"]
    }
  ],
  "background": {
    "scripts": ["sample.js"]
  },
  "manifest_version": 2
}
function genericOnClick(info, tab) 
{
       //copy pasted from google tutorials. My own code also didn't work
       chrome.tabs.getSelected(null, function(tab) {
  chrome.tabs.sendMessage(tab.id, {greeting: "hello"}, function(response) {
    console.log(response.farewell);
  });
});


}



// Create a parent item and two children.
var parent1 = chrome.contextMenus.create({"title": "Rank Trigger", "contexts":["all"]});

var child1 = chrome.contextMenus.create
(
    {"title": "Rank 1", "contexts":["all"], "parentId": parent1, "onclick": genericOnClick}
);
var child2 = chrome.contextMenus.create
(
    {"title": "Rank 2", "contexts":["all"], "parentId": parent1, "onclick": genericOnClick}
);
var child2 = chrome.contextMenus.create
(
    {"title": "Rank 3", "contexts":["all"], "parentId": parent1, "onclick": genericOnClick}
);
我的背景页面

{
  "name": "x",
  "description": "x",
  "version": "0.1",
  "permissions": ["contextMenus", "tabs", "notifications"],
   "content_scripts": [
    {
      "matches": ["http://*/*","https://*/*"],
      "js": ["jquery.js", "content_script.js"]
    }
  ],
  "background": {
    "scripts": ["sample.js"]
  },
  "manifest_version": 2
}
function genericOnClick(info, tab) 
{
       //copy pasted from google tutorials. My own code also didn't work
       chrome.tabs.getSelected(null, function(tab) {
  chrome.tabs.sendMessage(tab.id, {greeting: "hello"}, function(response) {
    console.log(response.farewell);
  });
});


}



// Create a parent item and two children.
var parent1 = chrome.contextMenus.create({"title": "Rank Trigger", "contexts":["all"]});

var child1 = chrome.contextMenus.create
(
    {"title": "Rank 1", "contexts":["all"], "parentId": parent1, "onclick": genericOnClick}
);
var child2 = chrome.contextMenus.create
(
    {"title": "Rank 2", "contexts":["all"], "parentId": parent1, "onclick": genericOnClick}
);
var child2 = chrome.contextMenus.create
(
    {"title": "Rank 3", "contexts":["all"], "parentId": parent1, "onclick": genericOnClick}
);
内容脚本:

//copied from google tutorials
chrome.extension.onMessage.addListener(
  function(request, sender, sendResponse) {
    console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
    if (request.greeting == "hello")
      sendResponse({farewell: "goodbye"});
  });
有什么想法吗?可能是因为事件是从上下文菜单触发的,但我不太确定。我是JS和chrome扩展编程新手


谢谢

在元素上设置一个
onclick
处理程序,然后使用
event.currentTarget.outerHTML
获取所单击元素的HTML。

结果表明我的代码按预期工作,消息确实在每次onclick时发送。问题是我在错误的页面上测试它,而contentScript并没有被注入其中


我犯了一个愚蠢的错误…感谢大家的帮助

genericOnClick(e)
正在将事件对象作为
e
传递,对吗?因此,您需要
e.target
,还是我弄错了?我在这里发布代码后编辑代码时写的一个打字错误……这两个都是我代码中的“事件”:您应该显示用于设置句柄的代码按请求编辑代码如果出现以下错误,我是否需要“#包括”任何内容?“contextMenus”的事件处理程序中出错:无法读取未定义类型的属性“outerHTML”错误:无法在genericOnClick(chrome)上读取未定义类型的属性“outerHTML”-extension://inhjfhckdfpegapembanojbhadkkkdme/sample.js:5:30)在contextMenus:42:17在chrome.Event.dispatchToListener(事件绑定:387:21)在Object.chromeHidden.Event.dispatchEvent(Event_bindings:257:7)的dispatchArgs(Event_bindings:249:22)的chrome.Event.dispatchEvent(Event_bindings:373:27)中,表示您没有正确引用元素。触发onclick函数时,获取传递到函数中的事件对象,并查看它是否具有已定义的currentTarget属性。currentTarget属性应该是对所需元素的引用…我认为问题在于从后台页面向内容页面发送消息时。我再次编辑了这个问题,使其不那么本地化。