0

I am currently trying to pass a message from the background script to a content script on startup (i.e. before the extension icon is clicked).

I have tried the following (inspired by https://stackoverflow.com/a/23895822/10500893 ), but it doesn't seem to be working.

background.js

function preloadPopupScript(){
    return new Promise(function(resolve, reject){
        chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
            chrome.tabs.executeScript(tabs[0].id, {file: "popup.js"}, 
function(){
                if(chrome.runtime.lastError){
                    reject(Error("could not preload popup.js :("));
                }else{
                    resolve();
                }
            });
        });
    });
}

chrome.runtime.onStartup.addListener(function(){
    let preloadScript = preloadPopupScript();
    preloadScript.then(function(){

        let lastXHRresponse = chrome.storage.local.get("xhrResponse", 
function(d){
            if(chrome.runtime.lastError){
                console.log("could not find past authentication data!");
                return;
            }

            // GETS TO HERE
            chrome.runtime.sendMessage({status: "hasPreviouslyLoggedIn"});
            checkAccessToken();
            chrome.runtime.sendMessage({status: "checkArtistReleases"});
        });
    });
});

popup.js

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
    switch(request.status){

        // THE BELOW CASE DOES NOT SHOW ANY SIGNS OF GETTING TRIGGERED
        case "hasPreviouslyLoggedIn":
        case "successfulLogin":
            chrome.browserAction.setPopup({popup: "login_signedin.html"});
            window.location.href = "login_signedin.html";
            hasSignedIn = true;
            chrome.runtime.sendMessage({action: "canCreateAlarm"});
            break;
    }
});

Are there any issues with this code or did I mis-implement the suggested solution in https://stackoverflow.com/a/23895822/10500893?

Thanks much much much in advance!!!

(Also apologies in advance for noob styling issues, as I have just picked up JS >.<)

Castaire
  • 3
  • 5
  • 1. Use chrome.**tabs**.sendMessage with a tab id parameter in the background script to communicate with a content script. 2. You're injecting a browserAction popup script into the web page as though it's a content script, but it's not - for example content scripts can't use most of chrome.xxxx APIs. 3. Apparently this is a X/Y problem which should be solved differently. What is the desired behavior here? – wOxxOm Dec 24 '18 at 04:15
  • 2. Forgot about the API limits for content scripts, thanks much! 3. When the user opens up the browser, I would like to: a) update popup html version based on previously stored information (if available), and b) make an API call to check for updates. – Castaire Dec 25 '18 at 20:24
  • The browserAction/pageAction popup doesn't need to be preloaded/updated beforehand because it doesn't run when not actually opened by the user so you simply can store the data in localStorage or chrome.storage.local and read it in the popup's script which runs every time the popup is opened. As for update check, sounds like chrome.runtime.onUpdateAvailable – wOxxOm Dec 26 '18 at 07:01
  • That makes more sense! I will give that a go then, thanks again! – Castaire Dec 27 '18 at 02:47

0 Answers0