-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclips.js
316 lines (252 loc) · 11.8 KB
/
clips.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
/*** /clips.js - Meant for features that can run on clip pages
* _____ _ _ _
* / ____| (_) (_)
* | | | |_ _ __ ___ _ ___
* | | | | | '_ \/ __| | / __|
* | |____| | | |_) \__ \_| \__ \
* \_____|_|_| .__/|___(_) |___/
* | | _/ |
* |_| |__/
*/
/** @file Defines the clips logic for the extension. Used for all {@link # clips.twitch.tv/*} sites.
* <style>[pill]{font-weight:bold;white-space:nowrap;border-radius:1rem;padding:.25rem .75rem}[good]{background:#e8f0fe;color:#174ea6}[bad]{background:#fce8e6;color:#9f0e0e;}</style>
* @author Ephellon Grey (GitHub {@link https://github.com/ephellon @ephellon})
* @module
*/
;
window.IS_A_FRAMED_CONTAINER = (top != window);
let here = parseURL(window.location.href);
Runtime.sendMessage({ action: 'FETCH_SHARED_DATA' }, data => Object.assign(window, data));
let {
PATHNAME = here.pathname,
STREAMER = ({
get href() { return `https://www.twitch.tv/${ STREAMER.name }` },
get name() { return here.searchParameters.channel },
get live() { return !$.all('[href*="offline_embed"i]').length },
}),
GLOBAL_EVENT_LISTENERS,
} = window;
let Clips__Initialize = async(START_OVER = false) => {
// Time how long jobs take to complete properly
class StopWatch {
static #WATCHES = new Map;
constructor(name, interval) {
interval ??= Timers[name];
StopWatch.#WATCHES.set(name, this);
return Object.assign(this, {
name, interval,
start: new Date,
stop: null,
span: null,
max: Math.abs(interval + new Date) * 1.1,
});
}
static stop(name) {
StopWatch.#WATCHES.get(name)?.time();
}
time() {
let stop = this.stop = new Date;
let span = this.span = Math.abs(this.start - stop);
let { max, name } = this;
if(span > max)
$warn(`"${ name.replace(/(^|_)(\w)/g, ($0, $1, $2, $$, $_) => ['',' '][+!!$1] + $2.toUpperCase()).replace(/_+/g, '- ') }" took ${ (span / 1000).suffix('s', 2).replace(/\.0+/, '') } to complete (max time allowed is ${ (max / 1000).suffix('s', 2).replace(/\.0+/, '') }). Offense time: ${ new Date }. Offending site: ${ location.pathname }`)
?.toNativeStack?.();
}
}
/*** Networking
* _ _ _ _ _
* | \ | | | | | | (_)
* | \| | ___| |___ _____ _ __| | ___ _ __ __ _
* | . ` |/ _ \ __\ \ /\ / / _ \| '__| |/ / | '_ \ / _` |
* | |\ | __/ |_ \ V V / (_) | | | <| | | | | (_| |
* |_| \_|\___|\__| \_/\_/ \___/|_| |_|\_\_|_| |_|\__, |
* __/ |
* |___/
*/
/*** Video Clips
* __ ___ _ _____ _ _
* \ \ / (_) | | / ____| (_)
* \ \ / / _ __| | ___ ___ | | | |_ _ __ ___
* \ \/ / | |/ _` |/ _ \/ _ \ | | | | | '_ \/ __|
* \ / | | (_| | __/ (_) | | |____| | | |_) \__ \
* \/ |_|\__,_|\___|\___/ \_____|_|_| .__/|___/
* | |
* |_|
*/
Handlers.save_ttv_clips = () => {
let EDITOR_MODE = location.pathname.equals('/create');
let { src } = $('video');
let title, author, original, textContainer, placeBefore, carryQuery;
if(EDITOR_MODE) {
title = new ClipName(2);
author = USERNAME;
original = $(carryQuery = '[data-a-target*="label"i][data-a-target*="text"i]')?.closest('[style]');
placeBefore = original;
if(nullish(original))
return;
$notice('Clip editor mode.');
} else {
let [streamerInfo,, clipInfo] = $.all('[class*="clip"i][class*="info"i]');
let [views, meta] = clipInfo.children;
let [clipTitle, data] = meta.children;
let [timestamp, clipAuthor] = $.queryBy('span, a', data);
views = parseInt(views.textContent.replace(/\D+/g, ''));
title = clipTitle.innerText;
timestamp = -parseTime(timestamp.innerText);
author = clipAuthor.innerText;
original = $('[class*="social"i][class*="button"i]:is([class*="copy"i], [class*="clip"i])').closest('[class*="social"i]:not(button, [class*="icon"i])').parentElement;
placeBefore = original.parentElement.lastElementChild;
carryQuery = '.tw-tooltip';
$notice('Clip data!', { src, views, title, timestamp, author });
}
let { filename } = parseURL(src);
let [ext, ...name] = filename.split('.').reverse();
name = name.join('.');
let parent = original.parentElement;
let container = original.cloneNode(true);
let button = $('button', container);
let id = 'tt_download_link';
for(let child of $.all('[class*="clip"i]', container))
for(let key of child.classList)
child.classList.replace(key, key.replaceAll('clip', 'download'));
textContainer ??= $(carryQuery, container);
button.parentElement.setAttribute('aria-describedby', textContainer.id = id);
textContainer.innerText = `Download this clip`;
if(EDITOR_MODE)
textContainer.innerHTML = furnish(`a#tt-download__${ author.replace(/\W+/g, '') }__${ title.replace(/\W+/g, '_') }`, { href: src, download: title, style: `color:inherit!important` }, 'Download').outerHTML;
else
$('figure', button)?.replaceWith(furnish(`a#tt-download__${ author.replace(/\W+/g, '') }__${ title.replace(/\W+/g, '_') }`, { href: src, download: title }, Glyphs.utf8.download));
parent.insertBefore(container, placeBefore);
};
Timers.save_ttv_clips = -500;
__Save_TTV_Clips__:
if(true || parseBool(Settings?.save_ttv_clips)) {
RegisterJob('save_ttv_clips');
}
/*** Miscellaneous
* __ __ _ _ _
* | \/ (_) | | |
* | \ / |_ ___ ___ ___| | | __ _ _ __ ___ ___ _ _ ___
* | |\/| | / __|/ __/ _ \ | |/ _` | '_ \ / _ \/ _ \| | | / __|
* | | | | \__ \ (_| __/ | | (_| | | | | __/ (_) | |_| \__ \
* |_| |_|_|___/\___\___|_|_|\__,_|_| |_|\___|\___/ \__,_|___/
*
*
*/
Miscellaneous: {
// ...
}
// End of Clips__Initialize
};
// End of Clips__Initialize
let Clips__Initialize_Safe_Mode = async(START_OVER = false) => {
/*** Miscellaneous
* __ __ _ _ _
* | \/ (_) | | |
* | \ / |_ ___ ___ ___| | | __ _ _ __ ___ ___ _ _ ___
* | |\/| | / __|/ __/ _ \ | |/ _` | '_ \ / _ \/ _ \| | | / __|
* | | | | \__ \ (_| __/ | | (_| | | | | __/ (_) | |_| \__ \
* |_| |_|_|___/\___\___|_|_|\__,_|_| |_|\___|\___/ \__,_|___/
*
*
*/
Miscellaneous: {
// ...
}
// End of Clips__Initialize_Safe_Mode
};
// End of Clips__Initialize_Safe_Mode
let Clips__PAGE_CHECKER,
Clips__WAIT_FOR_PAGE,
Clips__SETTING_RELOADER;
Clips__PAGE_CHECKER = setInterval(Clips__WAIT_FOR_PAGE = async() => {
// The user might not be bannable on `clips.twitch.tv`...
// Only executes if the user is banned
let banned = STREAMER?.veto || !!$.all('[class*="banned"i]').length;
if([banned].contains(true)) {
$warn('[NON_FATAL] Clip container unavailable. Reason:', { banned });
await Settings.get();
wait(5000).then(Clips__Initialize_Safe_Mode);
clearInterval(Clips__PAGE_CHECKER);
}
// Only executes if the user is NOT banned
let ready = (true /* Assume OK if this loads in the first place... */
// The main controller is ready
// && parseBool(top.MAIN_CONTROLLER_READY)
// There is an error message
|| $.nullish('[data-test-selector^="content-overlay-gate"i]')
);
if(ready) {
$log(`Clip container ready → ${ location.href }`);
await Settings.get();
wait(5000).then(Clips__Initialize);
clearInterval(Clips__PAGE_CHECKER);
window.FRAMED_CONTROLLER_READY = true;
// Only re-execute if in an iframe
if(IS_A_FRAMED_CONTAINER) {
// Observe [top] location changes
LocationObserver: {
let { body } = document,
observer = new MutationObserver(mutations => {
mutations.map(mutation => {
if(PATHNAME !== location.pathname) {
let OLD_HREF = PATHNAME;
PATHNAME = location.pathname;
for(let [name, func] of (top?.__ONLOCATIONCHANGE__ ?? []))
func(new CustomEvent('locationchange', { from: OLD_HREF, to: PATHNAME }));
}
});
});
observer.observe(body, { childList: true, subtree: true });
}
}
// Set the SVGs' section IDs
SectionLabeling: {
let conversions = {},
Glyphs = window.Glyphs;
for(let container of $.all('figure')) {
let svg = $('svg', container);
if(nullish(svg))
continue;
comparing:
for(let glyph in Glyphs)
if(Glyphs.__exclusionList__.contains(glyph))
continue comparing;
else
resemble(svg.toImage())
.compareTo(Glyphs.modify(glyph, { height: 20, width: 20 }).asNode.toImage())
.ignoreColors()
.scaleToSameSize()
.onComplete(async data => {
let { analysisTime, misMatchPercentage } = data;
analysisTime = parseInt(analysisTime);
misMatchPercentage = parseFloat(misMatchPercentage);
let matchPercentage = 100 - misMatchPercentage;
if(matchPercentage < 80 || container.getAttribute('tt-svg-label')?.length)
return;
// $log(`Labeling section "${ glyph }" (${ matchPercentage }% match)...`, container);
container.setAttribute('tt-svg-label', conversions[glyph]?.pop());
});
}
}
// Add custom styling
CustomCSSInitializer: {
AddCustomCSSBlock('clips.js', ``);
}
// Update the settings
SettingsInitializer: {
switch(Settings.onInstalledReason) {
// Is this the first time the extension has run?
// If so, then point out what's been changed
case INSTALL: {
// Alert something for the players...
} break;
}
Storage.set({ onInstalledReason: null });
}
}
}, 500);
Clips__SETTING_RELOADER = setInterval(() => {
for(let MAX_CALLS = 60; MAX_CALLS > 0 && window.REFRESH_ON_CHILD?.length; --MAX_CALLS)
RestartJob(window.REFRESH_ON_CHILD.pop(), 'clips-setting-reloader:max-calls');
}, 250);