I'm sending a string message from background.js to popup.js. I do receive a message in popup.js but I can't manipulate it. I am able to view it if a alert() it or if I assign it to innerHTML of a popup.html element, but I can't assign it to any variables in popup.js so I could modify it and make use of it.
I tried regular assignment, I tried using substring() to copy the message into another variable
Here I have a piece of background.js that sends a message to popup.js. myString is a string:
chrome.runtime.sendMessage({
action: "getSource",
source: myString
});
Here is a piece of popup.js that receives the message once background.js is executed, the code below works, it displays the string in popup.html:
chrome.runtime.onMessage.addListener(function(request, sender) {
if (request.action == "getSource") {
container.innerHTML = request.source;}})
This also works:
chrome.runtime.onMessage.addListener(function(request, sender) {
if (request.action == "getSource") {
alert(request.source);}})
But when I try to assign it to a variable, I get nothing (testString is still empty):
var testString = '';
chrome.runtime.onMessage.addListener(function(request, sender) {
if (request.action == "getSource") {
testString = request.source;}})
If I assign a value to testString outside of chrome.runtime.onMessage.addListener(...), the assignment works just fine, testString ends up being "abcde":
var testString = '';
chrome.runtime.onMessage.addListener(function(request, sender) {
if (request.action == "getSource") {
console.log("do nothing")}});
testString = "abcde";
I want to have the contents of request.source in a variable so I could manipulate it.
Thank you.