Chrome 扩展 - 从 DOM 到 Popup.js 消息传递 [英] Chrome Extension - From the DOM to Popup.js message passing
问题描述
我正在尝试让一个简单的 Google Chrome 扩展程序工作,其中消息/变量流经以下每个步骤......
I'm trying to get a simple Google Chrome extension working where a message/variable flows through each of the following steps ...
- DOM 内容(来自特定的 HTML 标签)
- Contentscript.js
- 背景.js
- Popup.js
- Popup.html
我已经想出了如何向一个方向发送消息/变量到Background.js和来自(Background.js -> Popup.js
或 Background.js -> Contentscript.js
),但无法成功通过所有三个(Contentscript.js -> Background.js ->; Popup.js
).这是我的演示中的文件.
I've figured out how to send a message/variable to Background.js and from it in one direction (Background.js -> Popup.js
or Background.js -> Contentscript.js
), but can't get it through all three successfully (Contentscript.js -> Background.js -> Popup.js
). Here are the files in my demo.
DOM
Joe Blow
Content.js
fromDOM = $('h1.name').text();
chrome.runtime.sendMessage({contentscript: "from: contentscript.js", title: fromDOM}, function(b) {
console.log('on: contentscript.js === ' + b.background);
});
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);
});
});
Popup.js
chrome.extension.sendMessage({pop: "from: popup.js"}, function(b){
console.log('on: popup.js === ' + b.background);
$('.output').text(b.background);
});
Popup.html
<html>
<head>
<script src="jquery.js"></script>
<script src="popup.js"></script>
</head>
<body>
<p class="output"></p>
</body>
</html>
Manifest.json
{
"name": "Hello World",
"version": "1.0",
"manifest_version": 2,
"description": "My first Chrome extension.",
"background" : {
"scripts": ["background.js"]
},
"permissions": [
"tabs"
],
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["http://*/*"],
"js": ["jquery.js","contentscript.js"],
"run_at": "document_end"
}
]
}
我有一种感觉,我知道绊倒是什么,但是 manifest_version: 2
的文档严重缺乏,很难破译.一个简单的、可重用的示例对学习过程非常有帮助,因为我确信这是一个常见问题.
I have a feeling I know what the trip-up is, but the documentation is severely lacking for manifest_version: 2
that its tough to decipher. A simple, reusable example would be very helpful in the learning process, as I'm sure this is a common issue.
推荐答案
好的,更改代码中的一些内容应该可以让它按照您想要的方式工作.并非我将要进行的所有更改都是必需的,但我可能会这样做.
Alright, changing a few things in your code should make it work like you want. Not all of the changes I am going to make are necessary, but this is just how I might do it.
内容脚本
var fromDOM = $('h1.name').text();
chrome.runtime.sendMessage({method:'setTitle',title:fromDOM});
背景
var title;
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
if(message.method == 'setTitle')
title = message.title;
else if(message.method == 'getTitle')
sendResponse(title);
});
Popup.js
chrome.runtime.sendMessage({method:'getTitle'}, function(response){
$('.output').text(response);
});
这篇关于Chrome 扩展 - 从 DOM 到 Popup.js 消息传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!