Filester Fixes

Small quality-of-life improvements for Filester, including proper album item links, download referrers, video thumbnails, and various tweaks.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Filester Fixes
// @version      1.1.1
// @description  Small quality-of-life improvements for Filester, including proper album item links, download referrers, video thumbnails, and various tweaks.
// @author       Murray
// @namespace    https://github.com/MurrayJunkie
// @homepageURL  https://github.com/MurrayJunkie/filester-fixes
// @supportURL   https://github.com/MurrayJunkie/filester-fixes/issues
// @match        https://filester.me/*
// @match        https://filester.sh/*
// @match        https://filester.si/*
// @match        https://filester.gg/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=filester.me
// @license      MIT
// @grant        unsafeWindow
// ==/UserScript==

(async function () {
    'use strict';

    const css = `
        .filester-fixed-item {
            position: relative; !important;
        }
    `

    document.head.insertAdjacentHTML("beforeend", `<style>${css}</style>`)

    function downloadWithFullReferrer(target) {
        const a = document.createElement("a");
        a.href = target;
        a.referrerPolicy = "unsafe-url"
        a.download = "";
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
    }

    // Patch window.open to insert a referrer url to downloads.
    ; ((windowOpen) => {
        unsafeWindow.open = function (url, target, features) {
            const u = new URL(url, location.href)
            if (target === "_blank" && u.searchParams.get("download") === "true") {
                downloadWithFullReferrer(url)
                return
            }

            return windowOpen.apply(this, arguments);
        }
    })(unsafeWindow.open)

    function waitForElement(selector) {
        return new Promise((resolve) => {
            const interval = setInterval(() => {
                const element = document.querySelector(selector);
                if (element == null) return;

                clearInterval(interval);
                resolve(element);
            })
        });
    }

    function addLinkToItem(item) {
        const [_, slug] = /window\.location\.href='(.*?)'/.exec(item.getAttribute('onclick'))

        const a = document.createElement("a");
        a.addEventListener("click", (e) => e.stopPropagation());
        a.href = slug;

        Object.assign(a.style, {
            position: "absolute",
            inset: 0,
            zIndex: 10,
            textDecoration: "none",
            color: "inherit"
        });

        if (getComputedStyle(item).position === "static") {
            item.style.position = "relative";
        }

        item.appendChild(a);
        item.classList.add("filester-fixed-item");
    }

    for (const item of document.querySelectorAll(".file-item")) {
        addLinkToItem(item);
    }

    const observer = new MutationObserver((mutations) => {
        for (const mutation of mutations) {
            for (const node of mutation.addedNodes) {
                if (node.classList && !node.classList.contains("filester-fixed-item") && node.classList.contains("file-item")) {
                    addLinkToItem(node);
                }
            }
        }
    });

    waitForElement("body").then((body) => {
        observer.observe(body, { childList: true, subtree: true });
    })

    if (location.pathname.startsWith("/d/")) {

        // Insert thumbail as poster
        const thumbUrl = document.querySelector("meta[property=\"og:image\"]").content
        const vidOverlay = document.querySelector("#videoPlayOverlay")
        vidOverlay.setAttribute("onclick", "window.loadVideo(); window.videoPlayer.autoplay = true;")

        vidOverlay.style.background = "none"
        for (const child of vidOverlay.children) {
            child.style.display = "none"
        }

        const vid = document.querySelector("#videoPlayer")
        vid.poster = thumbUrl
    }

    else if (location.pathname.startsWith("/f/")) {

        // Force the sort to be applied on page load, as the website doesn't do it
        const sortSelect = await waitForElement("#sortSelect")
        sortSelect.dispatchEvent(new Event("change", {
            bubbles: true
        }));
    }

    // Your code here...
})();