-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
chat-injector.ts
93 lines (82 loc) · 3.15 KB
/
chat-injector.ts
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
import HcButton from '../components/HyperchatButton.svelte';
import { getFrameInfoAsync, isValidFrameInfo, frameIsReplay } from './chat-utils';
import { isLiveTL, isAndroid } from './chat-constants';
// const isFirefox = navigator.userAgent.includes('Firefox');
const hcWarning = 'An existing HyperChat button has been detected. This ' +
'usually means both LiveTL and standalone HyperChat are enabled. ' +
'LiveTL already includes HyperChat, so please enable only one of them.\n\n' +
'Having multiple instances of the same scripts running WILL cause ' +
'problems such as chat messages not loading.';
const chatLoaded = async (): Promise<void> => {
if (document.querySelector('.toggleButton')) {
console.error(hcWarning);
return;
}
document.body.style.minWidth = document.body.style.minHeight = '0px';
const hyperChatEnabled = localStorage.getItem('HC:ENABLED') !== 'false';
// Inject HC button
const ytcPrimaryContent = document.querySelector('#primary-content');
if (!ytcPrimaryContent) {
console.error('Failed to find #primary-content');
return;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const hcButton = new HcButton({
target: ytcPrimaryContent
});
// Everything past this point will only run if HC is enabled
if (!hyperChatEnabled) return;
const ytcItemList = document.querySelector('#chat>#item-list');
if (!ytcItemList) {
console.error('Failed to find #chat>#item-list');
return;
}
// Inject hyperchat
const frameInfo = await getFrameInfoAsync();
if (!isValidFrameInfo(frameInfo)) {
console.error('Failed to get valid frame info', { frameInfo });
return;
}
const params = new URLSearchParams();
params.set('tabid', frameInfo.tabId.toString());
params.set('frameid', frameInfo.frameId.toString());
if (frameIsReplay) params.set('isReplay', 'true');
const source = chrome.runtime.getURL(
(isLiveTL ? 'hyperchat/index.html' : 'hyperchat.html') +
`?${params.toString()}`
);
ytcItemList.outerHTML = `
<iframe id="hyperchat" src="${source}" style="border: 0px; width: 100%; height: 100%;"/>
`;
// const hyperchat = document.querySelector('#hyperchat') as HTMLIFrameElement;
// if (!hyperchat) {
// console.error('Failed to find #hyperchat');
// return;
// }
// if (isFirefox || isLiveTL) {
// const scale = 0.8;
// const inverse = `${Math.round((1 / scale) * 10000) / 100}%`;
// hyperchat.style.transformOrigin = '0px 0px';
// hyperchat.style.minWidth = inverse;
// hyperchat.style.minHeight = inverse;
// hyperchat.style.transform = `scale(${scale})`;
// }
// Remove ticker element
const ytcTicker = document.querySelector('#ticker');
if (!ytcTicker) {
console.error('Failed to find #ticker');
return;
}
ytcTicker.remove();
// Hide input panel on android
if (isAndroid) {
const inputPanel = document.querySelector('#input-panel');
if (!inputPanel) return;
(inputPanel as HTMLElement).style.display = 'none';
}
};
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => async () => await chatLoaded());
} else {
chatLoaded().catch(e => console.error(e));
}