Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.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 browser.runtime.sendMessage返回未定义的消息,尽管直接复制了Mozilla示例_Javascript_Google Chrome Extension - Fatal编程技术网

Javascript browser.runtime.sendMessage返回未定义的消息,尽管直接复制了Mozilla示例

Javascript browser.runtime.sendMessage返回未定义的消息,尽管直接复制了Mozilla示例,javascript,google-chrome-extension,Javascript,Google Chrome Extension,我正在尝试创建一个浏览器扩展,专门针对Chrome。对于内容和背景脚本,我几乎完全从这里复制了这个示例() 我的content.js文件 function send(e){ const sending = chrome.runtime.sendMessage({content:"message"}) console.log("THIS SHOULD BE A PROMISE",sending) } window.addEventListener("

我正在尝试创建一个浏览器扩展,专门针对Chrome。对于内容和背景脚本,我几乎完全从这里复制了这个示例()

我的content.js文件

function send(e){
const sending = chrome.runtime.sendMessage({content:"message"})
console.log("THIS SHOULD BE A PROMISE",sending)
}
window.addEventListener("click",send)
My background.js文件

function handleMessage(request, sender, sendResponse) {
  console.log(`content received is: ${request.content}`);
  return Promise.resolve({response: "response from background script"});
}

chrome.runtime.onMessage.addListener(handleMessage);
当我打开后台页面并单击时,后台页面会打印
内容脚本发送的消息:message
,但我的浏览器控制台会打印
,这应该是一个未定义的承诺

我的manifest.json文件是复制此文件的最小文件

{
  "manifest_version": 2,
  "name": "My First Extension",
  "version": "0.1",
  "browser_action": {
       "default_icon": {                    
         "16": "icon.png",        
         "24": "icon.png",          
         "32": "icon.png"     
       },
    "default_title": "test"      
  },
  "permissions": [
    "activeTab",
    "storage"
  ],
  "background": {
    "scripts": ["js/background.js"],
    "persistent": false
  },
  "content_scripts": [
    {
      "matches": [ "http://*/*", "https://*/*", "file://*/*" ],
      "js": [ "js/content.js" ]
    }
  ],

有人知道发生了什么吗?我在这个主题上检查了多个其他线程,但都没有结果。

您应该使用chrome developer文档来开发chrome扩展,而不是mozilla文档。如果你想让代码像mozilla浏览器扩展一样工作,我想你会想做什么就做什么,在这个链接中没有
.runtime.sendMessage
,所以,我不知道你的代码是如何接近链接中的代码的,更不用说几乎完全相同的副本了!