0

I've read through the Message Passing Documentation and tried to utilize it in my code as follows:

contentscript.js:

var buttonEl = document.getElementById("activateBtn"); if (buttonEl) { buttonEl.addEventListener("click", function () { alert("activateBtn clicked! " + chrome.runtime.sendMessage); chrome.runtime.sendMessage({button: buttonEl}); }); } 

backgroundscript.js:

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { console.log("onMessage:", request); }); 

The goal is to let the content script be injected into a page (specified in the manifest.json) and then run. The script is running just fine - the alert message get's displayed, but the message is not send. Or not received. Either way, that button holds an ID which I need to pass to my privileged background code someway.

When I run the code in the developer console of the extension I receive the message just fine - so the receiving end seems to work.

Any leads on the problem?

Thank you for you time. :)

1 Answer 1

2

If you check the injected page's console, you should see something like

TypeError: Converting circular structure to JSON 

This is because you are trying to pass an HTML object through JSON. Try passing just the ID of the button instead and see if it works then.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that was the problem. Do you also know where I can see such errors? When I open the console of the page where the content script gets injected I don't see any errors ..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.