javascript 内容脚本:未捕获的类型错误:无法读取未定义的属性“onRequest”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10627623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 10:30:29  来源:igfitidea点击:

Content Script: Uncaught TypeError: Cannot read property 'onRequest' of undefined

javascriptgoogle-chrome-extension

提问by Nick Fury

I've been searching all over SO and reading through google docs but I can't seem to find a solution.

我一直在到处搜索并阅读谷歌文档,但我似乎找不到解决方案。

My Chrome extension is injecting a content script and I want to set an onRequest.listenerin order to sendRequests to the content script. This is the scriptI used to for the onRequest.listener. The problem is I keep getting this error for some unknown reason.

我的 Chrome 扩展程序正在注入一个内容脚本,我想设置一个onRequest.listener以便向内容脚本发送请求。这是脚本用来为我onRequest.listener。问题是我由于某些未知原因不断收到此错误。

Error Message:

错误信息:

Uncaught TypeError: Cannot ready property 'onRequest' of undefined

Uncaught TypeError: Cannot ready property 'onRequest' of undefined

contentscript.js line 1;

contentscript.js line 1;

Here's the relevant code...

这是相关的代码...

Manifest.json

清单文件.json

{
  "name": "Injector Extension",
  "version": "1.0",
  "manifest_version": 1,
  "icons": { "128": "icon.png" },
  "browser_action": {
    "default_icon": "icon.png",
    "default_title": "Injector Extension",
    "default_popup": "popup.html"
  },
  "options_page": "options.html",
  "background": {
    "page": "background.html"
  },
  "permissions": [
    "tabs", 
    "http://*/*",
    "https://*/*",
    "unlimitedStorage"],
  "content_scripts": [{
        "matches": [" (injector specific url) "],
        "js": ["contentscript.js"]
  }],
  "web_accessible_resources": ["js/script.js"] 
}

content script

内容脚本

 chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
  if (request.method == "fromPopup") {

    // Send JSON data back to Popup.
    sendResponse({data: "from Content Script to Popup"});

  } else {
     sendResponse({}); // snub them.
  }
});

popup

弹出

chrome.tabs.getSelected(null, function(tab) {
   chrome.tabs.sendRequest(tab.id, {method: "fromPopup", tabid: tab.id}, function(response) {
      console.log(response.data);
   });
});

回答by Silviu-Marian

chrome.extension.onRequest.addListenerworks only in extension context. It won't run inside a content script.

chrome.extension.onRequest.addListener仅适用于扩展上下文。它不会在内容脚本中运行。

chrome.extension.sendRequestworks in content script context

chrome.extension.sendRequest在内容脚本上下文中工作

Update accordingly and will work.

相应地更新并将工作。

Edit: Exemplifying simple message passing:

编辑:举例说明简单的消息传递:

Extension script:

扩展脚本:

chrome.extension.onRequest.addListener(function(r,s,sr){ 
     if(r==='HELLO') return sr.call(this,'BACK AT YOU');
});

Content script:

内容脚本:

chrome.extension.sendRequest('HELLO', function(data){ alert(data); });
// will alert "BACK AT YOU"