2

I have a script that looks for a cookie to determine whether a user has an active session for a website. Based on this information, it shows the appropriate popup HTML page. I want this to run every time the extension icon is clicked, but it seems to only run once. I think I may be missing something.

cookieChecker.js

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.cookies.get({url: 'https://somesite.com', name: 'TOKEN'}, function(cookie) {
  if (cookie) {
   console.log('Cookie', cookie);
   var decoded = jwt_decode(cookie.value);
   var expired = isExpired(decoded);
   if (expired === false) {
    chrome.browserAction.setPopup({popup: 'loggedIn.html'});
      }
   else {
        chrome.browserAction.setPopup({popup: 'loggedOut.html'});
      }
    }
  else {
      chrome.browserAction.setPopup({popup: 'loggedOut.html'});
    }
  })

  function isExpired(token) {
    var date = getExpirationDate(token);
    if (date < (Date.now() / 1000)) {
      console.log(date, Date.now());
      return true;
    }
    return false;
  }

  function getExpirationDate(token){
    if (!token.exp) {
      return null;
    }
    var expDate = token.exp;
    return expDate;
  }
});

manifest.json

{
  "manifest_version": 2,

  "name": "Getting started example",
  "description": "This extension shows a Google Image search result for the current page",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png"
  },
  "permissions": [
"activeTab",
"cookies",
"storage",
"https://ajax.googleapis.com/",
"https://somesite.com"
  ],
  "background": {
    "scripts": ["jwt-decode.min.js","cookieChecker.js"]
  }
}
Makyen
  • 31,849
  • 12
  • 86
  • 121
Hamed
  • 71
  • 6
  • @wOxxOm The thing is, I want the script to query for the cookie of my site no matter what the current 'tab.url' is. Can this not be done? – Hamed Feb 28 '17 at 22:07

1 Answers1

2

The event browserAction.onClicked only fires if no popup is defined for the browser action button. Once you set a page for the popup using browserAction.setPopup({popup:... (or if you have a default_popup defined in your manifest.json), the browserAction.onClicked event will not fire for any subsequent user clicks on the browser action button. Instead, when the browser action button is clicked, your popup will be opened.

If you want to return to receiving browserAction.onClicked events, somewhere else in your code (e.g. in the popup's JavaScript), you would need to set the popup to '' with:

chrome.browserAction.setPopup({popup: ''});
Makyen
  • 31,849
  • 12
  • 86
  • 121
  • Omg thank you so much! I set the popup to '''' in javascript files for my loggedIn.html and loggedOut.html and it works! However, it now takes me two clicks on the button just to displaythe popup. The first click does nothing and the second click shows the popup. Any ideas why this could be happening? – Hamed Feb 28 '17 at 22:26
  • That is expected. The user interaction which would have opened the popup has already occurred by the time you define the popup. Defining the popup will make it open for all *subsequent* clicks. There is no programmatic way to open the actual popup. However, you could [open something that looks mostly like a popup and acts like one](http://stackoverflow.com/a/40296092/3773011). I need to enhance that code a bit and move it to one of the questions about programmatically opening the popup. While the question that answer is on is tagged firefox-webextensions, I did test the code on Chrome. – Makyen Feb 28 '17 at 22:47
  • @Hamed did you find a solution for the issue of it taking two clicks to open the popup? – colouredFunk Feb 13 '20 at 10:33