我有一個腳本查找cookie以確定用戶是否有網站的活動會話。基於這些信息,它顯示了適當的彈出html頁面。我希望它在每次單擊擴展圖標時運行,但似乎只運行一次。我想我可能會錯過一些東西。觸發問題chrome.browserAction.onClicked.addListener
任何幫助,將不勝感激
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"]
}
}
@wOxxOm的事情是,我希望腳本能夠查詢我網站的cookie,而不管當前的'tab.url'是什麼。這不能做到嗎? – Hamed