Wypok 4.0 Ulepszacz

Usprawnia Wykop 4.0 bo tak.

当前为 2023-01-20 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Wypok 4.0 Ulepszacz
// @namespace    http://tampermonkey.net/
// @version      0.6
// @description  Usprawnia Wykop 4.0 bo tak.
// @author       yojc
// @match        https://wykop.pl/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=wykop.pl
// @grant        none
// @license MIT
// ==/UserScript==

(function() {

    const debugFlag = false;

    function debugLog(arguments) {
        if (debugFlag) {
            if (Array.isArray(arguments)) {
                console.log(...arguments);
            }
            else {
                console.log(arguments);
            }
        }
    }

    const newSettings = {
        wypokHideWithoutTags: {
            text: "Ukrywaj wpisy bez tagów",
            default: false
        },
        wypokHideGreenEntries: {
            text: "Ukrywaj wpisy zielonek",
            default: false
        },
/*
        wypokHideGreensComments: {
            text: "Ukrywaj komentarze zielonek",
            default: false
        },
        wypokHideBlacklisted: {
            text: "Ukrywaj całkiem komentarze osób z czarnej listy",
            default: false
        },
*/
        wypokDelete: {
            text: "Usuwaj powyższą treść z DOM zamiast ukrywać",
            default: true
        },
        wypokMoveImagesInsideAnchor: {
            text: "Opakuj obrazki w tradycyjne linki",
            default: false
        },
/*
        wypokDisableLazyLoad: {
            text: "Wyłącz lazyload obrazków (zaznacz, jeśli jest problem z ładowaniem)",
            default: false
        },
        wypokDisableClickingOnComments: {
            text: "Wyłącz przejścia do wpisu/komentarza przez kliknięcie na treść",
            default: false
        },
*/
        wypokEnlargeSpaceBetweenEntries: {
            text: "Zwiększ odstęp między wpisami",
            default: false,
            style: "section.stream.microblog > .content > * + * { margin-top: 32px !important }"
        },
        wypokHideNewsButton: {
            text: "Ukryj pływający przycisk \"Nowości\"",
            default: false,
            style: ".content > .popper-button.onboarding-btn { display: none; }"
        },
        wypokHideAddButton: {
            text: "Ukryj pływający przycisk \"Dodaj\"",
            default: false,
            style: ".content > .popper-button:not(.onboarding-btn) { display: none; }"
        },
        wypokHideDoodle: {
            text: "Ukryj dodatkowe \"doodle\" logo w pasku tytułowym",
            default: false,
            style: "aside.doodle { display: none !important; }"
        }
    }

    function getSettingValue(name) {
        const localValue = localStorage.getItem(name);

        if (localValue) {
            return localValue === "true";
        }
        else if (newSettings[name]) {
             return newSettings[name].default;
        }
        else {
            debugLog("Setting ${name} is not set or present in the config!");
            return false;
        }
    }
    function setSettingValue(name, value) {
        debugLog(`Setting ${name} in localStorage to ${value}`);
        localStorage.setItem(name, value);
    }

    function refreshStyles() {
        let stylesTag;

        if (document.querySelector("#wypokStyles")) {
            debugLog(`Styles tag found`);
            stylesTag = document.querySelector("#wypokStyles");
        }
        else {
            debugLog(`Creating styles tag`);
            stylesTag = document.createElement("style");
            stylesTag.setAttribute("id", "wypokStyles");
            document.head.append(stylesTag);
        }

        let newStyle = "";

        for (const [key, value] of Object.entries(newSettings)) {
            if (value.style && getSettingValue(key)) {
                debugLog(["Applying style for: ", key, value.style]);
                newStyle += value.style;
            }
        }

        stylesTag.innerHTML = newStyle;
    }

    function appendSettings() {
        debugLog(`Appending settings`);

        const firstSettingsPane = document.querySelector(".display");
        const settingsPane = firstSettingsPane.cloneNode(true);
        const settingSwitchTemplate = settingsPane.querySelector(".form-group").cloneNode(true);

        settingsPane.setAttribute("class", "wypokScript")
        settingsPane.querySelector("h3 span").innerHTML = "<a href=\"https://greasyfork.org/en/scripts/458532-wypok-4-0-ulepszacz\" target=\"_blank\">Wypok 4.0 Ulepszacz</a>";

        // Disable spinner and stuff
        settingsPane.querySelector(".form-elements").classList.remove("waiting");
        for (const node of settingsPane.querySelectorAll(".simple-spinner")) {
            node.remove();
        }

        // Remove old switches
        for (const node of settingsPane.querySelectorAll(".form-group")) {
            node.remove();
        }

        // Add new switches
        for (const [key, value] of Object.entries(newSettings)) {
            const node = settingSwitchTemplate.cloneNode(true);
            node.querySelector("input").setAttribute("id", key);
            node.querySelector("input").checked = getSettingValue(key);
            node.querySelector("input").onchange = function() {
                setSettingValue(key, this.checked);
                if (value.style) {
                    refreshStyles();
                }
            }


            node.querySelector("label").setAttribute("for", key);
            node.querySelector("span").textContent = value.text;

            settingsPane.querySelector(".form-elements").append(node);
        }

        firstSettingsPane.parentNode.insertBefore(settingsPane, firstSettingsPane);
    }

    function hideOrRemove(node) {
        debugLog(["Removing or hiding node", node]);

        if (getSettingValue("wypokDelete")) {
            // Fixing broken Mikroblog navigation
            if (node.dataset.wypokDontRemove) {
                node.style.display = "none";
                node.innerHTML = "";
            }
            else {
                node.remove();
            }
        }
        else {
            node.style.display = "none";
        }
    }

    function filterEntries(nodes) {
        debugLog(`Filtering ${nodes.length} entries`);

        for (const node of nodes) {
            node.dataset.wypokChecked = true;

            let toBeRemovedFlag = false;
            const hasNoTags = (node.querySelectorAll(":scope > article .entry-content a[href^='/tag/']").length === 0);
            const isGreen = node.querySelector("header a.green-profile") !== null;

            debugLog(["hasNoTags", hasNoTags, "isGreen", isGreen]);

            if ((getSettingValue("wypokHideWithoutTags") && hasNoTags) || (getSettingValue("wypokHideGreenEntries") && isGreen)) {
                toBeRemovedFlag = true;
            }

            if (toBeRemovedFlag) {
                hideOrRemove(node);
            }
        }
    }

    function moveImageInsideAnchor(nodes) {
        debugLog(`Moving ${nodes.length} images to <a> tag`);

        for (const node of nodes) {
            node.dataset.wypokChecked = true;

            const container = node.querySelector("figure");
            const newLink = node.querySelector("figcaption a").cloneNode();
            const image = node.querySelector("figure > img");

            newLink.onclick = function(e) {
                e.preventDefault();
            }
            newLink.href = newLink.href.split("?")[0];

            newLink.append(image);
            container.prepend(newLink);
        }
    }

    // Observer

    let checkMirkoEntriesFlag = false;
    let appendSettingsFlag = false;

    let oldHref = document.location.href;
    let bodyList = document.querySelector("body")

    let observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            if (oldHref != document.location.href) {
                debugLog(`Document location changed`);
                oldHref = document.location.href;
                initPage();
            }
            else {
                if (getSettingValue("wypokMoveImagesInsideAnchor")) {
                    moveImageInsideAnchor(document.querySelectorAll(".entry-photo:not([data-wypok-checked])") );
                }

                if (checkMirkoEntriesFlag) {
                    debugLog(`Checking Mirko entries`);

                    const mirkoStream = document.querySelector("section.stream.microblog");

                    if ((getSettingValue("wypokHideWithoutTags") || getSettingValue("wypokHideGreenEntries")) && mirkoStream && !mirkoStream.classList.contains("waiting") && !mirkoStream.classList.contains("pending")) {
                        document.querySelector("section.entry:not(.reply):first-child").dataset.wypokDontRemove = true;
                        filterEntries(mirkoStream.querySelectorAll("section.entry:not(.reply):not([data-wypok-checked])"));
                    }
                }

                if (appendSettingsFlag && document.querySelector(".display")) {
                    debugLog("Clearing settings flag");
                    appendSettingsFlag = false;
                    appendSettings();
                }
            }
        });
    });

    let config = {
        childList: true,
        subtree: true
    };

    observer.observe(bodyList, config);

    function initPage() {
        debugLog(`Initialising page`);

        appendSettingsFlag = false;
        checkMirkoEntriesFlag = false;

        if (oldHref.startsWith("https://wykop.pl/ustawienia/ogolne")) {
            debugLog(`Setting appendSettingsFlag`);
            appendSettingsFlag = true;
        }
        else if (oldHref.startsWith("https://wykop.pl/mikroblog")) {
            debugLog(`Setting checkMirkoEntriesFlag`);
            checkMirkoEntriesFlag = true;
        }
    }

    function initPageOnce() {
        debugLog(`Initialising things to do only once`);
        refreshStyles();
    }

    initPage();
    initPageOnce();

})();