Greasy Fork

来自缓存

Greasy Fork is available in English.

Fandom to BreezeWiki

Redirects from Fandom to BreezeWiki

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Fandom to BreezeWiki
// @namespace    FandomToBreeze
// @version      0.0.2
// @description  Redirects from Fandom to BreezeWiki
// @author       frostice482
// @match        https://*.fandom.com/wiki/*
// @icon         https://fandom.com/favicon.ico
// @grant        GM.xmlHttpRequest
// @connect      z.opnxng.com
// @connect      breezewiki.com
// @connect      antifandom.com
// @connect      breezewiki.pussthecat.org
// @connect      *
// @run-at       document-body
// @license      MIT
// ==/UserScript==

const mirrors = [
    'https://z.opnxng.com',
    'https://breezewiki.com',
    'https://antifandom.com',
    'https://breezewiki.pussthecat.org'
]
const abortTime = 500

// handle aborting redirection
const abort = new AbortController()
document.addEventListener('pointerdown', () => abort.abort(), { signal: abort.signal })
document.addEventListener('click', () => abort.abort(), { signal: abort.signal })
document.addEventListener('keypress', ev => ev.code === 'Escape' && abort.abort(), { signal: abort.signal })

// redirection
const workingMirror = getWorkingMirror(abort)
const timeout = setTimeout(async () => {
    const targetURL = await workingMirror
    if (!targetURL || abort.signal.aborted) return
    location.replace(targetURL)
}, abortTime)

async function getWorkingMirror(signal) {
    const wikiname = location.host.slice(0, location.host.indexOf('.'))
    const targetURL = new URL(location)
    targetURL.pathname = '/' + wikiname + targetURL.pathname

    // try each mirror
    for (const mirror of mirrors) {
        if (signal?.aborted) return
        try {
            const mirrorURL = new URL(mirror)
            targetURL.host = mirrorURL.host

            // send HEAD request
            console.debug('mirror HEAD:', targetURL.toString())
            const res = await new Promise((res, rej) => GM.xmlHttpRequest({
                method: "HEAD",
                url: targetURL.toString(),
                onload: res,
                ontimeout: rej,
                onerror: rej,
                timeout: 1000,
                signal
            }))

            //validate status
            if (res.status !== 200) throw Error(`HEAD ${mirror} HTTP ${res.status} ${res.statusCode}`)

            // is working
            return targetURL
        } catch(e) {
            console.error('mirror error', mirror, '->', e)
        }
    }
}