Let Me Out | TM Edition

Prevents actions (like alerts) when navigating away from a page.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Let Me Out | TM Edition
// @namespace    tedbigham.blogspot.com
// @version      1.3
// @description  Prevents actions (like alerts) when navigating away from a page.
// @author       Ted Bigham
// @license MIT
// @match        *://*/*
// @icon         https://lh3.googleusercontent.com/UQFgp7s3-HgSxCENa2xNzGQAJhbA6r4VkY1iLAuZrtTBkEQ3cidC5CrkzpyjwYyPK9sXTAECWz1ab_ZJJyLnc96YoKQ=s60
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Before injection
    var beforeScript = document.createElement('script');
    beforeScript.textContent = `
        Window.prototype.addEventListener2 = Window.prototype.addEventListener;
        Window.prototype.addEventListener = function(type, listener, useCapture) {
            if (type != "beforeunload") {
                addEventListener2(type, listener, useCapture);
            }
        }
    `;
    (document.head||document.documentElement).insertBefore(beforeScript, (document.head||document.documentElement).firstChild);
    beforeScript.onload = function() {
        this.parentNode.removeChild(this);
    };

    // After injection
    var afterScript = document.createElement('script');
    afterScript.textContent = `
        function letmeout() {
            var all = document.getElementsByTagName("*");
            for (var i=0, max=all.length; i < max; i++) {
                if(all[i].getAttribute("onbeforeunload")) {
                    all[i].setAttribute("onbeforeunload", null);
                }
            }
            window.onbeforeunload = null;
        }
        letmeout();
        setInterval(letmeout, 500);
    `;
    (document.head||document.documentElement).appendChild(afterScript);
    afterScript.onload = function() {
        this.parentNode.removeChild(this);
    };

})();