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"]
}
}