Furaffinity-Custom-Pages

Library to create Custom pages on Furaffinitiy

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.greasyfork.org/scripts/476762/1549463/Furaffinity-Custom-Pages.js

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Furaffinity-Custom-Pages
// @namespace   Violentmonkey Scripts
// @grant       none
// @version     1.2.2
// @author      Midori Dragon
// @description Library to create Custom pages on Furaffinitiy
// @icon        https://www.furaffinity.net/themes/beta/img/banners/fa_logo.png
// @license     MIT
// @homepageURL https://greasyfork.org/scripts/476762-furaffinity-custom-settings
// @supportURL  https://greasyfork.org/scripts/476762-furaffinity-custom-settings/feedback
// ==/UserScript==
// jshint esversion: 8
(() => {
    "use strict";
    var LogLevel;
    !function(LogLevel) {
        LogLevel[LogLevel.Error = 1] = "Error";
        LogLevel[LogLevel.Warning = 2] = "Warning";
        LogLevel[LogLevel.Info = 3] = "Info";
    }(LogLevel || (LogLevel = {}));
    class Logger {
        static log(logLevel = LogLevel.Warning, ...args) {
            if (null == window.__FF_GLOBAL_LOG_LEVEL__) window.__FF_GLOBAL_LOG_LEVEL__ = LogLevel.Error;
            if (!(logLevel > window.__FF_GLOBAL_LOG_LEVEL__)) switch (logLevel) {
              case LogLevel.Error:
                console.error(...args);
                break;

              case LogLevel.Warning:
                console.warn(...args);
                break;

              case LogLevel.Info:
                console.log(...args);
            }
        }
        static setLogLevel(logLevel) {
            window.__FF_GLOBAL_LOG_LEVEL__ = logLevel;
        }
        static logError(...args) {
            Logger.log(LogLevel.Error, ...args);
        }
        static logWarning(...args) {
            Logger.log(LogLevel.Warning, ...args);
        }
        static logInfo(...args) {
            Logger.log(LogLevel.Info, ...args);
        }
    }
    function extractParameter(url, parameterName) {
        const parts = url.split("?");
        if (parts.length > 1) {
            const params = parts[parts.length - 1].split("&");
            for (const param of params) {
                const [key, value] = param.split("=");
                if (key === parameterName) return {
                    key,
                    value: decodeURIComponent(value)
                };
            }
        }
    }
    class CustomData {
        constructor(document) {
            this.document = document;
        }
        removeDocumentSiteContent() {
            const siteContent = this.document.getElementById("site-content");
            if (null != siteContent) siteContent.remove();
            return siteContent;
        }
    }
    class CustomPage extends EventTarget {
        constructor(pageUrl, parameterName = "") {
            super();
            Object.setPrototypeOf(this, CustomPage.prototype);
            this.pageUrl = pageUrl;
            this.parameterName = parameterName;
            CustomPage.customPages.push(this);
            if ("" !== parameterName) Logger.logInfo(`New CustomPage at '${pageUrl}'='${parameterName}'`); else Logger.logInfo(`New CustomPage at '${pageUrl}'`);
        }
        get isOpen() {
            const url = window.location.toString();
            if (!url.includes(this.pageUrl)) return false; else if ("" === this.parameterName) return true;
            const parameter = extractParameter(url, this.parameterName), isOpen = (null == parameter ? void 0 : parameter.key) === this.parameterName;
            if (isOpen) Logger.logInfo(`CustomPage '${this.pageUrl}'='${this.parameterName}' is open`);
            return isOpen;
        }
        get parameterValue() {
            const parameter = extractParameter(window.location.toString(), this.parameterName);
            return null == parameter ? void 0 : parameter.value;
        }
        get onOpen() {
            return this._onOpen;
        }
        set onOpen(handler) {
            this._onOpen = handler;
        }
        static checkAllPages() {
            CustomPage.customPages.forEach((page => page.checkPageOpened()));
        }
        checkPageOpened() {
            if (this.isOpen) this.pageOpened(this.parameterValue, document);
        }
        pageOpened(parameterValue, openedPage) {
            const customData = new CustomData(openedPage);
            customData.parameterValue = parameterValue;
            const event = new CustomEvent("onOpen", {
                detail: customData
            });
            this.dispatchEvent(event);
        }
        invokeOpen(doc, parameterValue) {
            var _a;
            const customData = new CustomData(doc);
            customData.parameterValue = parameterValue;
            null === (_a = this._onOpen) || void 0 === _a || _a.call(this, customData);
            this.dispatchEvent(new CustomEvent("open", {
                detail: customData
            }));
        }
    }
    CustomPage.customPages = [];
    Object.defineProperties(window, {
        FACustomPage: {
            get: () => CustomPage
        }
    });
})();