Issue with triggering chrome.browserAction.onClicked.addListener











up vote
0
down vote

favorite












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









share|improve this question
























  • @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

















up vote
0
down vote

favorite












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









share|improve this question
























  • @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















up vote
0
down vote

favorite









up vote
0
down vote

favorite











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









share|improve this question















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






javascript google-chrome google-chrome-extension google-chrome-app






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 22:04









Makyen

20k83665




20k83665










asked Feb 28 '17 at 21:48









Hamed

33




33












  • @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




















  • @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


















@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






@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 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










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: ''});





share|improve this answer





















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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42519382%2fissue-with-triggering-chrome-browseraction-onclicked-addlistener%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










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: ''});





share|improve this answer





















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















up vote
2
down vote



accepted










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: ''});





share|improve this answer





















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













up vote
2
down vote



accepted







up vote
2
down vote



accepted






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: ''});





share|improve this answer












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: ''});






share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 28 '17 at 22:11









Makyen

20k83665




20k83665












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


















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
















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




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




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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42519382%2fissue-with-triggering-chrome-browseraction-onclicked-addlistener%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python