Search code examples
google-chrometampermonkey

Globally Disable Nag modals "Leave Site" "Changes may not be saved" Chrome


How can I globally and PERMANENTLY disable chromes ability to open

"Leave Site?" "Changes may not be saved" popups.

I want to close every tab of every chrome window and I know what I'm doing I don't need the nanny app to prevent my shutdown or get in my way when I'm trying to close out.

I tried tampermonkey scripts for disabling "onbeforeunload" events but they're not stopping this obnoxious behavior from chrome.


Solution

    1. Install a beforeunload listener before the page does by declaring @run-at document-start in the metadata block.
    2. Call stopImmediatePropagation to prevent the subsequently added page listeners from seeing the event.
    3. Also clear window.onbeforeunload.
    // ==UserScript==
    // @name        ignore beforeunload 
    // @match       *://*/*
    // @grant       none
    // @run-at      document-start
    // ==/UserScript==
    
    window.addEventListener('beforeunload', e => {
      window.onbeforeunload = null;
      e.stopImmediatePropagation();
    });