没有得到内容脚本的响应

时间:2017-06-14 19:25:24

标签: google-chrome-extension message

manifest.json

{

"name":"Word Replacer",
    "description" : "Changes One word to another",
    "version": "1.0",
    "manifest_version": 2,

    "permissions" : ["tabs"],

    "browser_action":{
        "default_icon":"images/16.png",
        "default_popup":"popup.html"
    },

    "background":{
        "scripts":["background.js"]
    },

    "icons":{
        "16":"images/16.png",
        "48":"images/48.png",
        "128":"images/128.png"
    },

    "content_scripts":[{
        "matches" : ["http://*/*", "https://*/*"],
        "js":["content_script.js"],
        "run_at" : "document_end"
    }]


}

popup.html

    <!DOCTYPE html>
<html>
<head>
    <title>Word Replacer</title>
    <script src="popup.js"></script>
</head>
<body style="width: 200px;" id="body">

    <form id="replaceWord" method="POST">
        Enter Old Word: <input type="text" name="oldWord" id="old">
        Enter New Word: <input type="text" name="newWord" id="new">
        <button type="submit" value="Change Word">CHange</button>
    </form>
</body>
</html>

popup.js

function replaceWord(){
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
        console.log(response.value);
    });
});
}



document.addEventListener('DOMContentLoaded', function(){
    var form = document.getElementById('replaceWord');
    form.addEventListener('submit', function(){
        replaceWord();
    })
})

content_script.js

chrome.runtime.onMessage.addListener(function(response, sender, sendResponse){
    sendResponse({value : "hello"});
});

尝试从content_Script接收响应以弹出但不会这样做。 在弹出窗口我有标签ID正确但我没有得到任何回应。 为什么会这样?请帮助。

0 个答案:

没有答案