-
Notifications
You must be signed in to change notification settings - Fork 327
/
bangumi_domain_redirector_.user.js
58 lines (57 loc) · 2.03 KB
/
bangumi_domain_redirector_.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// ==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
}