0

I'm trying to write a simple extension to decode a URL that´s given within the body of a page. I've got this contentscript:

function getText(){
var text = document.body.innerText;
return text;
}
var Text = getText();
console.log(Text);
chrome.runtime.sendMessage({ Text: Text});

and this for my popup:

function decodeURL(Encoded) {
var Decoded = decodeURIComponent(Encoded);
return Decoded;
}
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse){
   localStorage["Text"] = request.Text;
   console.log(localStorage["Text"]);
}
);


var List = localStorage["Text"].split(" ");
var URL = List[List.length - 1];

document.write("<a href=");
document.write(URL);
document.write(">");
document.write("Log In");
document.write("</a>");

Now my content script get´s the text and prints it to the console but the popup script doesn´t seem to be able to access it. I know this has been asked many times but none of the other Threads contained anything I could get to work.

godisalie92
  • 126
  • 1
  • 6
  • Possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Haibara Ai May 23 '16 at 10:21
  • You should put your logic inside the callback of `chrome.runtime.onMessage.addListener` – Haibara Ai May 23 '16 at 10:22
  • I worked through this Thread for the last hour and I still don´t understand a single word. I usually don´t use javascript so I find it highly confusing given the fact that the other thread seems to tackle a completely different issue. Could you maybe give me any pointer towards what I need to implement? – godisalie92 May 23 '16 at 11:18
  • When you call `var List = localStorage["Text"].split(" ");`, `localStorage["Text"] = request.Text;` may have not been executed, because it is a asynchronous call. So when calling `localStorage["Text"]`, you will get `undefined`. – Haibara Ai May 23 '16 at 11:23
  • I had the idea already so i put a while loop around my popup code that breaks when URL is not null but it didn´t really change anything. – godisalie92 May 23 '16 at 11:28
  • URL would be `undefined`, not `null` – Haibara Ai May 24 '16 at 00:39

0 Answers0