0

I'm trying to send a message from my popup.js to a content.js and I just don't get why it doesn't work.

Here is my set up:

manifest.json

{
    "manifest_version": 2,

    "name": "Form Builder",
    "description": "This extension populates a form.",
    "version": "0.1",

    "content_scripts": [{
        "all_frames": true,
        "matches": ["http://*/*", "https://*/*"],
        "js" : [
            "/js/jquery/jquery.min.js", 
            "/js/formLoader.js",
            "/js/content.js"
        ]
    }],

    "options_page": "/html/options.html",
    "permissions": ["https://localhost:8443/", 
                    "tabs", "activeTab", "storage", "background"
    ],
    "icons": { "16": "/images/icons/wb_icon_16.png",
               "48": "/images/icons/wb_icon_48.png",
              "128": "/images/icons/wb_icon_128.png" 
    },
    "browser_action": {
        "default_popup": "/html/popup.html"
    }
}

content.js

chrome.extension.onRequest.addListener(function(request, sender, sendResponse){
    sendResponse("received message: " + request);
    formLoader.setFormData(request, sendResponse);

    // Why isn't this getting executed when I send a message from popup?

    sendResponse("Thank you");
});

popup.js

var popup = {
    // a few parts snipped out for brevity
    sendMessage: function(message) {
        chrome.tabs.sendMessage(tab.id, message, function(message) {
            console.log(chrome.runtime.lastError);
            popup.handleCallbacks(message);
        });
    },
    handleCallbacks: function (message)  { 
        console.log("handleCallbacks: " + message);
    }
}

formLoader.js

var formLoader = {
    init : function() {
        $("#PostingBody").val("formLoader.init");
    },
    setFormData: function(data, callback) {
        callback("[formLoader.js] got data: " + data);
    }
}

formLoader.init();

When I do something like:

popup.sendMessage(data);

In my console I get:

Object {message: "Could not establish connection. Receiving end does not exist."}
handleCallbacks: undefined

Why isn't there a receiving end? What am I doing wrong with the listener in the content.js?

kasdega
  • 18,396
  • 12
  • 45
  • 89

1 Answers1

0

Thanks to: This answer from another question

For Chrome 26+, use chrome.runtime.onMessage and chrome.runtime.sendMessage.

even though I'm positive I've found several pages and references that fail to mention this.

Community
  • 1
  • 1
kasdega
  • 18,396
  • 12
  • 45
  • 89