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 >.<)