简体   繁体   English

如何将消息从 popup.html 发送到内容脚本?

[英]How to send the message from popup.html to content script?

I am making an extension and want to pass the message from the popup.html to content.js but the following code alert undefined .我正在做一个扩展,并希望将消息从popup.html 传递content.js但以下代码警告undefined Please give me a simple script that send message from popup.html to content.js and vice versa, further i will handle it.请给我一个简单的脚本,将消息从popup.html发送到content.js ,反之亦然,我会进一步处理它。 I want to access the DOM through this extension for modifying and designing the website layouts.我想通过这个扩展访问 DOM 来修改和设计网站布局。

Manifest显现

{
  "manifest_version": 2,
  "name": "Extension",
  "description": "Description",
  "version": "1.0",
  "background": {
   "scripts": ["background.js"],
   "persistent": true
  },
  "content_scripts": [{
    "matches": ["*"],
    "js": ["content.js"]
  }],
  "browser_action": {
   "default_icon": "icons/icon.png",
   "default_popup": "popup.html"
  },
  "permissions":["tabs"]
}

popup.js popup.js

document.addEventListener('DOMContentLoaded',function(){

document.getElementById('button').onclick=function(){

    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
            alert(response);
        });
    });

}

});

Content.js内容.js

chrome.runtime.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"});
});

Try this simple code.试试这个简单的代码。

manifest.json清单.json

{
  "name": "test",
  "version": "0.1",
  "manifest_version": 2,

  "description": "Test Extension",

  "permissions": [ "tabs" ],

  "browser_action": {
    "default_popup": "popup.html"
  },
  "content_scripts" : [{
        "matches" :  ["*://*/*"],
        "js" : ["content.js"]
    }]
}

popup.html popup.html

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="utf-8" />
</head>
<body>
  <div id="data"></div>
  <input type="text" id="text"></input>
  <button id="set">Set</button>
  <script src="popup.js"></script>
</body>
</html>



> 

popup.js popup.js

document.getElementById("set").onclick = function() {

  chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
            alert(response.farewell);
        });
    });
}

content.js内容.js

chrome.runtime.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"});
});

alert警报

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM