0

What i am trying to do is,

Popup.html

In popup.html i created a logIn page, after user click the LogIn button i successfully loggedIn at that time in local storage i store my token and username.

content.js

    chrome.runtime.sendMessage({greeting: "hello"}, function (response) {
            // console.log('Data to Call', response, dialingNumber);
            if (response == undefined || response == null) {
                window.confirm('Please LogIn...!');
            }
            else {
// ....
}  

background.js

chrome.runtime.onMessage.addListener(
        function (request, sender, sendResponse) {
            var credentials = localStorage.getItem('voi-auth').split("|");
            console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
            if (request.greeting == "hello")
                sendResponse({farewell: credentials});
        });  

It's working fine at the time i first installed, able to call the credential which i store in local storage from the content script.

Problem

My problem is that if i went to the extension and disable the extension then again enable it , i can't communicate between content and background scripts , response is undefined in content script when i call the send message function, even though the local storage has the token and username. Sad part is it will work fine, only when i inspect the background.js and make it open all the time. It won't work when i close my popup.

Is there any solution to avoid this suitation something like execute the background page like that?

Sangeeth John
  • 330
  • 1
  • 10
  • This is a known problem in Chrome and you can find several answers here on StackOverflow ([example](https://stackoverflow.com/questions/10994324/chrome-extension-content-script-re-injection-after-upgrade-or-install)). In short, reinject your content scripts explicitly using chrome.tabs.executeScript in the background page. – wOxxOm Jun 27 '18 at 12:35
  • @wOxxOm, Is it applicable when i disable/enable my extension – Sangeeth John Jun 27 '18 at 12:42
  • @wOxxOm, i mean are you sure the code you provide will work, Anyway that's not solving my problem. – Sangeeth John Jun 27 '18 at 12:53
  • @wOxxOm, actually my problem is with background.js not content.js – Sangeeth John Jun 27 '18 at 12:54
  • I'm too lazy to convince you that the underlying problem is the same so hopefully someone else will help here. – wOxxOm Jun 27 '18 at 12:56
  • BTW you can store the data in chrome.storage.local and thus there'll be no need for communication because the content scripts can access chrome.storage.local of the extension. – wOxxOm Jun 27 '18 at 13:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/173890/discussion-between-sangeeth-john-and-woxxom). – Sangeeth John Jun 27 '18 at 13:29

0 Answers0