Open in Current

Always open pages in the current window, instead of creating a popup.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Open in Current
// @namespace    http://tampermonkey.net/
// @version      0.1.0
// @description  Always open pages in the current window, instead of creating a popup.
// @author       PRO-2684
// @match        none
// @icon         none
// @license      gpl-3.0
// @grant        unsafeWindow
// ==/UserScript==

(function() {
    "use strict";
    const { name, version } = GM_info.script;
    const debug = console.debug.bind(console, `[${name}@${version}]`);

    const originalOpen = unsafeWindow.open.bind(unsafeWindow);
    function filteredOpen(url, target, features) {
        debug("Intercepted open", url, target, features);
        // Only keep noopener, noreferrer, and attributionsrc
        if (typeof features === "string") {
            // Example: window.open(url, "_blank", "width=780,height=490, top=100, left=380, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no")
            const filteredFeatures = features.split(",").map(f => f.trim()).filter(f =>
                f === "noopener" || f === "noreferrer" || f.startsWith("attributionsrc=")
            ).join(", ");
            return originalOpen(url, target, filteredFeatures);
        } else if (typeof features === "object" && features !== null) {
            const filteredFeatures = {};
            if (features.noopener) filteredFeatures.noopener = features.noopener;
            if (features.noreferrer) filteredFeatures.noreferrer = features.noreferrer;
            if (features.attributionsrc) filteredFeatures.attributionsrc = features.attributionsrc;
            return originalOpen(url, target, filteredFeatures);
        }
    };
    unsafeWindow.open = filteredOpen;
})();