On Chrome Extension "popup" open/focus - javascript

How can I determine if the popup window was opened? E.g. someone clicked on the extension (chrome.browserAction), the window open and I want to show a loading sign inside the popup every time someone does that?

You can register onClicked event listener for browserAction.
chrome.browserAction.onClicked.addListener(function () {
// do what you want
});
You can also listen to window.onload event in popup page.
window.onload = function() {
// do what you want
};

Related

Is there any event to handle window alert popup's Button in js?

Here I have attached screenshot Attached screenshot for window alert popup. This is window popup it will appear while closing browser tab. If i click leave, the tab will close. If I click stay I need to do some other action. Is there any event to handle 'stay' button in this popup?.
Please share your view, Thank in advance.
Regards,
Marutharaj M.
I'd say its hard since there is no stay event for the beforeunload window.
A dirty fix could be to attach a timer to the beforeunload event:
$(window).on('beforeunload', function() {
setTimeout(function() {
$('#stay').text("Yes, you've decided to stay!");
}, 1000);
return "You save some unsaved data, Do you want to leave?";
});
JsFiddle

show fancybox popup before close browser window

When user close the browser window I want to show fancybox popup with confirm custom message, or my div with my style and image. How I can do it?
With Jquery, you have to bind the beforeunload event to a function.
Try this :
$(window).bind('beforeunload', function () {
// display fancybox popup
});

Detect tab/window activation in JavaScript

It seems that Google+ checks for notification updates when I activate the tab in Firefox
It'd show "0" every time I activate it, but change to a number of new notifications in a couple of seconds after that.
What's the mechanism allowing to tap into that event? Is there a specific DOM event for that? Or are they using something like onmouseover handler and just consider any kind of activity to be a sufficient indicator of tab activation?
Just a guess because I haven't all relevant browsers available for testing.
What about using the focus event on the window. Whenever a user clicks somewhere this is invoked but also on switching of tabs. To distinguish between a user's actions on the page and a user switching to the page you could check if the event's explicitOriginalTarget points to the window.
window.onfocus=function(event){
if(event.explicitOriginalTarget===window){
console.log('switched from tab');
}
}
There is Page visibility document, which describes document.onvisibilitychange event handler.
The usage
document.onvisibilitychange = function() {
console.log("Visibility of page has changed!");
};
Unfortunately there's no 100% accurate solution
onvisibilitychange correctly triggers on tab changes, but does not trigger on window changes (ALT+TAB) visibilitychange event is not triggered when switching program/window with ALT+TAB or clicking in taskbar
window.onfocus triggers when the document becomes focused. This works as expected if the tab's focus is already inside the web page, then it correctly triggers when window or tab becomes focused.
But if you have the focus on the URL bar, or in the console, you are already "out of focus", and when you get out of the window or tab and return, you will remain "out of focus", so this event won't trigger until you click inside the page, or navigate into it through TAB key
You can test below how each event triggers (click inside the white iframe to test onfocus/onblur events)
window.onfocus = () => console.log("focus");
window.onblur = () => console.log("out of focus");
document.onvisibilitychange = () => console.log("visibilityState: ", document.visibilityState);

How to create popup window when browser close

I want to open popup window when browser closed or closed tabs.
window.onUnLoad= function (evt) {
//your code goes here
window.open("yourpage/some link");
}
EDIT NOTE: Now it will be called on onUnload event of browser. Try now
Try window.onclose event:
Capture event onclose browser
https://developer.mozilla.org/en/DOM/window.onclose
How to capture the browser window close event?
References:
http://dotnetacademy.blogspot.com/2010/09/call-function-in-javascript-before.html
On below like you can find all events with example of window for javascript:
http://www.java2s.com/Tutorial/JavaScript/0380__Window/windowonUnLoad.htm

Javascript firefox extension to get the text around the link

Is it possible for me waiting for a user to click a link, and upon clicking the link the link's text would be obtained?
Maybe by using onClick?
If you mean handling the click event for the links in the page that the user is browsing then this is how:
// handle the load event of the window
window.addEventListener("load",Listen,false);
function Listen()
{
gBrowser.addEventListener("DOMContentLoaded",DocumentLoaded,true);
}
// handle the document load event, this is fired whenever a document is loaded in the browser. Then attach an event listener for the click event
function DocumentLoaded(event) {
var doc = event.originalTarget;
doc.addEventListener("click",GetLinkText,true);
}
// handle the click event of the document and check if the clicked element is an anchor element.
function GetLinkText(event) {
if (event.target instanceof HTMLAnchorElement) {
alert(event.target.innerHTML);
}
}
This is very simple using jQuery:
<script>
$(document).ready(function(){
$("a").click(function(){alert($(this).text());});
});
</script>
Of course, you'll probably want to do something other than alert the text.

Categories