1

I want to get all titles from reddit, and pass it to backgroundScript.js.

It shows all URLs in the console of the page but for some reason it shows me this message on background page:

https://i.stack.imgur.com/3fiBS.png

My code:

manifest.json:

    {
  "manifest_version": 2,

  "name": "Testing stuff",

  "description": "This extension is for testing purposes",

  "version": "1.0",

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

  "content_scripts": [ {
      "exclude_matches": [ "*://*.reddit.com/r/*/ comments /*" ],
      "js": [ "contentScript.js" ],
      "matches": [ "*://*.reddit.com/r/*", "*://*.reddit.com/", "*://*.reddit.com/me/m/*", "*://*.reddit.com/user/*/ m /*" ],
      "run_at": "document_end"
    }  ],
    "permissions": [ "history", "tabs", "http://*/ *" ]

}

contentScript.js:

titleUrls = document.querySelectorAll("a.title");

for (i = 0; i < titleUrlsArray.length; i++) {
    var u = i + 1
    console.log(u + " " + titleUrls[i].href);
}

chrome.runtime.sendMessage(titleUrls)

backgroundScript.js:

chrome.runtime.onMessage.addListener(
    function (response, sender, sendResponse) {
        var data = response;

        alert("in backgroundScript.js, received " + data[1].href);
    }
);

Why does it show undefined on the background page when it shows all URLs just fine in the console of the main page? What am I doing wrong?

Makyen
  • 31,849
  • 12
  • 86
  • 121
KulaGGin
  • 943
  • 2
  • 12
  • 27
  • Possible duplicate of [Send DOM node object via chrome.tabs.sendMessage](http://stackoverflow.com/questions/40062328/send-dom-node-object-via-chrome-tabs-sendmessage) – Makyen Nov 30 '16 at 06:44
  • You are trying to send the actual DOM elements in the message. You can't do that (see linked duplicate). You could create an array of just the `.href` URLs and send that. – Makyen Nov 30 '16 at 06:46
  • Thank you very much for the answer. That solved my problem. For some reason I couldn't find that linked duplicate. And I googled for a long time for an answer before I decided to ask a question here. – KulaGGin Nov 30 '16 at 07:11
  • I'm glad I was able to help. It is sometimes hard to find answers (duplicates). Prior to writing my answer to the linked possible duplicate, I did try looking for a duplicate. It is a question I would expect people to have from time to time. Frankly, I was surprised I did not find a question that had been asked quite some time ago, but a reasonable amount of searching did not result in finding one. As to the one linked, obviously, i knew about the linked duplicate due to having written an answer to it. – Makyen Nov 30 '16 at 07:19

1 Answers1

2

You can't send DOM elements in a runtime.sendMessage() message

The message in runtime.sendMessage() must be "a JSON-ifiable object". DOM elements/nodes are not JSON-ifiable. Thus, you can not send them.

What you will need to do instead of trying to serialize the DOM element is, ultimately, determined by why you need this information in your background script.

You have stated that the information you need is the URLs from the .href property. You could build an array of those URLs and send that:

contentScript.js:

titleUrls = document.querySelectorAll("a.title");
var urlList=[];
for (i = 0; i < titleUrlsArray.length; i++) {
    var u = i + 1
    console.log(u + " " + titleUrls[i].href);
    urlList.push(titleUrls[i].href);
}

chrome.runtime.sendMessage(urlList);

backgroundScript.js:

chrome.runtime.onMessage.addListener(function(response, sender, sendResponse) {
    var urlList = response;
    alert("in backgroundScript.js, received " + urlList[0]);
});
Makyen
  • 31,849
  • 12
  • 86
  • 121