-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
599 lines (526 loc) · 16.2 KB
/
script.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/**
* SadgeClipper
* JS Twitch Clip Dashboard
* @author dylmye
* (c) 2020- dylan myers under ISC license
*
* Depends on: cash (by Fabio Spampinato), dayjs (by iamkun),
* timeago.js (by Hust.cc), micromodal (by Indrashish Ghosh)
* and localforge (by Matthew R MacPherson & Thodoris Greasidis)
*/
const SHOW_DEBUG = false;
const SADGE_VER = "1.1.1";
// globals
let bearer_token = "";
let client_id = "33bst72zxflxxz5o3xrhntafqimhbh"; // make sure to use your own client ID
let embed_parent = "dylmye.me"; // see https://dev.twitch.tv/docs/embed#embedded-experiences-requirements 1.2
let raw_base_url = "https://dylmye.me/sadgeclipper/";
let base_url = encodeURIComponent(raw_base_url); // used in oauth callback
let base_helix_url = "https://api.twitch.tv/helix/"; // change to sandbox api if needed
let base_oauth_url = "https://id.twitch.tv/oauth2/";
const LOGIN_URL = `${base_oauth_url}authorize?client_id=${client_id}&redirect_uri=${base_url}oauth-callback.html&response_type=token&force_verify=${SHOW_DEBUG ? "true" : "false"}`;
const LOGOUT_URL = `${raw_base_url}logout.html`;
/** API Action endpoints from https://dev.twitch.tv/docs/api/reference */
const ACTIONS = {
USERS: "users",
CLIPS: "clips",
};
/** Sort options for Clips action */
const SORT_CLIPS = {
byStreamerDesc: "Streamer (A-Z)",
byStreamerAsc: "Streamer (Z-A)",
chronologicalDesc: "Day Clipped (latest first)",
chronologicalAsc: "Day Clipped (earliest first)",
};
/** Pagination sizes for Get Clips action, max: 100 */
const CLIPS_PAGE_SIZES = [5, 10, 20, 50, 100];
/** Number of days to search for with Get Clips action */
const CLIPS_DAY_OPTIONS = [5, 7, 14, 30];
/** Max pagination size for Get Clips action */
const MAX_CLIPS_PAGE_SIZE = 100;
/** Default option settings */
const DEFAULT_SETTINGS = {
settingsVersion: 1.0,
previewsOpenEmbeds: false,
usePrettyTimestamps: true,
defaultSort: "chronologicalDesc",
pageSizePerStreamer: 10,
daysToSearch: 7,
showDownloadButton: false,
};
const SETTING_TYPES = {
BINARY_RADIO: "binary_radio",
SIMPLE_DROPDOWN: "simple_dropdown",
DROPDOWN: "dropdown",
};
/** Human-friendly text for settings. False = don't show */
const SETTING_LABELS = {
settingsVersion: false, // internal value for versioning
previewsOpenEmbeds: {
text: "Click thumbnail to play embed?",
type: SETTING_TYPES.BINARY_RADIO,
},
usePrettyTimestamps: {
text: "Show dates relative to today?",
type: SETTING_TYPES.BINARY_RADIO,
},
defaultSort: false,
// defaultSort: {
// text: "Sort clips by",
// type: SETTING_TYPES.DROPDOWN,
// options: SORT_CLIPS
// },
pageSizePerStreamer: {
text: "How many clips to show per streamer (max)",
type: SETTING_TYPES.SIMPLE_DROPDOWN,
options: CLIPS_PAGE_SIZES,
},
daysToSearch: {
text: "How many days to search",
type: SETTING_TYPES.SIMPLE_DROPDOWN,
options: CLIPS_DAY_OPTIONS,
},
showDownloadButton: false,
// showDownloadButton: {
// text: "Show download button?",
// type: SETTING_TYPES.BINARY_RADIO,
// },
};
// helpers
/**
* Creates a URL object for the API action
* @param {string} action - should be from `ACTIONS` array
* @returns {URL} url object for action
*/
function apiUrl(action) {
return new URL(`${base_helix_url}${action}`);
}
/**
* Returns the fetch options obj (RequestInit).
* Has to be a function because otherwise the
* bearer token doesn't update.
* @param {string} method - HTTP method type to use. default: GET
* @returns {RequestInit} options object
*/
function getOptions(method = "GET") {
return {
method,
headers: new Headers({
"Client-ID": client_id,
Authorization: `Bearer ${bearer_token}`,
}),
mode: "cors",
};
}
/**
* Sets an object of params on a URL object that is then returned
* @param {URL} url - the URL object to append search params to
* @param {*} params - the object of key->values to add as params
* @returns {URL} updated URL
*/
function setUrlSearchParams(url, params) {
Object.keys(params).forEach((k) => {
url.searchParams.set(k, params[k]);
});
return url;
}
/** Setting rules:
* * Key must only contain letters a-z, A-Z
* * Value must not contain = < > or &
*/
function setValueSafely(key, value, debug = SHOW_DEBUG) {
const safeKey = key.replace(/[^A-Za-z]/g, "");
const safeVal =
typeof value === "string" ? value.replace(/[=<>&]/g, "") : value;
if (debug) console.debug(`Setting value for key ${key}:`, value);
return localforage.setItem(safeKey, safeVal, (err) => {
if (err)
console.error(
`Unable to set value '${value}' (formatted to '${safeVal}') for key '${safeKey}': ${err}`
);
});
}
/** Getting rules:
* * Key must only contain letters a-z, A-Z
* * Value must not contain = < > or &
*/
async function getValueSafely(key, debug = SHOW_DEBUG) {
const safeKey = key.replace(/[^A-Za-z]/g, "");
const value = await localforage.getItem(safeKey, (err, val) => {
if (debug) console.debug(`the value of '${key}' is`, val);
if (err)
console.error(
`Unable to get value for key ${key} (formatted to ${safeKey}): ${err}`
);
});
return value;
}
/**
* Get all key-value pairs from store
* @param {boolean} debug
*/
async function getStore(debug = SHOW_DEBUG) {
const store = {};
await localforage.iterate((v, k) => {
store[k] = v;
});
if (debug) console.debug("the store contents are:", store);
return store;
}
/**
* Get a setting by its key.
* @param {string} key The settings key to retrieve
* @returns {string | number | boolean} the settings value
*/
async function getSetting(key) {
const settings = await getValueSafely("settings");
if (!settings) {
console.error("Couldn't load settings object as it hasn't been initalised");
return null;
}
if (!(key in settings)) {
console.error(`Couldn't find setting: ${key}`);
return null;
}
return settings[key];
}
/**
* Update the Settings object with current settings as backups
* @param {*} newSettings The settings object to replace the current one with
*/
async function updateSettings(newSettings) {
let newObj = {};
const current = await getValueSafely("settings");
Object.keys(DEFAULT_SETTINGS).forEach((k) => {
if (k === "settingsVersion") {
// always update to the latest version as we disregard
// any old keys
newObj.settingsVersion = DEFAULT_SETTINGS.settingsVersion;
} else if (!(k in newSettings)) {
// set default value
if (k in current) {
newObj[k] = current[k];
} else {
newObj[k] = DEFAULT_SETTINGS[k];
}
} else {
// set our updated thing
if (newSettings[k] === "false" || newSettings[k] === "true") {
newObj[k] = newSettings[k] === "true";
} else if (!isNaN(newSettings[k])) {
newObj[k] = parseInt(newSettings[k]);
} else {
newObj[k] = newSettings[k];
}
}
});
setValueSafely("settings", newObj);
}
/**
* Returns the filled HTML template for a given clip
* @param {*} c - the clip to create HTML for
* @param {*} settings - settings object
* @returns {string} the HTMl for given clip
*/
function generateClipHtml(c, { previewsOpenEmbeds }) {
return `
<li class="clip" id="clip-${c.id}">
<div class="clip-container">
<a href="${!previewsOpenEmbeds ? c.url : "#"}">
<img src="${c.thumbnail_url}" alt="Thumbnail of a clip of ${
c.broadcaster_name
}'s stream" class="clip-thumb"${
previewsOpenEmbeds ? `onClick="openEmbed('${c.id}')"` : ""
} />
</a>
</div>
<div class="clip-description">
<p><a href="${c.url}">${c.title}</a></p>
<p>
Clipped by ${
c.creator_name
} • Clipped <span class="renderable-date" datetime="${
c.created_at
}" title="${c.created_at}">${c.created_at}</span>
</p>
</div>
</li>`;
}
/**
* Replace image with iframe
* @param {string} clipId - the clip to replace image with iframe
*/
function openEmbed(clipId) {
$(`#clip-${clipId} img.clip-thumb`).replaceWith(
`<iframe src="https://clips.twitch.tv/embed?clip=${clipId}&parent=${embed_parent}" frameborder="0" allowfullscreen="true" scrolling="no" height="100%" width="100%"></iframe>`
);
}
// action code
/**
* Returns an array of broadcaster IDs related to provided
* usernames.
* @returns {string[]} array of broadcaster IDs
*/
async function getBroadcasterIds() {
let endpoint = apiUrl(ACTIONS.USERS);
const searchVal = await getValueSafely("search");
if (!searchVal || !searchVal.length) {
console.warning("Search value is empty; nothing to search for");
}
searchVal.split(",").forEach((username) => {
endpoint.searchParams.append("login", username);
});
const fetcher = await fetch(endpoint, getOptions());
const res = await fetcher.json();
if (res.error) console.error(res.message);
return res.data.map((x) => x.id);
}
/**
* Retrieve clips for a specified broadcaster
* @param {string | number} broadcasterId - the broadcaster to search for
* @param {string} from - date filter start
* @param {string} to - date filter end
* @returns {any[]} clips for requested streamer with filters applied
*/
async function getClipsForBroadcasterId(broadcasterId, from, to) {
const pageSize = await getSetting("pageSizePerStreamer");
let endpoint = apiUrl(ACTIONS.CLIPS);
endpoint = setUrlSearchParams(endpoint, {
broadcaster_id: broadcasterId,
first: pageSize,
started_at: from,
ended_at: to,
});
const fetcher = await fetch(endpoint, getOptions());
const res = await fetcher.json();
if (res.error) console.error(res.message);
return res.data;
}
/**
* Get clips for provided usernames
* @returns {any[]} array of clips for all users
*/
async function getClipsForUsernames() {
const ids = await getBroadcasterIds();
const daysToSearch = await getSetting("daysToSearch");
const now = dayjs().toISOString();
const fromDate = dayjs().subtract(daysToSearch, "days").toISOString();
const clips = await Promise.all(
ids.map(async (i) => await getClipsForBroadcasterId(i, fromDate, now))
);
return clips;
}
/**
* Sync wrapper for search() so we can use it in DOM
*/
function onClickSearch() {
search();
}
/**
* Onclick action for search button. Calls API actions and creates DOM content for clips.
*/
async function search() {
$("#loader").show();
if (!$("#usernames").val().length) return;
timeago.cancel();
await setValueSafely("search", $("#usernames").val());
const store = await getStore();
let clips = await getClipsForUsernames();
clips = [].concat.apply([], clips);
const noResElem = $("#empty-no-results").hide();
if (!clips?.length) {
noResElem.css("display", "flex");
} else {
noResElem.hide();
}
$("#clips").empty();
(clips || []).forEach((c) => {
$("#clips").append(generateClipHtml(c, store.settings));
});
store.settings.usePrettyTimestamps &&
clips?.length &&
timeago.render(document.querySelectorAll(".renderable-date"));
$("#loader").hide();
}
// settings modal
/**
* Render a key-value pair
* @param {string} key The key to render
* @param {string} value The value currently selected
* @returns {string} The key-value pair rendered
*/
function renderSetting(key, value) {
const labelObj = SETTING_LABELS[key];
const label = typeof labelObj === "object" ? labelObj.text : null;
if (label === null) return;
let formattedVal;
switch (labelObj.type) {
case SETTING_TYPES.BINARY_RADIO: {
formattedVal = `<div>
<label><input type="radio" name="${key}" value="true"${
value ? " checked" : ""
}>Yes</label>
<label><input type="radio" name="${key}" value="false"${
!value ? " checked" : ""
}>No</label>
</div>`;
break;
}
case SETTING_TYPES.SIMPLE_DROPDOWN: {
const options = labelObj.options.map(
(o) => `<option${value === o ? " selected" : ""}>${o}</option$>`
);
formattedVal = `<select name="${key}">${options.join("\n")}\n</select>`;
break;
}
case SETTING_TYPES.DROPDOWN: {
const options = Object.entries(labelObj.options).map(
(o) =>
`<option value="${o[0]}"${value === o[0] ? " selected" : ""}>${
o[1]
}</option>`
);
formattedVal = `<select name="${key}">${options.join("\n")}\n</select>`;
break;
}
default: {
formattedVal = "no!";
break;
}
}
return `<div class="setting-row">
<span class="setting-label">${label}</span>
<span>${formattedVal}</span>
</div>
`;
}
/**
* Populates the modal content
*/
async function onSettingsModalOpened() {
$("#modal-settings-content").empty();
const settings = await getValueSafely("settings");
let settingElems = "";
for (const [key, value] of Object.entries(settings)) {
const row = renderSetting(key, value);
if (typeof row === "undefined") continue;
settingElems = settingElems + row;
}
// Feel free to replace this with your own attribution but remember to adhere to the project license
const credits = `<div class="credit-row"><p>SadgeClipper version ${SADGE_VER} by <a href="https://dylmye.me">dylmye</a></p></div>`;
settingElems = settingElems + credits;
$("#modal-settings-content").append(settingElems);
}
/**
* Saves the form content in local storage
*/
function onSaveSettings() {
const form = new FormData(document.getElementById("modal-settings-form"));
const updatedFields = form.entries();
let extractedFields = {};
for (const [key, value] of updatedFields) {
extractedFields[key] = value;
}
updateSettings(extractedFields);
MicroModal.close("modal-settings");
}
/**
* Empties the modal content
*/
function onSettingsModalClosed() {
$("#modal-settings-content").empty();
$("#modal-settings-content").append("<p>Loading settings...</p>");
}
// initalisation
/**
* Set up localforage and determine how many keys are in the local store
* @returns {boolean} If localforage is set up
*/
function getForageState() {
if (typeof localforage === "undefined") {
console.error(
"Unable to set up SadgeClipper storage: localforage is not supported by the browser (returns undefined)."
);
return false;
}
localforage.config({
name: "SadgeClipper",
description: "Keystore for SadgeClipper data",
version: 2,
});
return localforage.length().then((l) => {
return l > 0;
});
}
/**
* Create the local storage variables.
* Throws an error if localforage failed.
* @returns {boolean} whether localforage was set up successfully
*/
function setupStorage() {
try {
setValueSafely("search", "");
setValueSafely("accessToken", "");
setValueSafely("settings", DEFAULT_SETTINGS);
console.debug("SadgeClipper sucessfully set up for the first time.");
return true;
} catch (e) {
console.error("Unable to set up SadgeClipper storage:", e);
return false;
}
}
/**
* Set bearer token and filters in DOM from localforage
*/
async function setValues() {
const store = await getStore();
if (store.accessToken && store.accessToken.length) {
bearer_token = store.accessToken;
}
if (store.search && store.search.length) {
$("#usernames").val(store.search);
}
if (!store.settings) {
setValueSafely("settings", DEFAULT_SETTINGS);
}
}
/**
* Determine whether to show or hide the login and search elements in DOM.
*/
async function determineVisibility() {
let loginButton = $("#logged-out");
let logoutButton = $("#log-out");
let loggedOutMessage = $("#empty-logged-out");
if (!bearer_token || !bearer_token.length) {
loginButton.attr("href", LOGIN_URL);
$("#searchbar").hide();
} else {
loginButton.hide();
loggedOutMessage.hide();
logoutButton.attr("href", LOGOUT_URL);
logoutButton.show();
}
}
// setup on pageload
document.addEventListener("DOMContentLoaded", async () => {
$("html").addClass("dom-loaded");
let isSetUp = await getForageState();
if (!isSetUp) {
const successful = setupStorage();
if (!successful) return;
} else {
await setValues();
}
determineVisibility();
MicroModal.init({
onShow: onSettingsModalOpened,
onClose: onSettingsModalClosed,
openClass: "is-open",
disableScroll: true,
disableFocus: false,
awaitOpenAnimation: false,
awaitCloseAnimation: false,
debugMode: false,
});
$("#loader").hide();
});