1

I have made a Chrome extension that opens Windows calculator when I click on the browser action button. Now, I'm trying to start the extension on web page by click using JavaScript code.

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

This is my background.js code:

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

Is there any way to do it?

Makyen
  • 31,849
  • 12
  • 86
  • 121
riv
  • 93
  • 1
  • 11

1 Answers1

2

This is done by message-passing. So your webpage can send a message:

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

and your extension can listen to it:

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

source: https://developer.chrome.com/extensions/messaging

mhtsbt
  • 1,029
  • 2
  • 12
  • 26