Comick Docs Fix

Replaces all instances of api.comick.fun with api.comick.dev and intercepts API calls

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Comick Docs Fix
// @namespace    https://github.com/GooglyBlox
// @version      1.0
// @description  Replaces all instances of api.comick.fun with api.comick.dev and intercepts API calls
// @author       GooglyBlox
// @match        https://api.comick.dev/docs*
// @grant        none
// @license      MIT
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    const originalFetch = window.fetch;
    window.fetch = function(...args) {
        if (args[0] && typeof args[0] === 'string') {
            args[0] = args[0].replace(/api\.comick\.fun/g, 'api.comick.dev');
        } else if (args[0] && args[0].url) {
            args[0].url = args[0].url.replace(/api\.comick\.fun/g, 'api.comick.dev');
        }
        return originalFetch.apply(this, args);
    };

    const originalOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function(method, url, ...rest) {
        if (typeof url === 'string') {
            url = url.replace(/api\.comick\.fun/g, 'api.comick.dev');
        }
        return originalOpen.call(this, method, url, ...rest);
    };

    function replaceURLs(node) {
        if (node.nodeType === Node.TEXT_NODE) {
            const originalText = node.textContent;
            const replacedText = originalText
                .replace(/https:\/\/api\.comick\.fun/g, 'https://api.comick.dev')
                .replace(/api\.comick\.fun/g, 'api.comick.dev');
            if (originalText !== replacedText) {
                node.textContent = replacedText;
            }
        } else if (node.nodeType === Node.ELEMENT_NODE) {
            if (node.tagName !== 'SCRIPT' && node.tagName !== 'STYLE') {
                for (let child of node.childNodes) {
                    replaceURLs(child);
                }
            }

            if (node.hasAttribute('href')) {
                const href = node.getAttribute('href');
                if (href.includes('api.comick.fun')) {
                    node.setAttribute('href', href.replace(/api\.comick\.fun/g, 'api.comick.dev'));
                }
            }

            if (node.hasAttribute('src')) {
                const src = node.getAttribute('src');
                if (src.includes('api.comick.fun')) {
                    node.setAttribute('src', src.replace(/api\.comick\.fun/g, 'api.comick.dev'));
                }
            }
        }
    }

    function processPage() {
        replaceURLs(document.body);
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', processPage);
    } else {
        processPage();
    }

    const observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            mutation.addedNodes.forEach(function(node) {
                if (node.nodeType === Node.ELEMENT_NODE || node.nodeType === Node.TEXT_NODE) {
                    replaceURLs(node);
                }
            });
        });
    });

    if (document.body) {
        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    } else {
        document.addEventListener('DOMContentLoaded', function() {
            observer.observe(document.body, {
                childList: true,
                subtree: true
            });
        });
    }
})();