相关疑难解决方法(0)

Chrome扩展程序 - 从DOM到Popup.js消息传递

我正在尝试使用简单的Google Chrome扩展程序来处理消息/变量流经以下每个步骤...

  1. DOM内容(来自特定的HTML标记)
  2. Contentscript.js
  3. Background.js
  4. Popup.js
  5. Popup.html

我已经想出了如何将消息/变量发送 Background.js并一个方向(Background.js -> Popup.jsBackground.js -> Contentscript.js)发送,但无法通过所有三个成功(Contentscript.js -> Background.js -> Popup.js).以下是我演示中的文件.

大教堂

<h1 class="name">Joe Blow</h1>

Content.js

fromDOM = $('h1.name').text();

chrome.runtime.sendMessage({contentscript: "from: contentscript.js", title: fromDOM}, function(b) {
    console.log('on: contentscript.js === ' + b.background);
});
Run Code Online (Sandbox Code Playgroud)

Background.js

chrome.tabs.getSelected(null, function(tab) {
    chrome.extension.onMessage.addListener(function(msg, sender, sendResponse) {

        sendResponse({background: "from: background.js"});
        console.log('on: background.js === ' + msg.title);

    });
});
Run Code Online (Sandbox Code Playgroud)

Popup.js

chrome.extension.sendMessage({pop: "from: popup.js"}, function(b){
    console.log('on: popup.js === ' + b.background);

    $('.output').text(b.background);
}); …
Run Code Online (Sandbox Code Playgroud)

sendmessage google-chrome-extension

10
推荐指数
1
解决办法
6575
查看次数