forked from yuyuyzl/Q2WebDisplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bilibili Live Translate Fetcher.user.js
166 lines (152 loc) · 8.98 KB
/
Bilibili Live Translate Fetcher.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// ==UserScript==
// @name Bilibili Live Translate Fetcher
// @namespace http://tampermonkey.net/
// @version 0.0.1
// @description 测试用的小玩具
// @author yuyuyzl
// @require https://code.jquery.com/jquery-3.4.0.min.js
// @require https://cdn.bootcss.com/jqueryui/1.12.1/jquery-ui.min.js
// @grant unsafeWindow
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_xmlhttpRequest
// @grant GM_setClipboard
// @grant GM_info
// @match *://live.bilibili.com/*
// @match *://www.youtube.com/*
// ==/UserScript==
var BLDFReg;
var intervalID=-1;
var updateTime="";
var config = {
"BLDFIntervalDelay": 3000,
"BLDFRegex": "【(.+?)】",
"CJId": "test",
"CJUrlPrefix": "https://api.vtb.wiki/q2w"
};
var lastTime = '0';
(function() {
'use strict';
var reloadConfig=function(){
Object.keys(config).forEach(function(key){
//console.log(key,config[key]);
var valuet=GM_getValue(key);
if(valuet!=null){
config[key]=valuet;
}else {
GM_setValue(key,config[key]);
}
});
};
reloadConfig();
if(window.location.href.match(/.*live.bilibili.com.*/) || window.location.href.match(/.*www.youtube.com.*/)) {
if((GM_getValue("UpdateTime"))==null)GM_setValue("UpdateTime","NAN");
updateTime=GM_getValue("UpdateTime");
setTimeout(function () {
// 以下CSS以及字幕框元素来自SOW社团的自动字幕组件
// 发布帖链接:http://nga.178.com/read.php?tid=17180967
$("head").append('<style type="text/css">\n' +
' .SubtitleBody{height:160px;background-color:rgba(0, 0, 0, 0.8);color:#fff;}\n' +
' .SubtitleBody.mobile{position:relative;top:5.626666666666667rem;}\n' +
' .SubtitleBody .title{padding:10px;font-size:14px;color:#ccc;}\n' +
' .SubtitleBody.mobile .title{font-size:12px;}\n' +
' .SubtitleBody .SubtitleTextBodyFrame{padding:0 10px;overflow-y:auto;position:absolute;top:8px;bottom:8px;}\n' +
' .SubtitleBody .SubtitleTextBody{min-height:110px;font-size:14px;color:#ccc;}\n' +
' .SubtitleBody.mobile .SubtitleTextBody{font-size:12px;}\n' +
' .SubtitleBody .SubtitleTextBody p{margin-block-start:5px;margin-block-end:5px;}\n' +
' .SubtitleBody .SubtitleTextBody p:first-of-type{color:#fff;font-size:20px;font-weight:bold;}\n' +
' .SubtitleBody.mobile .SubtitleTextBody p:first-of-type{font-size:18px;}\n' +
' .SubtitleBody.Fullscreen{position:absolute;left:20px;bottom:30px;z-index:50;background-color:rgba(0, 0, 0, 0.6);width:800px;display:none}\n' +
' .SubtitleBody.mobile.Fullscreen{width:300px;}\n' +
' .bilibili-live-player[data-player-state=fullscreen] .SubtitleBody.Fullscreen,.bilibili-live-player[data-player-state=web-fullscreen] .SubtitleBody.Fullscreen{display:block;}\n' +
' .bilibili-lmp-video-wrapper[data-mode=fullScreen] .SubtitleBody.Fullscreen,.bilibili-lmp-video-wrapper[data-orientation=landscape] .SubtitleBody.Fullscreen{display:block;}\n' +
' .invisibleDanmaku{opacity:0 !important;}\n' +
' .SubtitleTextBodyFrame::-webkit-scrollbar {display: none;}' +
' </style>');
$(".icon-left-part").append('<span data-v-b74ea690="" id="regexOn" title="开关过滤" class="icon-item icon-font icon-block" style="color: royalblue"></span>');
$(".icon-left-part").append('<span data-v-b74ea690="" id="regexSettings" title="正则过滤设置" class="icon-item icon-font icon-config" style="color: royalblue"></span>');
if (window.location.href.match(/.*live.bilibili.com.*/)) {
$("#gift-control-vm").before('<div class="SubtitleBody"><div style="height:100%;position:relative;"><div class="SubtitleTextBodyFrame"><div class="SubtitleTextBody"></div></div></div></div>');
$(".bilibili-live-player").append('<div class="SubtitleBody Fullscreen ui-resizable"><div style="height:100%;position:relative;"><div class="SubtitleTextBodyFrame"><div class="SubtitleTextBody"></div></div></div><div class="ui-resizable-handle ui-resizable-e" style="z-index: 90;"></div><div class="ui-resizable-handle ui-resizable-s" style="z-index: 90;"></div><div class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se" style="z-index: 90;"></div></div>');
$(".SubtitleBody.Fullscreen").draggable();
}
if (window.location.href.match(/.*www.youtube.com.*/)) {
$("#player-container-outer").after('<div class="SubtitleBody"><div style="height:100%;position:relative;"><div class="SubtitleTextBodyFrame"><div class="SubtitleTextBody"></div></div></div></div>');
//$(".bilibili-live-player").append('<div class="SubtitleBody Fullscreen ui-resizable"><div style="height:100%;position:relative;"><div class="SubtitleTextBodyFrame"><div class="SubtitleTextBody"></div></div></div><div class="ui-resizable-handle ui-resizable-e" style="z-index: 90;"></div><div class="ui-resizable-handle ui-resizable-s" style="z-index: 90;"></div><div class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se" style="z-index: 90;"></div></div>');
//$(".SubtitleBody.Fullscreen").draggable();
}
var lock = false;
var BLDFReg = new RegExp(config.BLDFRegex, 'g');
function update() {
if (lock) return;
lock = true;
GM_xmlhttpRequest({
method: "GET",
cache: true,
url: config.CJUrlPrefix + "?"+"t="+(new Date().getTime())+"&"+"args=" + config.CJId + '|' + lastTime,
onload: function (data) {
data=data.responseText;
console.log(data);
var tsget = data.trim().split("\n");
lastTime = tsget[0];
tsget.shift();
tsget.slice().reverse().forEach(function (line, index) {
var lineProc = line.replace(/<.*>/g, "");
//lineProc = filterXSS(lineProc);
var matches = lineProc.match(BLDFReg);
if (matches != null)
matches.forEach(function (match) {
lineProc = lineProc.replace(match, "<span class=\"translation\">" + match.replace("【", "").replace("】", "") + "</span>");
});
$('.SubtitleTextBody').prepend("<p class='new'>" + lineProc + "</p>");
setTimeout(function () {
$('.new').removeClass('new');
}, 1);
});
lock = false;
}
});
}
update();
setInterval(function () {
update();
}, config.BLDFIntervalDelay);
/*
$("#regexSettings").click(function () {
window.open("https://yuyuyzl.github.io/BiliDMFilter/");
});
$("#regexOn").click(function () {
if (intervalID >= 0) {
clearInterval(intervalID);
$(".bilibili-danmaku").each(function (i, obj) {
$(obj).removeClass("invisibleDanmaku");
});
intervalID=-1;
$('.SubtitleTextBody').prepend("<p style='color: gray'>" + "弹幕过滤停止" + "</p>");
}
else {
$('.SubtitleTextBody').prepend("<p style='color: gray'>" + "弹幕过滤开始" + "</p>");
startInterval();
}
});*/
}, 3000);
}else
if(window.location.href.match(/.*\/BiliDMFilter\/.*/)){
console.log(config);
$("#BLDFSettingsSave").removeAttr("disabled");
Object.keys(config).forEach(function(key){
$("#"+key).removeAttr("disabled");
console.log(typeof config[key]);
if (typeof config[key]=="string"||typeof config[key]=="number")$("#"+key).val(config[key]);
if (typeof config[key]=="boolean")$("#"+key).attr("checked", config[key]);
});
$('#BLDFSettingsSave').click(function () {
GM_setValue("UpdateTime",new Date());
Object.keys(config).forEach(function(key){
if (typeof config[key]=="string")GM_setValue(key,$("#"+key).val());
if (typeof config[key]=="number")GM_setValue(key,parseInt($("#"+key).val()));
if (typeof config[key]=="boolean")GM_setValue(key,$("#"+key).is(':checked'));
});
})
}
})();