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"]
}
}
javascript google-chrome google-chrome-extension google-chrome-app
add a comment |
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"]
}
}
javascript google-chrome google-chrome-extension google-chrome-app
@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
add a comment |
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"]
}
}
javascript google-chrome google-chrome-extension google-chrome-app
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
javascript google-chrome google-chrome-extension google-chrome-app
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
add a comment |
@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
add a comment |
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: ''});
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
add a comment |
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: ''});
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
add a comment |
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: ''});
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
add a comment |
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: ''});
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: ''});
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
@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