common_libs_of_array

增加页面顶部底部按钮和一键下种按钮

Tính đến 12-08-2024. Xem phiên bản mới nhất.

Script này sẽ không được không được cài đặt trực tiếp. Nó là một thư viện cho các script khác để bao gồm các chỉ thị meta // @require https://update.sleazyfork.org/scripts/476583/1426578/common_libs_of_array.js

// ==UserScript==
// @name         common_libs_of_array
// @namespace    websiteEnhancement
// @author   jimmly
// @version      2024.7.22
// @description  增加页面顶部底部按钮和一键下种按钮
// @create         2023-9-21
// @include        *
// @grant         GM_getValue
// @grant         GM_setValue
// @grant         GM.getValue
// @grant         GM.setValue
// @license MIT
// @run-at document-idle
// ==/UserScript==

async function withJQuery(callback, safe, unsafeWindow) {
    if (typeof jQuery == "undefined") {
        let script = document.createElement("script")
        script.type = "text/javascript"
        script.src = "https://code.jquery.com/jquery-3.7.1.js"
        if (safe) {
            let cb = document.createElement("script")
            cb.type = "text/javascript"
            cb.textContent = `jQuery.noConflict();(${callback.toString()})(jQuery, window);`
            script.addEventListener("load", function () {
                document.head.appendChild(cb)
            })
        }
        else {
            let dollar
            if (typeof $ != "undefined")
                dollar = $
            script.addEventListener("load", function () {
                jQuery.noConflict()
                $ = dollar
                callback(jQuery, window)
            })
        }
        document.head.appendChild(script)
    }
    else {
        setTimeout(function () {
            // Firefox supports
            callback(jQuery, typeof unsafeWindow === "undefined" ? window : unsafeWindow)
        }, 30)
    }
}
// addStyle
function addStyle(css) {
    let s = document.createElement('style')
    s.appendChild(document.createTextNode(css))
    document.getElementsByTagName('head')[0].appendChild(s)
}

function getFilePathFromUrl(url) {

    try {
        url = new URL(urlString)
    }
    catch (e) {
        try {
            url = new URL(urlString, window.location.origin)
        }
        catch {

        }
    }
    return url.pathname.split('/').pop() // 或使用 url.pathname.split('/').pop() 如果url已经是一个URL对象
}
function getUrlWithoutHost(urlString) {
    let url = urlString
    try {
        url = new URL(urlString)
    }
    catch (e) {
        try {
            url = new URL(urlString, window.location.origin)
        } catch {

        }
    }
    return url.href.substring(url.origin.length)
}
class LRUCache {
    cache = []
    saveMapToLocalStorage() {
        // 将Map转换为普通对象
        const obj = this.cache ?? [];
        // 将对象转换为JSON字符串
        const jsonString = JSON.stringify(obj);
        // 存储到localStorage
        localStorage.setItem(this.key, jsonString);
    }
    loadMapFromLocalStorage() {
        // 从localStorage获取JSON字符串
        const jsonString = localStorage.getItem(this.key);
        if (!jsonString) {
            return []; // 如果没有数据,返回空Map
        }
        // 将JSON字符串转换为普通对象
        try {
            const obj = JSON.parse(jsonString)
            if (obj instanceof Array)
                return obj;
            return Object.keys(obj)
        } catch (error) {

        }
        return []
    }
    constructor(key = "default_key", capacity = 500) {
        this.key = key;
        this.capacity = capacity;
        this.cache = this.loadMapFromLocalStorage();
    }

    get(value) {
        let idx = this.cache.findIndex(v => v == value)
        if (idx > -1) {
            // 如果存在,则先删除再添加,以更新Map中的顺序(模拟最近最少使用)
            this.cache.splice(idx, 1)
            this.cache.push(value)
            return value;
        }
        return null;
    }

    put(value) {
        let idx = this.cache.findIndex(v => v == value)
        if (idx > -1) {
            // 如果键已经存在,先删除旧值
            this.cache.splice(idx, 1)
        } else if (this.cache.length >= this.capacity) {
            // 如果达到容量限制,删除最老的项
            this.cache.pop()
        }
        // 添加新值
        this.cache.push(value)
    }
    clearAllCache() {
        this.cache.length = 0
        this.saveMapToLocalStorage()
    }
}

