I'm developing a Chrome extension and I wanted to know if it is possible to close a popup by simply clicking again the icon that lets you open the popup: I tried anything but it looks like you must click elsewhere to close it. The docs states the onClicked event is:
Fired when a browser action icon is clicked. This event will not fire if the browser action has a popup (http://developer.chrome.com/extensions/browserAction.html#popups).
Thanks in advance.
[UPDATE] I tried the following and it half (!) works:
1. in popup.html I link popup.js;
2. popup.js reads the value of a variable contained in background.js;
var currentStatus = chrome.extension.getBackgroundPage().open;
if(currentStatus==0){
chrome.extension.getBackgroundPage().open=1;
}else{
chrome.extension.getBackgroundPage().open=0;
window.close();
}
What happens: the first click opens the app, the second closes it, BUT it remains a micro popup with no content upon the icon. If I remove that, I reached my goal.
The onClicked event is called if your extension's browser action does not define default_popup in the manifest. That note from the documentation isn't about whether the popup is currently open.
If the manifest defines default_popup then clicking the button again closes and reopens the plugin. The mousedown closes and the mouseup opens. (So clicking on the button and dragging away and releasing the mouse does close the popup, not that anyone should do this.)
I recommend setting default_popup and making a button in the html for the popup that closes the popup with window.close;, or find a point in your popup's use case where closing makes sense.
Well It has been a long time, and the issue/bug still persists on Chrome browsers. I've found a workaround, it's not great, but it does what I need - closes the window on a second icon click. Here's what I did in the popup javascript file:
if(localStorage.getItem('firstClick')==='true'){
localStorage.setItem('firstClick', 'false');
window.close();
}
else {
localStorage.removeItem('firstClick');
localStorage.set('firstClick', 'true')
}
Related
I use the following to open a new window:
var win = window.open('URL HERE' , type ,'width=1100,height=500,left=200,top=200');
The reason I assign a variable to it is because it allows me to do other things with the variable such as actions on close (which I am not using in this case).
The window opens just fine. I can do everything I need to do inside the window. But the problem occurs when I close the window: The window closes but reopens automatically when i click anywhere on the parent page.
This happens if I click the browsers "x" on the new window, or even if I click a link that that includes onclick="window.close()", and it happens every single time I try this. It's not sporadic at all.
What am I missing?
Found it!
Avoid using onchange and onblur together - even though some people post that this fixes some onchange problems that occur on ios devices.
At some point in the past I was dealing with an issue with iphone and ipad not opening a window when running an onchange event. Everyone kept posting about using onblur events on those devices. That didn't fix the problem but I had left an onchange and an onblur event inside the select dropdown by accident not realizing that was what it was doing.
What was happening was that I was running the window.open when the selection changed, and then it was rerunning window.open when the window lost focus which resulted in another window opening when refocusing parent page.
I am making a Chrome extension with a "default_popup":"popup.html".
Documentation of chrome.browserAction.onClicked says:
Fired when a browser action icon is clicked. This event will not fire
if the browser action has a popup.
I know browserAction.onClicked will not fire in my extension. But can i disable popup so that browserAction.onClicked can fire?
Conclusion : While my extension is running, I want to disable popup and have a chrome.browserAction.onClicked in my background.jsso that later some time I can call browserAction.onClicked. Is it possible? How? Also I would like to know If I can do the reverse meaning disable browserAction.onClicked and enable popup.
You can disable the popup (automatically enabling the dispatch of onClicked events) by setting the popup path to an empty string:
chrome.browserAction.setPopup({popup: ""});
Likewise, you can enable the popup afterwards by providing a valid path:
chrome.browserAction.setPopup({popup: "popup.html"});
You can keep a listener to onClicked regardless - it's just that the event is not always dispatched.
Please note: you can't have the popup disabled, capture the click and then show the popup - you can only change what happens at next click. If you want both a popup and some event in the background, it's best to simply message background from the popup.
I haven't found a single answer able to tell me what's the right way to open a popup.
Times have changed, and popups have been mostly replaced with fancybox-like boxes. However, there are still times when popups are needed.
For those cases, I don't want my popup to be blocked by the browsers.
What's the right way to open a popup without it being blocked? Even if it opens a new tab in the browser. I just want my popup to be open, and have control of it from the parent or vice versa.
Popup blockers will block any popup, unless it is opened because of an user action.
If the user clicks on a link, and a popup is opened in the click listener of that link, the popup blocker knows the user want to open something and will not (or should not) block the popup.
What you cannot do:
open a popup when the page is opened or closed
open a popup after a certain interval
open a popup after something asynchronous happens
What you can do:
open a popup in the on click listener
using target="_blank" in a anchor tag
You can access both windows with JavaScript variables:
if you use window.open, the parent can have a reference to the popup by assigning the result of window.open to a variable. Check out this article at W3Schools.
If the popup needs to have access to the window who has opened it, you can use window.opener. Check out this question.
try this, it works for me
$('#myButton').click(function () {
var redirectWindow = window.open('http://google.com', '_blank');
redirectWindow.location;
});
Js fiddle for this is here https://jsfiddle.net/safeeronline/70kdacL4/2/
if you want to open new tab after ajax call see this fiddle http://jsfiddle.net/safeeronline/70kdacL4/1/
If I open my extension popup then I open another window or tab following the popup does not stay open if I return to it.
Is there a way to force it so the popup stays open?
As a user, you currently cannot force the the popup to stay open. That is a UI decision the UI team made. If you want to want to force a setup, you can have other way to show this by changing the popup icon, open a new tab when it requests, or new popup view for registration.
As a developer, inspect the popup, and it will stay open.
You cannot stop the Chrome pop-up from closing, unless you're in developer mode. You could consider this alternative, though:
Launching a normal pop-up instead:
In your popup.html file, load a Javascript file that runs this:
var popupWindow = window.open(
chrome.extension.getURL("normal_popup.html"),
"exampleName",
"width=400,height=400"
);
window.close(); // close the Chrome extension pop-up
This will open the file normal_popup.html in your extension in a normal pop-up window, which won't close when it loses focus. Because the name parameter is the same, the pop-up window will get reused if the user launches popup.html again.
In an answer to a FAQ here: https://developer.chrome.com/docs/extensions/mv3/faq/#faq-persist-popups
Popups automatically close when the user focuses on some portion of the browser outside of the popup. There is no way to keep the popup open after the user has clicked away.
As others have said, this is a deliberate limitation of popup UI.
Instead, you could inject some HTML into the page which loads the content you want in your popup into an element which hovers over the existing page. You will have to implement the close functionality yourself, but it will persist.
Have a look at e.g. how keyframes.app has done it: https://github.com/mitchas/Keyframes.app/blob/master/Keyframes.app%20(Extension)/src/inject/ui.js
If you enable panels at "chrome://flags/#enable-panels" you can use something like:
chrome.windows.create({
url:"popup.html",
type:"panel",
width:300,
height:200
});
to open a panel window instead which will stay on top all the time as long as you don't move it from the bottom of the screen.
Best way to workaround this is to :
Right-Click inside the popup
Click: Inspect
Or just press CTRL+Shift+I
A new window will open with the Developer Tools... just keep that window open and the popup will never close.
This answer to How do I prevent Chrome developer tools from closing when the current browser window closes? what very helpful in my case:
Not a perfect solution, but you can add breakpoints on the events Window.close and unload by turning on the checkboxes at:
Developer tools -> "Sources" tab -> Event Listener Breakpoints -> Window -> close
And
Event Listener Breakpoints -> Load -> unload
Try to mark both and see which one works best for you
I have a popup window and in that page I have the following code in the body.
<img src="...something"/>
The purpose is to have this popup window close when a user clicks on the image link, and to open a new page and be directed to http://www.example.com.
It works in IE and Chrome, but not in Firefox. The popup window closes but no new window is opened.
Any ideas?
Yes, I can repro this - interesting. setTimeout works around it:
onClick="javascript: setTimeout(window.close, 10);"
I can only guess that once the window closes (which happens before the hyperlink is followed) Firefox stops processing that page.
Edit: better make it 10ms delay - with 1ms Chrome doesn't close the window.
The question is actually solved for the opener but it didn't help my issue (not wished: the new windows under firefox keep the same size as the current popup).
So I find following solution:
function whenClicked()
{
window.close();
opener.location.href = "http://www.example.com";
}
or this if the ppage should open in a new tab:
function whenClicked()
{
window.close();
opener.open(http://www.example.com, '_blank');
}
When you add some functionality to an element's click event via javascript, that functionality is executed before the default click event (in this case, opening a new page), in order to allow for the possibility of intercepting and overriding the default event. The default behavior will only execute when and if the event returns a boolean value of true.
In this case, the additional functionality would be to close the window and my guess is that Firefox chooses to interpret this as "we're all done here", so the click event never returns true, and thus the new page never gets opened.
Evgeny's suggestion of using a short timeout would allow the click event to return true before the window is closed, thus allowing the new window to open.