FontLoaderBypass

Injeção de Fontes em Userscripts

สคริปต์นี้ไม่ควรถูกติดตั้งโดยตรง มันเป็นคลังสำหรับสคริปต์อื่น ๆ เพื่อบรรจุด้วยคำสั่งเมทา // @require https://update.greasyfork.org/scripts/564164/1745532/FontLoaderBypass.js

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name               FontLoaderBypass
// @namespace          http://github.com/0H4S
// @version            1.1
// @author             OHAS
// @description        Injeção de Fontes em Userscripts
// @license            CC-BY-NC-ND-4.0
// @copyright          2026 OHAS. All Rights Reserved. (https://gist.github.com/0H4S/ae2fa82957a089576367e364cbf02438)
// ==/UserScript==

/*
    Copyright Notice & Terms of Use
    Copyright © 2026 OHAS. All Rights Reserved.

    This software is the exclusive property of OHAS and is licensed for personal, non-commercial use only.

    You may:
    - Install, use, and inspect the code for learning or personal purposes.

    You may NOT (without prior written permission from the author):
    - Copy, redistribute, or republish this software.
    - Modify, sell, or use it commercially.
    - Create derivative works.

    For questions, permission requests, or alternative licensing, please contact via
    - GitHub:       https://github.com/0H4S
    - Greasy Fork:  https://greasyfork.org/users/1464180

    This software is provided "as is", without warranty of any kind. The author is not liable for any damages arising from its use.
*/