window.__LRUCache = new LRUCache("__vistList", 2000)
window.addEventListener('beforeunload', function (event) {
    window.__LRUCache.saveMapToLocalStorage()
});
// createSuperLabel 创建超链接,不会被拦截
function createSuperLabel(url, id, downloadName, win) {
    win = win ?? window
    if (downloadName && localStorage.getItem("autoclosewindow") == 'Auto') {
        if (win.closeTimer) {
            win.clearTimeout(win.closeTimer)
        }
        win.closeTimer = setTimeout(function () {
            win.open("about:blank", "_self").close()
        }, 2000)
    }

    if (!id || id.length > 5)
        id = getUrlWithoutHost(url)
    // 防止反复添加
    if (!document.getElementById(id) && !__LRUCache.get(id)) {
        let tmpLink = document.createElement("a")
        __LRUCache.put(id, true)
        if (downloadName)
            tmpLink.download = downloadName
        tmpLink.setAttribute("href", url)
        tmpLink.setAttribute("target", "_blank")
        tmpLink.setAttribute("id", id)
        document.body.appendChild(tmpLink)
        tmpLink.click()
        return true
    }
    return false
}

function unique(arr) {
    let obj = {}
    return arr.filter(function (item, index, arr) {
        return obj.hasOwnProperty(typeof item + item) ? false : (obj[typeof item + item] = true)
    })
}

/// ignore \r \t \n space and caseinsitive
function a_Contains_b(a, b) {
    a = a.replace(/(\r\n|[\n\r\t ])/g, "").toLowerCase()
    b = b.replace(/(\r\n|[\n\r\t ])/g, "").toLowerCase()
    if (!!a && !!b && a.includes(b)) {
        return true
    }

    return false
}

function autoFind(funcIsRun, cmgId, selector, funcText, $, elBindOpen, unsafeWindow) {
    clearBtn = elBindOpen.clone().attr('id', 'clear').text('clear').appendTo(elBindOpen.parent())
    clearBtn.click(function () {
        window.__LRUCache.clearAllCache()
    })
    autocloseBtn = elBindOpen.clone().attr('id', 'autoclose').text(`${localStorage.getItem("autoclosewindow") ?? 'Keep'}`).appendTo(elBindOpen.parent())
    autocloseBtn.click(function () {
        localStorage.setItem("autoclosewindow", `${localStorage.getItem("autoclosewindow") == 'Auto' ? 'Keep' : 'Auto'}`)
        $(this).text(`${localStorage.getItem("autoclosewindow") ?? 'Keep'}`)
    })
    new Promise(resovle => resovle(new GM_config({
        id: `GM_config_${cmgId}`,
        title: 'javdb Configurable Options Script',
        fields: {
            asdf: {
                label: 'Search keys',
                type: 'textarea',
                rows: 30,
                cols: 50,
                default: '调J; 阴环;18岁;19岁;20岁;gvh;sm;tki;一字马;一线天;乳环;固定;圈养;奴隶;实录;性奴;拘;拘束;拷问;捆绑;挛;无毛;束;束缚;母G;母狗;痉;白虎;紧缚;萝莉;调教;软派;软体;缚;身动;绑;;肛塞;尾巴;极品;奴宠; 淫媚;尤物;凌辱;屈辱;少女;天然;素人;清纯;耻;調J; 陰環;18歲;19歲;20歲;18歳;20歳;21歳;一字馬;一綫天;乳環;圈養;奴隸;實錄;拷問;捆綁;攣;無毛;束縛;痙;緊縛;蘿莉;調教;軟派;軟體;縛;身動;綁;極品;奴寵;清純;恥;GIF;潮吹;陵辱;19歳;痉挛;弓背;高潮',

            },
            isRunInNewTabs: {
                options: ['Auto Run In New Tab', 'Not Run In New Tab'],
                label: 'Auto Run In New Tab?',
                type: 'radio',
                default: 'Auto Run In New Tab',
            },
        },
        events:
        {
            open() {
                let vals = unique(this.get('asdf').split(/[;;,,]/g)).join(';')
                this.set('asdf', vals)
            },
            save() {
                let vals = unique(this.get('asdf').split(/[;;,,]/g)).join(';')
                this.set('asdf', vals)
            },
        },
    })))
        .then((gmc) => {
            setTimeout(() => $(elBindOpen).click(() => gmc.open()), 500)
            return gmc
        })
        .then((gmc) => {
            (unsafeWindow || window).gmc = gmc
            if (!funcIsRun(gmc))
                throw new Error(`no run due to contion failed`)
            return gmc
        })
        .then((gmc) => {
            return { gmc, conf: gmc.get('asdf') }
        },

        )
        .then(({ gmc, conf }) => {
            console.log('config value of keys', conf)
            return ({ gmc, keys: conf.split(/[;;,,]/) })
        })
        .then(({ gmc, keys }) => {
            $(selector).each((i, element) => {
                let el = $(element)
                $.each(keys, (inex, key) => {
                    if (a_Contains_b(funcText(el), key)) {
                        let res = el.prop ? createSuperLabel(el.prop('href'), el.prop('href')) : createSuperLabel(el.attr('href'), el.attr('href'))
                        console.log(key, res, el.attr('href'))
                        return false
                    }
                })
            })
            return { gmc, keys }
        })
        .catch(e => console.log('error', e))
};