Greasy Fork

Greasy Fork is available in English.

番组计划域名重定向

重定向番组计划(Bangumi)域名; 改造自 http://greasyfork.icu/zh-CN/scripts/12803

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Bangumi Domain Redirector
// @namespace    https://github.com/ipcjs
// @description  RT; Refurbished from http://greasyfork.icu/en/scripts/12803
// @name:zh-CN   番组计划域名重定向
// @description:zh-CN 重定向番组计划(Bangumi)域名; 改造自 http://greasyfork.icu/zh-CN/scripts/12803
// @author       ipcjs
// @compatible   chrome
// @compatible   firefox
// @license      MIT
// @version      0.1.0
// @include      *://bgm.tv/*
// @include      *://*.bgm.tv/*
// @include      *://chii.in/*
// @include      *://*.chii.in/*
// @include      *://bangumi.tv/*
// @include      *://*.bangumi.tv/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @run-at       document-start
// ==/UserScript==
'use strict'
const hosts = {
    bgm: { https: true, host: 'bgm.tv' },
    bangumi: { https: false, host: 'bangumi.tv' },
    chii: { https: false, host: 'chii.in' },
}
let targetHost = hosts[GM_getValue('host-name') || 'bgm']
const menuIds = []
const rebuildMenus = () => {
    for (let menuId of menuIds) {
        GM_unregisterMenuCommand(menuId)
    }
    menuIds.length = 0
    for (let name of Object.keys(hosts)) {
        let host = hosts[name]
        menuIds.push(GM_registerMenuCommand(`${host === targetHost ? '=> ' : ''}http${host.https ? 's' : ''}://${host.host}`, () => {
            GM_setValue('host-name', name)
            // 直接刷新界面, 就没有重建菜单的必要了...
            const refresh = true
            if (refresh) {
                window.location.reload()
            } else {
                targetHost = host
                rebuildMenus()
            }
        }))
    }
}
rebuildMenus()

if (!window.location.hostname.endsWith(targetHost.host)) {
    const url = location.href.replace(/https?:\/\/(.*\.?)(bgm\.tv|bangumi\.tv|chii\.in)(.*)/, `http${targetHost.https ? 's' : ''}://$1${targetHost.host}$3`)
    console.log(`${location.href} => ${url}`)
    window.location.href = url
}