Bangumi Domain Redirector

RT; Refurbished from https://greasyfork.org/en/scripts/12803

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Bangumi Domain Redirector
// @namespace    https://github.com/ipcjs
// @description  RT; Refurbished from https://greasyfork.org/en/scripts/12803
// @name:zh-CN   番组计划域名重定向
// @description:zh-CN 重定向番组计划(Bangumi)域名; 改造自 https://greasyfork.org/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
}