-
Notifications
You must be signed in to change notification settings - Fork 4
/
ckylin-script-bilibili-dynamix-default-tab.user.js
93 lines (84 loc) · 2.73 KB
/
ckylin-script-bilibili-dynamix-default-tab.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// ==UserScript==
// @name [Bilibili]动态页面默认投稿视频页面
// @namespace ckylin-script-bilibili-dynamix-default-tab
// @version 0.1
// @description 让哔哩哔哩动态页面默认显示投稿视频
// @author CKylinMC
// @match https://t.bilibili.com/*
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @grant GM_getValue
// @grant GM_setValue
// @license WTFPL
// ==/UserScript==
(function() {
const getUrlParam = key=>(new URL(location.href)).searchParams.get(key)
const wait = ms=>new Promise(r=>setTimeout(r,ms));
const waitForDom = async (query,domparent=document,maxRetries=20,gagms=200)=>{
let i = maxRetries;
while(--i>0){
if(domparent.querySelector(query)) return true;
await wait(gagms);
}
return false;
}
async function trySwitch(){
const tabBtn = document.querySelector(".tab-bar>.tab:nth-child(2)>a.tab-text");
if(tabBtn&&!tabBtn.classList.contains("selected")){
let maxClick = 20;
while(--maxClick && !tabBtn.classList.contains("selected")){
tabBtn.click();
await wait(200);
}
}
}
async function way1(){
if(getUrlParam("tab")) return;
await waitForDom(".tab-bar");
await trySwitch();
}
function way2(){
if(!getUrlParam("tab")) return location.href = "?tab=8";
}
let menuIds = [];
let menus = {};
const registerMenu = (text, callback) => menuIds.push(GM_registerMenuCommand(text, callback));
const clearMenu = () => { menuIds.forEach(id => GM_unregisterMenuCommand(id)); menuIds = []; };
function applyMenus() {
clearMenu();
for (let item in menus) {
if(!menus.hasOwnProperty(item)) continue;
let menu = menus[item];
registerMenu(menu.text, menu.callback);
}
}
function setMenu(id,text,callback,noapply = false) {
menus[id] = { text, callback };
if (!noapply) applyMenus();
}
function setWayMethodA(){
GM_setValue("useJump",true);
setWayMenuB();
}
function setWayMethodB(){
GM_setValue("useJump",false);
setWayMenuA();
}
function setWayMenuA(noapply = false) {
setMenu("WAY", "改为使用跳转方案", setWayMethodA, noapply);
}
function setWayMenuB(noapply = false) {
setMenu("WAY", "改为使用自动点击方案", setWayMethodB, noapply);
}
function init(){
let targetMethod = way1;
if(GM_getValue("useJump")){
setWayMenuB();
targetMethod = way2;
}else{
setWayMenuA();
}
targetMethod();
}
init();
})();