Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 从网页中调用chrome扩展中的java脚本方法按钮单击_Html_Javascript Events_Google Chrome Extension - Fatal编程技术网

Html 从网页中调用chrome扩展中的java脚本方法按钮单击

Html 从网页中调用chrome扩展中的java脚本方法按钮单击,html,javascript-events,google-chrome-extension,Html,Javascript Events,Google Chrome Extension,我在chrome中添加了一个chrome扩展。我从中获取了扩展代码。现在我想从我的网页“tack shot”按钮触发这个扩展,点击下面的内容 <html> <head> <script> function takeShot(){ //code to trigger chrome extension script. } </script> </head> <body> <button onClcik='takeS

我在chrome中添加了一个chrome扩展。我从中获取了扩展代码。现在我想从我的网页“tack shot”按钮触发这个扩展,点击下面的内容

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

函数takeShot(){
//触发chrome扩展脚本的代码。
}
拍摄
谁能帮我解决这个问题。提前谢谢。

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


var extensionId=“此处的扩展id”;
函数takeShot(){
chrome.runtime.sendMessage(extensionId,yourMessage,
功能(响应){
如果(!response.success)
console.log('error');
});
}
拍摄
    <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>