我正在尝试使用简单的Google Chrome扩展程序来处理消息/变量流经以下每个步骤...
我已经想出了如何将消息/变量发送到 Background.js并从一个方向(Background.js -> Popup.js或Background.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)