2017-08-07 8 views
0

ブラウザのアクションボタンをクリックすると、Windows電卓を開くChrome拡張機能を作成しました。今、私はJavaScriptコードを使用してクリックしてWebページ上で拡張機能を開始しようとしています。ウェブページから拡張機能のバックグラウンドスクリプトに通信する方法

<!doctype html> 
 
<html> 
 
    <head><title>activity</title></head> 
 
<body> 
 
    <button id="clickactivity" onclick="startextension()">click</button> 
 
    <script> 
 
\t 
 
\t function startextension(){ 
 
\t \t //run/start the extension 
 
\t } 
 
\t 
 
\t </script> 
 
</body> 
 
</html>

これは私のbackground.jsコードです:

chrome.browserAction.onClicked.addListener(function(){ 
    chrome.extension.connectNative('com.rivhit.calc_test'); 
}); 

はそれを行うための方法はありますか?

答えて

0

これは、メッセージの受け渡しによって行われます。だからあなたのWebページには、メッセージを送信することができます。

chrome.runtime.sendMessage({greeting: "hello"}, function(response) { 
console.log(response.farewell); 
}); 

とあなたの拡張子はそれを聞くことができます。

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

ソース:https://developer.chrome.com/extensions/messaging

関連する問題