-1

I have a add this chrome extension in to Chrome. I took the extension code from here. Now I want to trigger this extension from my web page "tack shot" button click something like following:

<html>
<head>
<script>
function takeShot(){
    //code to trigger chrome extension script.
}
</script>
</head>
<body>
<button onClick='takeShot()'>take shot</button>
</body>
</html>

Can anybody help me to resolve this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Saravanan
  • 337
  • 2
  • 6
  • 17
  • possible duplicate of [Sending message to background script](http://stackoverflow.com/questions/26090563/sending-message-to-background-script) – Xan Jan 31 '15 at 11:04

1 Answers1

0

https://developer.chrome.com/extensions/messaging#external-webpage

manifest.json

{
    "name": "My Extension",
    "version": "1.0",
    "manifest_version": 2,
    "background": {
       "scripts": ["background.js"]
    },
    "externally_connectable": {
        "matches": ["http://www.example.com/*"]
    }
}

background.js

chrome.runtime.onMessageExternal.addListener(
  function(request, sender, sendResponse) {
    // your code in exstension
  });

http://www.example.com/

    <html>
    <head>
    <script>
   var extensionId = "your exstension id here";
    function takeShot(){
        chrome.runtime.sendMessage(extensionId, yourMessage,
            function(response) {
                if (!response.success)
                    console.log('error');
            });
    }
    </script>
    </head>
    <body>
    <button onClick='takeShot()'>take shot</button>
    </body>
    </html>