ContentScript.js 和 Chrome 扩展之间的通信 [英] Communication between ContentScript.js and Chrome Extension

查看:40
本文介绍了ContentScript.js 和 Chrome 扩展之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想将当前标签页 url 发送到我的扩展程序:

以下是我的 manifest.json

<代码>{"name": "DocUrlExtention","版本": "1.0",清单版本":2,"description": "我做的第一个扩展.",浏览器动作":{"default_icon": "icon.png","default_popup": "popup.html"},内容脚本":[{"匹配": ["http://*/*"],"js": ["contentscript.js"]}]}

以下是我的 contentscript.js

chrome.extension.sendRequest({url: window.location.href}, function(response) {控制台日志(响应.告别);});

以下是我的 popup.html

<头><title>入门扩展的弹出窗口</title><脚本>chrome.extension.onRequest.addListener(功能(请求,发件人,sendResponse){控制台.log(sender.tab ?来自内容脚本:"+ sender.tab.url :来自扩展");});<!-- 为了安全起见,JavaScript 和 HTML 必须位于不同的文件中.--><!--<script src="popup.js"></script>--><身体><div id="mydiv">文档 ID:</div>

我在控制台中没有看到任何东西.我是 Chrome 扩展程序的新手.

解决方案

您的清单文件包含 "manifest_version": 2,,它启用 内容安全政策.默认情况下,内联 JavaScript 不会被执行.而且,没有办法放松 CSP 以允许内联 JavaScript.

您有两个选择:

  1. 删除 "manifest_version": 2(禁用默认 CSP).
  2. 将内联 JavaScript 移至外部文件.

推荐第二种方法,在你的代码中也建议...

<前>...<!-- JavaScript 和 HTML 必须在不同的文件中以确保安全. --><!--<script src="popup.js"></script>-->...

附注.弹出窗口的开发工具可以通过右键单击浏览器操作图标打开,然后选择最后一个选项 "检查弹出窗口".

I simply want to send the current tab url to my extension:

Following is my manifest.json

{
 "name": "DocUrlExtention",
 "version": "1.0",
 "manifest_version": 2,
 "description": "The first extension that I made.",
 "browser_action": {
 "default_icon": "icon.png",
 "default_popup": "popup.html"
},
 "content_scripts": [
 {
  "matches": ["http://*/*"],
  "js": ["contentscript.js"]
 }
 ]}

Following is my contentscript.js

chrome.extension.sendRequest({url: window.location.href}, function(response) {
   console.log(response.farewell);
});

Following is my popup.html

<!doctype html>
<html>
<head>
<title>Getting Started Extension's Popup</title>

<script>
    chrome.extension.onRequest.addListener(
      function(request, sender, sendResponse) {
        console.log(sender.tab ?
                    "from a content script:" + sender.tab.url :
                    "from the extension");
      });
</script>

<!-- JavaScript and HTML must be in separate files for security. -->
<!--<script src="popup.js"></script>-->
</head>
<body>
<div id="mydiv">Doc Id:</div>
</body>
</html>

I don't see anything in console. I am new to Chrome extensions.

解决方案

Your manifest file contains "manifest_version": 2,, which enables the Content Security Policy. By default, Inline JavaScript will not be executed. And, there is no way to relax the CSP such that inline JavaScript is allowed.

You've got two options:

  1. Remove "manifest_version": 2 (which disabled the default CSP).
  2. Move the inline JavaScript to an external file.

The second method is recommended, and also suggested in your code...

...
<!-- JavaScript and HTML must be in separate files for security. -->
<!--<script src="popup.js"></script>-->
</head>
...

PS. The Dev tools for the pop-up can be opened by right-clicking on the browser action icon, and selecting the last option, "Inspect popup".

这篇关于ContentScript.js 和 Chrome 扩展之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