Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't try to suspend tabs with the "window.onbeforeunload" event #272

Open
rodrigograca31 opened this issue Aug 9, 2015 · 9 comments
Open
Labels

Comments

@rodrigograca31
Copy link

Some pages have code like this one:

window.onbeforeunload = function() {
    return "Are you sure?";
};

When "The Great Suspender" tries to suspend them it fires the event.... And focus the page.... (because it's like an alert)

More information: http://www.w3schools.com/jsref/event_onbeforeunload.asp

Example:

@AndrewJDR
Copy link
Contributor

+1

@AndrewJDR
Copy link
Contributor

AndrewJDR commented Mar 16, 2017

Here's an example of a page that recently stole focus from me when it got suspended:
https://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

I checked the event listeners registered to the page using dev tools. It has a number of unload events registered:
unload-event-listeners
Note it doesn't actually pop up a dialog on unload, but whatever it is doing on unload is perhaps sufficient for it to steal focus away. It's annoying when this happens, because sometimes I'm in the middle of typing something on a page that's buried somewhere in my dozens of open tabs, and I suddenly get switched away from when I was working on and have to find the tab I was working with again.

I'm actually happy to write a patch to fix this bug, but I'd like some suggestions on the best way to go about it. I'm guessing a lot of pages have unload event handlers, so simply not suspending such pages would probably result in many pages remaining unsuspended.

Does anyone know if there's a way from an extension to unbind some event handlers from a page? This way, we could just unbind all the unload event handlers before suspending the tab.

@AndrewJDR
Copy link
Contributor

Going to call out @deanoemcke in case he has any suggestions... Thanks!

@rodrigograca31
Copy link
Author

Extensions have access to the same window object as the tab? Right?
If so, you could just do window.onbeforeunload = false; before suspending that tab....

@AndrewJDR
Copy link
Contributor

Ah, makes sense. I haven't done much extension development so I wasn't sure how it worked.

However, I just tried reproducing the issue and was not able to. So I may be barking up the wrong tree here. I'm now not sure what causes these pages to steal focus when they suspend. Hopefully I will find a reproduction case some day and fix it.

AndrewJDR added a commit to AndrewJDR/thegreatsuspender that referenced this issue Mar 16, 2017
Tabs can sometimes steal focus on suspension.
Presumably, this is happening due to onunload/unbeforeunload even handlers.
This change removes the event handlers before suspending the tab.
Let's hope it works!
@AndrewJDR
Copy link
Contributor

@rodrigograca31 I'm testing this change:
AndrewJDR@274ba3c

Unfortunately, it doesn't seem to work for your example page:
https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onbeforeunload
If I click run, then let the tab get suspended, it still pops up the dialog. Perhaps I've added my code in the wrong place. Feedback welcome.

@rodrigograca31
Copy link
Author

This issue is from 2015... I don't remember the exact problem but:

If you go to: https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onbeforeunload
And try to suspend it it shows an alert message....
(Chrome 58)

That probably also happens if the extension trys to suspend it after X minutes...
I think I've not experienced that in a while maybe it's already fixed but as I said this is from 2015.....

@deanoemcke
Copy link
Collaborator

deanoemcke commented Oct 24, 2017

I agree this is annoying. But I could not find a way to turn these popups off either.
One hesitation I have about implementing such a feature (assuming I even could) would be that often these types of popups are there for a reason. Because you really dont want the user to lose a lot of typed information for example.

@AndrewJDR
Copy link
Contributor

is it possible to detect whether a page has any onunload listeners registered?

If so, another idea would be to have an option to allow users to simply not suspend pages that have onunload/onbeforeunload listeners registered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants