Detecting when a Chrome Extension popup is opened - javascript

I am trying to wire some analytics into the onStartup event of my Chrome Extension per the docs. However, the event never seems to fire when the extension is opened via the icon in the browser.
Note that the onInstalled event in the below code fires as expected when the extension is installed, reloaded, etc.
chrome.runtime.onInstalled.addListener(function(details) {
console.log('Extension installed: ' + details.reason);
});
chrome.runtime.onStartup.addListener(function() {
console.log('Extension started');
});
Note I'm running Chrome v37 - the onStartup event has been available since v23.

You are trying to invoke code when a popup is opened. This is not the same as "starting" the extension - the chrome.runtime.onStartup event normally fires once per browser launch.
chrome.browserAction.onClicked event will not fire when a popup page is set; instead, you need to execute some code in the popup page itself (and that code will be executed each time the popup is opened).
You can simply send your analytics event from the popup page itself. Or, if you prefer sending it from the background page, you just need to message it.

Related

Is there a way to perform action when HTML5 notification dissappears?

According to MDN, the onclose event for Notification API is obsolete:
The following event handlers are still supported as listed in the browser compatibility section below, but are no longer listed in the current spec. It is safe therefore to assume they are obsolete and may stop working in future browser versions.
Notification.onclose
A handler for the close event. It is triggered when the user closes the notification.
Notification.onshow
A handler for the show event. It is triggered when the notification is displayed.
I have script that, under certain circumstances, generates following notification:
X has happened. Click here to deal with it, ignore or close this notification to continue running.
If the user clicks the notification, script is stopped and user can deal with the situation. If the notification is not clicked, the script will continue with it's work after the notification is closed/disappears.
How to do that now that onclose event is deprecated?
Also according to MDN, you should be sending your own close commands because Chrome doesn't do that on its own. It would be trivial to co-opt that to handle the event.
Something like this:
let notification = new Notification('Test Notification');
setTimeout(close(notification), 5000);
function close(n) {
n.close.bind(n);
// your other code...
}

Why beforeunload event is not working when i'm closing the browser

Below is my code...
window.addEventListener("beforeunload", function (e) {
Actions.SendTimeOutEMail();
});
Actions.SendTimeOutEMail() is a flux action and will call an ajax function.
When I close the window tab in browser, it will run "Actions.SendTimeOutEMail"
, I checked it from my back-end server log.
However, if I close the browser itself, the event will not fire and I can't see the output of the event in the server log.
Why this behavior is different between click close of a tab and click close of browser?

pjax event listeners not working in chrome extension

I am writing an chrome extension related with Github. I want to trigger a function when user click to public activity tab on Github user profile page.I am adding pjax success event listener in extension but not triggering anytime.
I am adding jquery.pjax.js to manifest.json and adding below code in contentscript.js but function not calling when pjax request succeed:
$(document).on('pjax:success', function() {
console.log("Hello");
});
How can I run function when pjax succeed in chrome extension?

hotkey plugin opens new window even if pop-ups are blocked?

I want to open new window if "F2" pressed. Below code gives me newWindow is null error message in firefox. If I don't use pop-up blocker it works. The same in IE. It work in chrome even with pop-up blocker on.
using jstree pre 1.0 stable
hotkeys: {
"f3" : function () {
url = "http://www.vse.cz";
var newWindow = window.open(url, '_blank');
newWindow.focus();
return false;
},
Q1: Can I make it work for all browsers so users don't have to change their settings when using hotkeys plugin?
Q2: How come Using JavaScript instead of target to open new windows works without any troubles in firefox? Is that because it's a link and not using hotkeys plugin?
My understanding is that the script from above page somehow
manipulates what happens
when user clicks a link. It changes the properties of the click so
browsers "don't know" that it's new window so pop-up blocker is
bypassed.
In my case I use pure js function triggered by something else, not by
a user click. And that 'my function' doesn't changes properties of any html objects. I think this is the difference. I am not sure if I am
right here.
Unfortunately, there's nothing you can do to open a new window on a keypress (other than disabling the popup blocker).
The way that the popup blockers in IE, Firefox and Chrome work (from a high level) is by the browser (upon encountering a call to window.open) walking up the JavaScript call stack to determine if the current function is—or was called by a function that is—an event handler. In other words, it finds out if the current function is executing because the user did something that triggered a DOM event.
If so, then the popup is allowed; otherwise it is blocked. However, the question of which events qualify as "popup-allowing" vary by browser. By default in Mozilla, only change, click, dblclick, mouseup, reset, and submit qualify. (I assume IE is similar.)
Functions that are event handlers for any other type of event – such as keydown/keyup/keypress in your case – do not qualify for special popup-allowing treatment, which means your popup is blocked and is why your call to window.open returns null.
Chrome, however, does consider the keydown event eligible for allowing popups to be opened, which is why your script works in that browser.
Here's a reduced example to demonstrate how this works. This demo:
Defines a function called spawn() which calls window.open to open a popup.
Calls spawn() immediately as the page is loaded. This is blocked by all browsers since the call is made from the global scope; it is not called from an event handler.
Attaches a function to window.onkeydown which calls spawn(). If you press any key in Chrome, the popup will open because it allows popups from keydown handlers. In IE and Firefox, the popup will be blocked becuase those browsers do not allow popups from keyboard events.
Attaches an event handler to the link which calls spawn(). When you click the link, the popup will be allowed in all browsers because the call to window.open can be traced back to an event handler for a click event.
As you can now see, nothing goes on to manipulate event properties or "trick" the browser in to not knowing that there's a new window. The behavior of popups being allowed to open from link clicks is by design, the theory being that if you've clicked on something, it's likely that you want to see whatever is in the popup. However, when a call is made to window.open from a place where you've not done anything (such as the global scope), it's likely you do not have any interest in whatever [ad] is in the automatically-launching popup.
In this way, popup blockers prevent annoyances (automatically launching ads) while still allowing pages to open popups at the user's request.

Firefox addon code for every tab open

I'm attaching some functionality to javascript by doing a firefox addon. However when coding in chrome and listening to the load event in the chrome overlay triggers for every loaded tab, but the "content" variable only points to the tab currently in the foreground.
How can I get the content of every tab upon document load from a firefox addon?
Assuming you are using code like this to be told every time a new page loads (which is what you really want to use if you aren't), aEvent.originalTarget is a reference to the document that the event was for.

Categories

Resources