0

I'm having trouble with receiving messages from both background.js and popup.js. I want to access the activeTab DOM content when clicking on button in popup. Here is the part manifest.json:

    ...
"background": {
    "scripts":    ["background.js"],
    "persistent": false
},
"content_scripts": [
    {
        "matches": ["<all_urls>"],
        "js":      ["content.js"]
    }
],
"permissions": [
    "tabs",
    "activeTab",
    "<all_urls>"
]

I send message from popup.js to background (and to content scrpit):

document.addEventListener('DOMContentLoaded', function() {
    document.getElementById("start_button").onclick = function() {

        chrome.tabs.query({active:true, currentWindow: true}, function(tabs){
            chrome.tabs.sendMessage(tabs[0].id, {
                type: "start",
                text: "task from popup to content"
            });
    });
});

But i can't recieve message from both background and popup in content.js:

function get_stuff_from_page(text) {
    alert('recieved IN content:' + text);

    var blocks_number = document.getElementsByTagName('div').length;
    //how do i pass it back to background?
}

//i try to recieve it here and it doesn't work
chrome.extension.onMessage.addListener(function(message, sender, sendResponse) {
    switch (message.type) {
        case "start":
            alert('recieved message 1');
            get_stuff_from_page(message.text);
        break;
    }
});

//also doesn't work here
chrome.runtime.onMessage.addListener( function(message, sender, sendResponse) {
    switch (message.type) {
        case "start":
            alert('recieved message 2');
            get_stuff_from_page(message.text);
    }
});

Where is my fault in receiving messages (or even in sending them)? I'm really confused because I tried most ways to do it and nothing works. And also how do I pass page's DOM back to background? Thanks in advance!

UPD: suggested answer (Popup script and content script can't communicate?) seems not to work for me. Maybe I'm missing something global?

pah
  • 4,700
  • 6
  • 28
  • 37
Random Guy
  • 1,095
  • 16
  • 29
  • Which part did not work? What did you try from that answer? – Xan Jul 21 '15 at 13:18
  • @Xan i used chrome.tabs.sendMessage in popup and tried to recieve it in content with both chrome.extension.onMessage and chrome.runtime.onMessage. Also tried to send it from background and still nothing works. Not sure how to recieve messages properly – Random Guy Jul 21 '15 at 13:31
  • Have you read the link in that answer? Did you reload the page AFTER reloading your extension before trying to message it? – Xan Jul 21 '15 at 13:33
  • i have read it and tried reloading pages and opening new pages and also mathced that 'div' but when i press the button nothing happens. not a single alert appears – Random Guy Jul 21 '15 at 13:50
  • 1
    Oh dammit. You could've noticed it earlier if you inspected the popup. You can't assign `onclick` attribute, that's inline code. You need to `addEventListener`. **Your messaging code is fine**. – Xan Jul 21 '15 at 14:04
  • @Xan, you saved me. It works now. I also forgot to rename button's id in code that i use (i changed it before posting it here), so no wonder it didnt work. Thanks, man! – Random Guy Jul 21 '15 at 14:13

0 Answers0