-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path474214-fix-brave-yt-live-1.8.user.js
88 lines (78 loc) · 2.99 KB
/
474214-fix-brave-yt-live-1.8.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
// ==UserScript==
// @name Fix Brave Bug for YouTube Live Chat
// @namespace UserScripts
// @version 1.8
// @description To Fix Brave Bug for YouTube Live Chat
// @author CY Fung
// @license MIT
// @icon https://cdn.jsdelivr.net/gh/cyfung1031/userscript-supports@main/icons/brave.png
// @match https://www.youtube.com/*
// @grant none
// @run-at document-start
// @unwrap
// @inject-into page
// ==/UserScript==
(async () => {
'use strict';
const insp = o => o ? (o.polymerController || o.inst || o || 0) : (o || 0);
await customElements.whenDefined('ytd-live-chat-frame');
const chat = document.getElementById('chat') || (await new Promise(resolve => {
let mo = new MutationObserver(entries => {
const chat = document.getElementById('chat');
if (chat && mo) {
mo.disconnect();
mo.takeRecords();
mo = null;
resolve(chat);
}
});
mo.observe(document, { childList: true, subtree: true })
}));
if (!chat || chat.is !== 'ytd-live-chat-frame') return;
const chatWR = new WeakRef(chat);
/** @param {HTMLIFrameElement} chatframe */
const onChatFrameFound = (chatframe) => {
try {
const body = chatframe.contentDocument.body;
let io = new IntersectionObserver(function () {
if (io) {
io.disconnect();
io.takeRecords();
io = null;
const chat = chatWR.deref();
if (chat) {
const frameLocation = chatframe.contentWindow.location;
let src = chatframe.src || '';
if (src === '' || src === 'about:blank') {
const cnt = insp(chat);
src = (cnt.url || '');
if (!src.includes('/live_chat')) src = cnt.liveChatPageUrl(cnt.baseUrl, cnt.collapsed, cnt.data, cnt.forceDarkTheme);
}
if (body.firstChild === null && src.includes('/live_chat') && frameLocation.href === 'about:blank') {
frameLocation.replace(src.replace(/&\d+$/, '') + "&1");
}
}
chatframe = null;
}
});
io.observe(body);
} catch (e) {
console.warn(e);
}
}
const f = () => {
const chatframe = ((insp(chat).$ || chat.$ || chat).chatframe || 0);
if (chatframe instanceof HTMLIFrameElement && chatframe.isConnected === true) {
if (!chatframe.__b375__) {
chatframe.__b375__ = 1;
Promise.resolve(chatframe).then(onChatFrameFound);
}
}
}
const mo = new MutationObserver(f);
mo.observe(chat, {
attributes: true,
attributeFilter: ['collapsed', 'hidden']
});
f();
})();