0

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

Trying to receive a response from content_Script to popup but not getting so. On the popup I have the tabs id correct but I am not getting any response. Why is that so? Please Help.

Ayush Bansal
  • 51
  • 1
  • 8
  • Your `onMessage.addListener` call may be happening after the message has been sent. If so it will not be around to hear the message. – Zach Sadler Jun 14 '17 at 19:26
  • @ZachSadler so how do I rectify it? – Ayush Bansal Jun 14 '17 at 19:28
  • A form submission navigates the page, so your response-handler function in `sendMessage` will never run. If you cancel the submission (by `return false` in your `submit` handler) I think it will work as you expect. – apsillers Jun 14 '17 at 19:31
  • @apsillers, Still not working. – Ayush Bansal Jun 14 '17 at 19:36
  • 2
    Your user interaction *begins* with the user clicking a `browserAction` button, thus the content script should be injected with [`chrome.tabs.executeScript()`](https://developer.chrome.com/extensions/tabs#method-executeScript) instead of a *manifest.json* `content_script` entry. That way your content script does not burden the browser by being injected into every page just to wait to be used. Using `chrome.tabs.executeScript()`, the script can begin functioning when it is injected with [the data, if any is needed, that has been passed to it](http://stackoverflow.com/a/40815514/3773011). – Makyen Jun 14 '17 at 20:51

0 Answers0