0

I have created an extension that alternates popups based on if the user is logged in a specific website. I used chrome.tabs.onHighlighted and chrome.tabs.onActivated to check in the background page if the user is still logged in, but the change would only happen after I click 2 times on different tabs to change the highlited one (I'm using a promise in there so that might be the reason for the need of an extra click). Is there any other way to force the background page to always be aware? Thank you

chrome.tabs.onHighlighted.addListener(function (evt) {
var token = new Promise(function (resolve, reject) {
    try {
      // Get the current tab
      chrome.tabs.query({ active: false }, function (tabs) {
        var t = JSON.stringify(tabs);
        var tabs = JSON.parse(t);
        let found_tab = [];
        for (let i = 0; i < tabs.length; i++) {
          if (tabs[i].url.includes("https://example.xyz")) {
            found_tab = tabs[i];
          }
        }
// Execute script in the current tab
        chrome.tabs.executeScript(
          found_tab.id,
          { code: ` JSON.parse( localStorage.getItem('userCredentials' ))` },
          function (result) {
            if (
              result == null ||
              result == undefined ||
              result[0] == null ||
              result[0] == undefined
            )
              reject(null);
            resolve(result);
          }
        );
      });
    } catch (err) {
      // Log exceptions
      reject(err);
    }
  });

function getAuthToken() {
    token.then(function (value) {
      auth_token = value[0].auth_token;
      username = value[0].user_username;
    });
  }
  getAuthToken();
  if (auth_token === undefined || auth_token === null) {
    chrome.browserAction.setPopup({
      popup: "login.html",
    });
  } else {
    chrome.browserAction.setPopup({
      popup: "popup.html",
    });
  }

  fetch("https://api.example.xyz/token/user/" + username)
    .then(handleResponse)
    .then((response) => {
      if (JSON.parse(JSON.stringify(response.tokens)).includes(auth_token)) {
        console.log(auth_token);
      } else {
        auth_token = null;
      }
    });
});
Kevin Ku
  • 53
  • 7
  • Promise itself is not causing any problems just like there's no such thing as "awareness" of the background script. The problem is that your code is either making incorrect assumptions or these events are not relevant to the task you want to achieve. The question isn't really answerable unless you add [MCVE](/help/mcve). – wOxxOm Aug 12 '20 at 08:39
  • Thanks for the reply, I'll try to explain further, if it's not enough please ask me for more. So in the background page I am making an api call to check if the token that I have saved in a local variable still exists. If it doesn't I change the variable to null which triggers an if-else in the same spectrum that changes the extension popup. All those things are in a chrome.tabs.onHighlighted listener, so for them to work the user needs to change the highlighted tab which is not optimal. I'll also edit the question and add some code. Thank you – Kevin Ku Aug 12 '20 at 15:38
  • This fragment of code didn't clarify what you're doing there. I don't see why you're using onHighlighted or onActivated at all. If the goal is to detect the moment the user signs in or out then you need to intercept this exact action. The exact API or event depends on how signing in/out is performed. For example, if it's a value in `localStorage` of the web page then you can set up a hook in page context or you can check it periodically in the content script and send a message on change. – wOxxOm Aug 12 '20 at 15:55
  • I used onHighlighted so I can force the background page to check if the token still exists. I tried to do that in the content script but I couldn't find a way to always keep it open so it can hold a variable with the token. The reason i need a variable is because I can't have access on localStorage after the website tab is closed. If there is a way I can do that I would love to try it. Thank you again – Kevin Ku Aug 12 '20 at 16:38
  • You can save it into `chrome.storage`. – wOxxOm Aug 12 '20 at 16:42
  • And how would I force the content script to keep checking if the token still exists? – Kevin Ku Aug 12 '20 at 16:48
  • You can use a timer or set a hook for localStorage in [page context](/a/9517879). – wOxxOm Aug 12 '20 at 16:49
  • Ok I will try everything you suggested. Thank you – Kevin Ku Aug 12 '20 at 16:54

0 Answers0