メッセージ/変数が次の各ステップを通過する場所で、単純な Google Chrome 拡張機能を動作させようとしています...
- DOM コンテンツ (特定の HTML タグから)
- Contentscript.js
- Background.js
- Popup.js
- Popup.html
メッセージ/変数をBackground.jsに送信し、そこから一方向 (Background.js -> Popup.js
または)に送信する方法を理解しましたがBackground.js -> Contentscript.js
、3 つすべてを正常に通過させることはできません ( 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);
});
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>
マニフェスト.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
解読が難しいため、ドキュメントが大幅に不足しています。これは一般的な問題であると確信しているため、単純で再利用可能な例は、学習プロセスに非常に役立ちます。