0

I'm getting an URL via content script and try to pass it to my background page via sendMessage but I receive [object Object].

content script:

var kom = document.querySelectorAll("a[href*='permalink']");
var linkX = kom[0];

chrome.runtime.sendMessage(
    {
      link: linkX
    });

background script:

chrome.runtime.onMessage.addListener(function(response, sender, sendResponse) {
  console.log("MY LINK: " + response.link);
});

I found that you can't pass DOM nodes with messaging and data must be JSON-serializable.

How can I send my URL?

bimbam
  • 11
  • 4
  • @mike `JSON.stringify()` will likely not work on an HTML Element because it has circular references. – Heretic Monkey Mar 17 '17 at 21:55
  • @MikeMcCaughan you are right. I didn't see that he had defined `kom` on line 1, for some reason I thought he was receiving the object and passing it on again. I'll withdraw my comment. Knowing what the object is definitely lets us know how we should pass along the data. –  Mar 17 '17 at 22:44

2 Answers2

0

How about

chrome.runtime.sendMessage(
{
    link: linkX.href
});
Musa
  • 96,336
  • 17
  • 118
  • 137
0

You can try this ..

chrome.runtime.sendMessage({
     link: linkX.href
});
Ahmed Essawy
  • 326
  • 4
  • 13
  • thx very much that worked :) – bimbam Mar 17 '17 at 23:13
  • you are welcome @bimbam can you give correct mark to answer to help others reach answer ^_^ – Ahmed Essawy Mar 17 '17 at 23:15
  • i did that and it was registered but ive not enough reputation :) – bimbam Mar 17 '17 at 23:17
  • one additional quastion plz, now i have the same prob with var theName = document.querySelector("h2"); any suggestions? thx jana – bimbam Mar 17 '17 at 23:20
  • 1
    never mind, can you send more details about the prob ? – Ahmed Essawy Mar 17 '17 at 23:21
  • like the link before i tried to send i get the same [object Object] message when i try to send the result of document.querySelector("h2")... but that's not a link, just text – bimbam Mar 17 '17 at 23:32
  • 2
    You can try `var theName = document.querySelector("h2").innerText;` to get the inner text – Ahmed Essawy Mar 17 '17 at 23:34
  • thy again, innerText works, but i think the main problem is the iframe in which the content script is running http://stackoverflow.com/questions/42868426/content-script-and-iframe ...really makes me mad – bimbam Mar 18 '17 at 10:54
  • Can you give me the HTML code which you want to select by `div.posting.upost.upost-is-expanded[data-is-selected*='true']` to hep me test and provide you with answer ? @bimbam – Ahmed Essawy Mar 18 '17 at 10:58
  • sry for the delay ahmed.. i can send u a link http://derstandard.at/2000054634819/Dr-Oetker-bringt-Schoko-Pizza-auch-inOesterreich-auf-den-Markt#posting-1019413480 result should be 'bimbam007' in this case. as i wrote in my other topic: this selector works for me in an content script for the active tab, but it doesnt work when the content script runs in the iframe in background page. thx! jana – bimbam Mar 22 '17 at 19:54