Google chrome extension 从devtools(自定义选项卡)到内容脚本的消息?

Google chrome extension 从devtools(自定义选项卡)到内容脚本的消息?,google-chrome-extension,google-chrome-devtools,Google Chrome Extension,Google Chrome Devtools,如何将消息从devtools(自定义选项卡)发送到内容脚本 我尝试的是: 我在这里上传了我的代码: 代码: devtools.html <html> <body> <script src="panel.js"></script> yoyoyo </body> </html> Panel.html <html> <body> <

如何将消息从devtools(自定义选项卡)发送到内容脚本

我尝试的是:

我在这里上传了我的代码:

代码:

devtools.html

<html>   
    <body>
        <script src="panel.js"></script>    yoyoyo
    </body>
</html>
Panel.html

<html>
    <body>
        <script src="panel.js"></script>    yoyoyo
    </body>
</html>
injects.js(来自Panel.js的消息未到达此处

我看这里:

  • 指示我应该将脚本发送到Background.js,然后从那里发送到Content script。我也试过了,但没有成功。也可以添加,但我想这里的代码太多了
manifest.json

{
  "manifest_version": 2,
  "name": "Sivis Helper",
  "description": "Click on any element to scrape it via rvest / RSelenium",
  "homepage_url": "https://github.com/",
  "version": "0.1.1",
  "icons": {
    "64": "icons/default-64.png"
  },
  "background": {
    "scripts": ["background.js"]
  },
  "browser_action": {
    "default_icon": "icons/default-64.png",
    "default_title": "Click on any element to scrape it via rvest / RSelenium"
  },
  "content_scripts": [{ 
    "all_frames": true,
    "matches": ["<all_urls>"],  
    "js":["inspect.js"]
  }],
  "commands": {
    "toggle-xpath": {
      "suggested_key": { 
        "default": "Ctrl+Shift+U",
        "mac": "Command+Shift+U"
      },
      "description": "Toggle plugin"
    }
  },
  "options_ui": {
    "page": "options.html"
  },
  "permissions": ["debugger", "pageCapture", "tabs", "activeTab", "<all_urls>", "storage", "webRequest", "clipboardWrite", "clipboardRead", "webRequestBlocking"]
}
{
“清单版本”:2,
“名称”:“西维斯助手”,
“描述”:“点击任何元素,通过rvest/RSelenium将其刮除”,
“主页地址”:https://github.com/",
“版本”:“0.1.1”,
“图标”:{
“64”:“icons/default-64.png”
},
“背景”:{
“脚本”:[“background.js”]
},
“浏览器操作”:{
“默认图标”:“icons/default-64.png”,
“默认标题”:“单击任何元素,通过rvest/RSelenium将其刮除”
},
“内容脚本”:[{
“所有框架”:正确,
“匹配项”:[“”],
“js”:[“inspect.js”]
}],
“命令”:{
“切换xpath”:{
“建议的_键”:{
“默认值”:“Ctrl+Shift+U”,
“mac”:“命令+移位+U”
},
“说明”:“切换插件”
}
},
“选项界面”:{
“页面”:“options.html”
},
“权限”:[“调试器”、“页面捕获”、“选项卡”、“活动选项卡”、“存储”、“webRequest”、“剪贴板写入”、“剪贴板读取”、“webRequestBlocking”]
}

您可以直接向
background.js
发送消息,然后从
background.js->devtools.js
发送消息。
这是一种与
内容脚本
类似的机制,您需要通过
选项卡id

响应可能重复的内容脚本无法接收chrome.runtime.sendMessage,它用于扩展页面。通过chrome.devtools.inspectedWindow.eval有一个更简单的方法:。或者,您可以继续使用runtime.sendMessage,但也可以在后台脚本中添加一个onMessage侦听器,该侦听器将使用chrome.tabs.sendMessage将消息中继到内容脚本。如果您需要示例,应该会有一个说明如何操作的答案。非常感谢!通过后台脚本发送对我来说很有效。
        // i want to send a message  from here to inject.js
        chrome.runtime.sendMessage({
            response: "Hallo"
        });
alert(2); // this works
chrome.runtime.onMessage.addListener(function(response) {alert(Response)})
{
  "manifest_version": 2,
  "name": "Sivis Helper",
  "description": "Click on any element to scrape it via rvest / RSelenium",
  "homepage_url": "https://github.com/",
  "version": "0.1.1",
  "icons": {
    "64": "icons/default-64.png"
  },
  "background": {
    "scripts": ["background.js"]
  },
  "browser_action": {
    "default_icon": "icons/default-64.png",
    "default_title": "Click on any element to scrape it via rvest / RSelenium"
  },
  "content_scripts": [{ 
    "all_frames": true,
    "matches": ["<all_urls>"],  
    "js":["inspect.js"]
  }],
  "commands": {
    "toggle-xpath": {
      "suggested_key": { 
        "default": "Ctrl+Shift+U",
        "mac": "Command+Shift+U"
      },
      "description": "Toggle plugin"
    }
  },
  "options_ui": {
    "page": "options.html"
  },
  "permissions": ["debugger", "pageCapture", "tabs", "activeTab", "<all_urls>", "storage", "webRequest", "clipboardWrite", "clipboardRead", "webRequestBlocking"]
}