Chrome extension popup closes unexpectedly then becomes unclickable - javascript

I have a Chrome extension I built that I've been testing. It runs fine on PC and never crashes. But on MAC, occasionally/randomly the popup window suddenly closes, presumably because some kind of unhandled exception occurred. I didn't click outside the popup or do anything that would naturally cause it to close. But I was doing things inside the popup window that calls functions, so I believe that some kind of error is occurring, but it only happens on Mac.
What is even worse, as soon as the extension popup closes, it cannot be opened again. Clicking on the extension icon (which normally causes the popup to appear as a small window just beneath the icon) does nothing. Click, click, click...nothing.
The problem is, the associated Chrome developer console window linked to the extension popup also closes whenever the popup closes, so I can't think of any way to discover the error that is happening.
Is there some way I can discover what is causing my extension popup to unexpectedly close? And/or any way to discover why it won't show the popup again after it closes unexpectedly?
Note: I'm all about showing source code, but in this case it would seem unhelpful because I don't know which code to show. My extension popup has thousands of lines of javascript, and it all works fine on a windows PC.
Any ideas?

Related

How to keep google chrome extension running even if I open an another tab?

I am running the chrome extension in browser action. As a developer, I am opening the inspect the popup and running, so it will stay open in the current tab. When I open the other tab, the "inspect the popup" which I open in the developer mode is getting closed. Is there a way to make the "inspect the popup" run continously even if I open the other tabs?
You can't; normally, any sort of focus loss will cause the popup to close.
While inspecting the popup, it's less sensitive to focus loss for ease of debugging, but changing the tab still causes it to close (and therefore, the Dev Tools window attached to it closes as well).
My guess is that some important expectations of the Chrome API are lost if the current tab changes after invoking the popup. For example, "activeTab" permission will break. That, or it may be simply a bug.
Bottom line: this will never actually happen in normal use, so this inability isn't that important. If you need to access multiple tabs, open them in separate window: switch to it won't cause an inspected popup to close.

Google Chrome closes when I open a new tab using my browser action Javascript, no idea why

I'm in the process of writing a Google Chrome extension, and I'm hung up on what should be a very simple task. I have a browser action that opens a popup, "popup.html", which then loads a small Javascript file, "popup.js". As soon as I get the DOMContentLoaded signal from the popup page, my Javascript opens a new tab with a certain URL. This behavioral quirk is by design - in some cases it will display a menu, and in other cases it will just open this URL.
I currently have some very simple code which is supposed to do this, but whenever it opens this tab, Chrome suddenly closes. I'm using a Mac (OS X Mavericks), so it doesn't crash entirely (still running), the window just closes. When I open it up again, the tab it was supposed to open is there, but the "popup.html" menu is awkwardly hanging over the UI still and won't go away.
Here is my complete popup.js file:
function stuff() {
window.open("http://stackoverflow.com/");
}
document.addEventListener('DOMContentLoaded', stuff);
I'm guessing that I may need to gracefully close my popup window before going to this link or something, but that seems like I'm overthinking it. After all, I've seen other extensions that have normal links in their browser action popups, and you can click those to open new tabs without any Javascript magic.
I've also tried using the chrome.tabs.create function, yet the same thing happens. The Chrome developer console doesn't show any errors. Thoughts?
Edit1: Just disabled all extensions and tried again. Still breaking. Going to boot up my Windows 8 box and see what happens on it.
Edit2: Works with a short delay before opening the window now on Mac, see my answer below.
Ok, I think I may have figured this out, but it's still weird. I did this on my Windows 8 PC, and the browser didn't crash, but the browser action popup didn't close the way it's supposed to either.
On a hunch, I altered my code on the Mac to give the browser some time to render the popup window:
function stuff() {
window.open("http://stackoverflow.com/");
}
setTimeout(stuff, 500);
Worked without a problem on the Mac this time. I'm guessing it's an issue with the browser being interrupted while rendering the popup window, but it's still weird. I would still appreciate an explanation from someone who actually knows what's going on here! :-)

Debugging JavaScript in popup windows on Chrome

What is a good way to debug a popup window in Chrome? I need to debug some code that is run when the window is opened and I was wondering if there was a better way to debug it than pressing f12 as soon as the window opens. I'm looking for something similar to using Visual Studio where you can open a window, set a breakpoint on some JS, then open the window back up and VS will break on that breakpoint without you having to do anything.
If it can be done some way on Firefox I'd switch to that, as long as I don't have to use IE.
Nowadays there's an option in the Chrome devtools settings. "Auto-open DevTools for popups".
Check that checkbox, then if you have devtools open on the parent window and that opens a popup or new tab devtools will automatically open in that window/tab.
Short Answer:
"Ctrl + click"
Long Answer:
I had the same problem. It seems that it's a bug in Google Chrome and there's nothing we can do about it until it is fixed in future versions. I found a way around it, though, which is not an actual solution but solved my problem.
1) Close the popup window.
2) Open it again using "Ctrl + click" instead of just clicking on the link to the popup window.
3) The window will be opened in a new normal tab.
4) Now you can press F12 and go to debug mode.
Hope this solves someone else's problem too.

Keep Chrome extension popup open while navigating to a new page

I'm developing a Google Chrome extension with a popup (it's a browser action), and it changes the location of the page, which makes the popup disappear. How can I make it stay between page reloads?
You can't. browserAction popups are closed on any activity outside of the popup. You could potentially use Desktop Notifications though.
You'll need to use Background Page, and pull information from it every time you load the popup. Background pages run whether or your extension is currently being used or not.
Not sure if this helps, but from the FAQ:
Can extensions keep popups open after the user clicks away from them?
No, 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.
I had to implement a workaround for this as well. In my case, I was trying to use auth0's loginWithPopup. When the popup closed, it returned focus to the chrome window instead of the extension which caused the extension to close before authorization fully completed. I worked around it by opening a new window which acts as a barrier of sorts to prevent chrome's focus from going back to the window the extension was opened from. Anyway, just wanted to put it out there in case it helps someone in the future.

JavaScript close window for external URL

In my page when I click on a link a popup will be opened. In that popup, I have the close button. To close the window I am using simple JavaScript function as window.close().
This is working fine.
Now when I copy the URL of the popup link and open it in new window, I am not able to close the window.
In Firefox when using Firebug the warning given is:
'Scripts may not close windows that were not opened by script.'
Please help me out on any other alternative.
Firefox seems to answer that question: 'Scripts may not close windows that were not opened by script.'
This is a security measure. Imagine every site could close every other page you have open, that wouldn't work very well would it?
That's why only a parent window may close its children windows.
There may be a setting Firefox that allows windows not opened by script to be closed by script, but even if there is, what chance is there that your visitors will all have enabled this setting?
You can't work around this problem, it is how Firefox (and certainly other browsers) works. The only answer is to change your approach.
Why are you using windows as popups anyway? This has not been recommended for some time now and is mostly frowned upon. Popups that are actual windows may be blocked by popup-blockers.
You should probably use a modal popup instead of a window

Categories