Reputation: 570
I'm trying to write a simple Google chrome extension. But when I try to send some dynamic data from contentscript.js to background.html, I get no result. There is no problem with sending static data.
here is the background.html:
<html>
<head>
<script>
chrome.extension.onRequest.addListener(function(request) {
alert(request.text);
});
</script>
</head>
</html>
and here is the conentscript.js with static data that works fine:
var req = {"text": "salam"};
chrome.extension.sendRequest(req);
and this is contentscript.js that doesn't work:
var txt = getSelectedText();
var req = {"text": txt};
chrome.extension.sendRequest(req);
any help is welcomed.
Upvotes: 1
Views: 538
Reputation: 111255
When you pass an object via request you don't pass a reference to it, as with regular functions. This object is actually getting serialized into a string before a request, passed as a string, and then assembled back from a string in a background page (that's why you can't pass anything in a request that cannot be serialized). All references are lost in the process.
You probably would need to rethink your extension architecture to accommodate this.
Upvotes: 1