forked from zyronon/V2Next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Youtube Mobile Enhance.user.js
380 lines (372 loc) · 14 KB
/
Youtube Mobile Enhance.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// ==UserScript==
// @name Youtube Mobile Enhance 油管移动端增强
// @namespace http://tampermonkey.net/
// @version 2.8.3
// @author zyronon
// @description 针对油管移动端,点击视频新标签页打开,记忆播放速度,突破播放速度限制
// @license GPL License
// @icon https://v2next.netlify.app/favicon.ico
// @homepage https://github.com/zyronon/web-scripts
// @homepageURL https://github.com/zyronon/web-scripts
// @supportURL https://update.greasyfork.org/scripts/487013/Youtube%20Mobile%20Enhance%20%E6%B2%B9%E7%AE%A1%E7%A7%BB%E5%8A%A8%E7%AB%AF%E5%A2%9E%E5%BC%BA.user.js
// @downloadURL https://update.greasyfork.org/scripts/487013/Youtube%20Mobile%20Enhance%20%E6%B2%B9%E7%AE%A1%E7%A7%BB%E5%8A%A8%E7%AB%AF%E5%A2%9E%E5%BC%BA.user.js
// @updateURL https://update.greasyfork.org/scripts/487013/Youtube%20Mobile%20Enhance%20%E6%B2%B9%E7%AE%A1%E7%A7%BB%E5%8A%A8%E7%AB%AF%E5%A2%9E%E5%BC%BA.user.js
// @match https://m.youtube.com/*
// @require https://cdn.jsdelivr.net/npm/vue@3.4.14/dist/vue.global.prod.js
// @grant GM_addStyle
// @grant GM_openInTab
// @grant unsafeWindow
// ==/UserScript==
(t=>{if(typeof GM_addStyle=="function"){GM_addStyle(t);return}const e=document.createElement("style");e.textContent=t,document.head.append(e)})(" .ytb-next{font-size:1.4rem;display:flex;gap:1rem;position:fixed;top:0;right:10px;z-index:99999}.ytb-next .btn{color:#f1f1f1;background-color:#ffffff1a;padding:0 16px;height:36px;font-size:14px;line-height:36px;border-radius:18px}.msg{position:fixed;z-index:999;font-size:3rem;left:0;top:0;color:#000;background:white;padding:1rem 2rem}@media (min-width: 1280px) and (orientation: landscape){.player-container,.player-container.sticky-player{right:400px!important;top:0!important}ytm-watch{margin-right:400px!important}ytm-engagement-panel{width:400px!important;top:0!important}.playlist-entrypoint-background-protection,.slide-in-animation-entry-point{width:400px!important}ytm-single-column-watch-next-results-renderer [section-identifier=related-items],ytm-single-column-watch-next-results-renderer>ytm-playlist{width:400px!important;padding:0 0 8px 8px}ytm-single-column-watch-next-results-renderer .playlist-content{width:400px!important}} ");
(function (vue) {
'use strict';
var _GM_openInTab = /* @__PURE__ */ (() => typeof GM_openInTab != "undefined" ? GM_openInTab : void 0)();
var _unsafeWindow = /* @__PURE__ */ (() => typeof unsafeWindow != "undefined" ? unsafeWindow : void 0)();
const _hoisted_2 = {
key: 1,
class: "msg"
};
const _sfc_main = /* @__PURE__ */ vue.defineComponent({
__name: "App",
setup(__props) {
let refVideo = vue.ref(null);
let rate = vue.ref(1);
let lastRate = vue.ref(1);
let pageType = vue.ref("");
let msg = vue.reactive({
show: false,
content: "",
timer: -1
});
function stop(e) {
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
return true;
}
function openNewTab(href, active = false) {
_GM_openInTab(href, { active });
}
function getBrowserType() {
let userAgent = navigator.userAgent;
if (userAgent.indexOf("Opera") > -1) {
return "Opera";
}
if (userAgent.indexOf("Firefox") > -1) {
return "FF";
}
if (userAgent.indexOf("Chrome") > -1) {
return "Chrome";
}
if (userAgent.indexOf("Safari") > -1) {
return "Safari";
}
if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
return "IE";
}
}
function initStyle(type) {
let style2 = `
:root {
--color-scrollbar: rgb(147, 173, 227);
}
html[darker-dark-theme] {
--color-scrollbar: rgb(92, 93, 94);
}
${type === "FF" ? `/* 火狐美化滚动条 */
* {
scrollbar-color: var(--color-scrollbar);
/* 滑块颜色 滚动条背景颜色 */
scrollbar-width: thin;
/* 滚动条宽度有三种:thin、auto、none */
}` : `
::-webkit-scrollbar {
width: 1rem;
height: 1rem;
}
::-webkit-scrollbar-track {
background: transparent;
border-radius: .2rem;
}
::-webkit-scrollbar-thumb {
background: var(--color-scrollbar);
border-radius: 1rem;
}`}
`;
let addStyle2 = document.createElement("style");
addStyle2.rel = "stylesheet";
addStyle2.type = "text/css";
addStyle2.innerHTML = style2;
window.document.head.append(addStyle2);
}
function findA(target, e) {
let parentNode = target.parentNode;
let count = 0;
while (parentNode.tagName !== "A" && count < 10) {
count++;
parentNode = parentNode.parentNode;
}
console.log(parentNode);
openNewTab(parentNode.href, true);
return stop(e);
}
function checkPageType() {
if (location.pathname === "/watch") {
pageType.value = "watch";
}
if (location.pathname === "/") {
pageType.value = "home";
}
if (location.pathname.startsWith("/@")) {
pageType.value = "user";
}
}
function checkVideo() {
let v = document.querySelector("video");
if (v) {
v.playbackRate = rate.value;
refVideo.value = v;
window.funs.checkWatchPageDiv();
return true;
}
}
function playbackRateToggle() {
checkVideo();
if (refVideo.value) {
if (refVideo.value.playbackRate !== 1) {
lastRate.value = rate.value;
rate.value = refVideo.value.playbackRate = 1;
showMsg("播放速度: 1");
} else {
rate.value = refVideo.value.playbackRate = lastRate.value === 1 ? 2 : lastRate.value;
showMsg("播放速度: " + rate.value);
}
}
}
function toggle() {
checkVideo();
if (refVideo.value) {
if (refVideo.value.paused) {
refVideo.value.play();
} else {
refVideo.value.pause();
}
}
}
function setPlaybackRate(val) {
checkVideo();
if (refVideo.value) {
rate.value = refVideo.value.playbackRate = Number(val.toFixed(1));
showMsg("播放速度: " + rate.value);
}
}
function showMsg(text) {
if (msg.show) {
msg.show = false;
clearTimeout(msg.timer);
}
msg.show = true;
msg.content = text;
msg.timer = setTimeout(() => {
msg.show = false;
}, 3e3);
}
function checkOptionButtons() {
let dom = document.querySelector(".ytb-next");
if (dom)
return;
dom = document.createElement("div");
dom.classList.add("ytb-next");
dom.innerHTML = `
<div class="btn" onclick="window.cb('playbackRateToggle')">切换</div>
<div class="btn" onclick="window.cb('addRate')"> + </div>
<div class="btn" onclick="window.cb('removeRate')"> - </div>
<div class="btn" onclick="window.cb('playbackRateToggle2')"> 2 </div>
<div class="btn" onclick="window.cb('playbackRateToggle25')"> 2.5 </div>
<div class="btn" onclick="window.cb('playbackRateToggle3')"> 3 </div>
`;
document.body.append(dom);
}
function checkIsWatchPage() {
checkPageType();
return pageType.value === "watch";
}
function checkA(e) {
let target = e.target;
let tagName = target.tagName;
let classList = target.classList;
if (tagName === "IMG" && Array.from(classList).some((v) => v.includes("yt-core-image"))) {
console.log("封面");
if (checkIsWatchPage())
return;
return findA(target, e);
}
if (tagName === "SPAN" && Array.from(classList).some((v) => v.includes("yt-core-attributed-string"))) {
console.log("标题");
if (checkIsWatchPage())
return;
return findA(target, e);
}
if (tagName === "BUTTON" && Array.from(classList).some((v) => v.includes("ytp-large-play-button"))) {
console.log("播放按钮");
if (checkIsWatchPage())
return;
}
if (tagName === "DIV" && Array.from(classList).some((v) => v.includes("ytp-cued-thumbnail-overlay-image"))) {
console.log("播放按钮");
if (checkIsWatchPage())
return;
}
}
vue.watch(rate, (value) => {
localStorage.setItem("youtube-rate", value);
window.rate = value;
});
vue.onMounted(() => {
console.log("Youtube Next start");
let browserType = getBrowserType();
initStyle(browserType);
let youtubeRate = localStorage.getItem("youtube-rate");
if (youtubeRate) {
rate.value = Number(youtubeRate);
}
_unsafeWindow.cb = (type) => {
console.log("type", type);
switch (type) {
case "toggle":
toggle();
break;
case "playbackRateToggle":
playbackRateToggle();
break;
case "playbackRateToggle2":
setPlaybackRate(2);
break;
case "playbackRateToggle25":
setPlaybackRate(2.5);
break;
case "playbackRateToggle3":
setPlaybackRate(3);
break;
case "addRate":
setPlaybackRate(rate.value + 0.1);
break;
case "removeRate":
setPlaybackRate(rate.value - 0.1);
break;
}
};
if (checkIsWatchPage()) {
checkOptionButtons();
setTimeout(() => {
checkVideo();
if (refVideo.value) {
refVideo.value.muted = false;
refVideo.value.playbackRate = rate.value;
showMsg("播放速度: " + rate.value);
}
}, 500);
}
window.addEventListener("click", checkA, true);
window.addEventListener("visibilitychange", stop, true);
});
vue.onUnmounted(() => {
window.removeEventListener("click", checkA, true);
window.removeEventListener("visibilitychange", stop, true);
});
return (_ctx, _cache) => {
return vue.openBlock(), vue.createElementBlock(vue.Fragment, null, [
vue.createCommentVNode("", true),
vue.unref(msg).show ? (vue.openBlock(), vue.createElementBlock("div", _hoisted_2, vue.toDisplayString(vue.unref(msg).content), 1)) : vue.createCommentVNode("", true)
], 64);
};
}
});
window.videoEl = null;
window.rate = 1;
window.funs = {
checkWatchPageDiv() {
let header = document.querySelector("#header-bar");
let stickyPlayer = document.querySelector("#app.sticky-player");
if (header)
header.style["display"] = "none";
if (stickyPlayer)
stickyPlayer.style["padding-top"] = "0";
}
};
new MutationObserver(() => {
let v = document.querySelector("video");
if (v) {
if (!window.videoEl) {
console.log("init");
v.playbackRate = 2;
window.videoEl = v;
}
}
});
function proxyHTMLMediaElementEvent() {
if (HTMLMediaElement.prototype._rawAddEventListener_) {
return false;
}
HTMLMediaElement.prototype._rawAddEventListener_ = HTMLMediaElement.prototype.addEventListener;
HTMLMediaElement.prototype._rawRemoveEventListener_ = HTMLMediaElement.prototype.removeEventListener;
HTMLMediaElement.prototype.addEventListener = new Proxy(HTMLMediaElement.prototype.addEventListener, {
apply(target, ctx, args) {
const eventName = args[0];
const listener = args[1];
if (listener instanceof Function && eventName === "ratechange") {
args[1] = new Proxy(listener, {
apply(target2, ctx2, args2) {
if (ctx2) {
if (ctx2.playbackRate && eventName === "ratechange") {
if (ctx2._hasBlockRatechangeEvent_) {
return true;
}
const oldRate = ctx2.playbackRate;
const startTime = Date.now();
const result = target2.apply(ctx2, args2);
const blockRatechangeBehave1 = oldRate !== ctx2.playbackRate || Date.now() - startTime > 1e3;
const blockRatechangeBehave2 = ctx2._setPlaybackRate_ && ctx2._setPlaybackRate_.value !== ctx2.playbackRate;
if (blockRatechangeBehave1 || blockRatechangeBehave2) {
debug.info(`[execVideoEvent][${eventName}]检测到可能存在阻止调速的行为,已禁止${eventName}事件的执行`, listener);
ctx2._hasBlockRatechangeEvent_ = true;
return true;
} else {
return result;
}
}
}
try {
return target2.apply(ctx2, args2);
} catch (e) {
debug.error(`[proxyPlayerEvent][${eventName}]`, listener, e);
}
}
});
}
if (listener instanceof Function && eventName === "play") {
args[1] = new Proxy(listener, {
apply(target2, ctx2, args2) {
console.log("play", window.rate);
ctx2.playbackRate = window.rate;
window.funs.checkWatchPageDiv();
try {
return target2.apply(ctx2, args2);
} catch (e) {
debug.error(`[proxyPlayerEvent][${eventName}]`, listener, e);
}
}
});
}
return target.apply(ctx, args);
}
});
}
proxyHTMLMediaElementEvent();
let $section = document.createElement("section");
$section.id = "vue-app";
document.body.append($section);
let vueApp = vue.createApp(_sfc_main);
vueApp.config.unwrapInjectedRef = true;
vueApp.mount($section);
})(Vue);