(function() {
    'use strict';

    const API = {
        xhr:            (typeof GM_xmlhttpRequest   !== 'undefined') ? GM_xmlhttpRequest    : (typeof GM !== 'undefined' ? GM.xmlHttpRequest : null),
        listValues:     (typeof GM_listValues       !== 'undefined') ? GM_listValues        : (typeof GM !== 'undefined' ? GM.listValues : null),
        deleteValue:    (typeof GM_deleteValue      !== 'undefined') ? GM_deleteValue       : null,
        setValue:       (typeof GM_setValue         !== 'undefined') ? GM_setValue          : null,
        getValue:       (typeof GM_getValue         !== 'undefined') ? GM_getValue          : null
    };

    const FontLoaderBypass = {
        CACHE_PREFIX: 'flb_cache_',

        load: function(url, name, weight, style) {
            const isCss = url.includes('fonts.googleapis.com') || url.endsWith('.css');
            if (isCss) {
                this._processExternalCss(url);
            } else {
                if (!name) return;
                this.loadFontBase64(url, name, weight, style);
            }
        },

        clear: function(url) {
            const isCss = url.includes('fonts.googleapis.com') || url.endsWith('.css');
            if (isCss) {
                this._fetch(url, 'text')
                    .then(cssContent => {
                        const fontsFound = this._parseCssContent(cssContent);
                        if (fontsFound.length > 0) {
                            fontsFound.forEach(f => this._deleteSingleKey(f.src));
                        }
                    })
                .catch(() => {});
            } else {
                this._deleteSingleKey(url);
            }
        },

        clearAll: async function() {
            if (!API.listValues || !API.deleteValue) return;
            try {
                const keys = await API.listValues();
                keys.forEach(key => {
                    if (key.startsWith(this.CACHE_PREFIX)) {
                        API.deleteValue(key);
                    }
                });
            } catch (e) {}
        },

        _processExternalCss: function(cssUrl) {
            this._fetch(cssUrl, 'text')
                .then(cssContent => {
                    const fontsFound = this._parseCssContent(cssContent);
                    if (fontsFound.length === 0) return;
                    fontsFound.forEach(f => {
                        this.loadFontBase64(f.src, f.family, f.weight, f.style);
                    });
                })
            .catch(() => {});
        },

        _parseCssContent: function(cssText) {
            const results = [];
            const blockRegex = /@font-face\s*{([\s\S]*?)}/g;
            let match;
            while ((match = blockRegex.exec(cssText)) !== null) {
                const content = match[1];
                const familyMatch = content.match(/font-family:\s*['"]?([^'";]+)['"]?/);
                const styleMatch  = content.match(/font-style:\s*([a-zA-Z]+)/);
                const weightMatch = content.match(/font-weight:\s*([0-9a-zA-Z]+)/);
                const srcMatch    = content.match(/src:\s*url\((?:'|")?([^'")]+)(?:'|")?\)/);
                if (familyMatch && srcMatch) {
                    results.push({
                        family: familyMatch[1].trim(),
                        style:  styleMatch  ? styleMatch[1].trim()  : 'normal',
                        weight: weightMatch ? weightMatch[1].trim() : '400',
                        src:    srcMatch[1].trim()
                    });
                }
            }
            return results;
        },

        loadFontBase64: async function(url, fontFamilyName, fontWeight = 'normal', fontStyle = 'normal') {
            const cacheKey = this.CACHE_PREFIX + url;
            let blobFont = null;
            if (API.getValue) {
                try {
                    const cachedData = API.getValue(cacheKey);
                    if (cachedData) {
                        if (typeof cachedData === 'string' && cachedData.startsWith('{')) {
                            const parsed = JSON.parse(cachedData);
                            if (parsed && parsed.content) {
                                blobFont = this._base64ToBlob(parsed.content);
                            }
                        }
                        else if (typeof cachedData === 'string' && cachedData.startsWith('data:')) {
                            blobFont = this._base64ToBlob(cachedData);
                        }
                    }
                } catch (e) {}
            }
            if (!blobFont) {
                try {
                    const responseBlob = await this._fetch(url, 'blob');
                    blobFont = responseBlob;
                    const reader = new FileReader();
                    const base64Promise = new Promise((resolve) => {
                        reader.onloadend = () => resolve(reader.result);
                        reader.readAsDataURL(blobFont);
                    });
                    const base64Data = await base64Promise;
                    if (API.setValue) {
                        const storageObj = {
                            content: base64Data,
                            meta: {
                                fontName: fontFamilyName,
                                fontWeight: fontWeight,
                                fontStyle: fontStyle,
                                url: url
                            }
                        };
                        API.setValue(cacheKey, JSON.stringify(storageObj));
                    }
                } catch (err) { return; }
            }
            try {
                const arrayBuffer = await blobFont.arrayBuffer();
                const fontFace = new FontFace(fontFamilyName, arrayBuffer, {
                    weight: fontWeight,
                    style: fontStyle,
                    display: 'swap'
                });
                await fontFace.load();
                document.fonts.add(fontFace);
            } catch (e) {}
        },

        _deleteSingleKey: function(url) {
            if (API.deleteValue) {
                const key = this.CACHE_PREFIX + url;
                API.deleteValue(key);
            }
        },

        _base64ToBlob: function(base64) {
            const parts = base64.split(',');
            const mimeType = parts[0].match(/:(.*?);/)[1];
            const byteString = atob(parts[1]);
            const arrayBuffer = new ArrayBuffer(byteString.length);
            const int8Array = new Uint8Array(arrayBuffer);
            for (let i = 0; i < byteString.length; i++) {
                int8Array[i] = byteString.charCodeAt(i);
            }
            return new Blob([int8Array], { type: mimeType });
        },

        _fetch: function(url, responseType) {
            return new Promise((resolve, reject) => {
                if (!API.xhr) return reject();
                API.xhr({
                    method: 'GET',
                    url: url,
                    responseType: responseType,
                    onload: (res) => (res.status >= 200 && res.status < 300) ? resolve(res.response) : reject(),
                    onerror: () => reject(),
                    ontimeout: () => reject()
                });
            });
        }
    };

    const exportScope = (typeof unsafeWindow !== 'undefined') ? unsafeWindow : window;
    exportScope.FontLoaderBypass = FontLoaderBypass;

})();