-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfrontend-settings.js
More file actions
394 lines (356 loc) · 12.1 KB
/
frontend-settings.js
File metadata and controls
394 lines (356 loc) · 12.1 KB
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
let allSettings = [
// 'hashtags',
'youtubeHashtags',
'tiktokHashtags',
'delay',
'minViewCount',
'uploadFrequency',
'hotkey',
'youtubeTags',
'youtubePrivacy',
'youtubeDescription'
];
let canBeBlank = [
'youtubeHashtags',
'tiktokHashtags',
'youtubeTags',
'youtubePrivacy',
'youtubeDescription'
];
// Listen to submit event on the <form> itself!
document.addEventListener('DOMContentLoaded', async function (event) {
document.getElementById('update').addEventListener('submit', async (e) => {
e.preventDefault();
// must match id of element in html
let params = {};
let settingsWereChanged = false;
for (setting of allSettings) {
params[setting] = document.querySelector('#' + setting).value;
if (params[setting]) {
settingsWereChanged = true;
} else if (!canBeBlank.includes(setting)) {
delete params[setting];
}
}
if (params.youtubeTags) {
console.log('youtubeTags: ' + typeof params.youtubeTags);
params.youtubeTags = params.youtubeTags.split(',');
params.youtubeTags = params.youtubeTags.map((tag) => tag.trim());
console.log('youtubeTags fixed: ' + params.youtubeTags?.length);
}
params['defaultApprove'] =
document.querySelector('#defaultApprove').checked;
params['uploadEnabled'] = document.querySelector('#uploadEnabled').checked;
params['tiktokUploadEnabled'] = document.querySelector(
'#tiktokUploadEnabled'
).checked;
params['youtubeUploadEnabled'] = document.querySelector(
'#youtubeUploadEnabled'
).checked;
params['fastUploadEnabled'] =
document.querySelector('#fastUploadEnabled').checked;
params['youtubeAutoCategorizationEnabled'] = document.querySelector(
'#youtubeAutoCategorizationEnabled'
).checked;
settingsWereChanged = true;
let url = new URL('http://localhost:42074/update');
if (!settingsWereChanged) {
SafeSwal.fire({
icon: 'info',
text: 'No settings changed',
});
return false;
}
if (!checkFieldsAreValid()) {
return false;
}
Object.keys(params).forEach((key) => {
if (params[key] != undefined) {
url.searchParams.append(key, params[key]);
}
});
try {
console.log('fetching: ' + url);
await fetch(url);
updateFields(params);
hideSettingsNotSavedPopup();
SafeSwal.fire('Settings updated!');
ipcRenderer.send('settings_updated');
return true;
} catch {
return false;
}
});
});
const rotateChevron = (chevronSelector) => {
let chevron = document.querySelector(chevronSelector);
chevron.classList.toggle('rotate90');
};
document.addEventListener('DOMContentLoaded', async function (event) {
document
.getElementById('clipDefaultsHeader')
.addEventListener('click', async (e) => {
e.preventDefault();
document.querySelector('#clipDefaults').classList.toggle('hidden');
rotateChevron('#clipDefaultsChevron');
});
document
.getElementById('AdvancedSettingsHeader')
.addEventListener('click', async (e) => {
e.preventDefault();
document.querySelector('#AdvancedSettings').classList.toggle('hidden');
rotateChevron('#AdvancedSettingsChevron');
});
document
.getElementById('uploadSettingsHeader')
.addEventListener('click', async (e) => {
e.preventDefault();
document.querySelector('#uploadSettings').classList.toggle('hidden');
rotateChevron('#uploadSettingsChevron');
});
});
let updateFields = (fields) => {
for (fieldName in fields) {
let fieldSpan = document.querySelector('#' + fieldName); //+ "-value"
console.log('name:' + fieldName);
console.log('span:' + fieldSpan);
console.log('val:' + fields[fieldName]);
if (
((fields[fieldName] !== '' && fields[fieldName] !== undefined) ||
canBeBlank.includes(fieldName)) &&
fieldSpan
) {
if (
fieldName == 'defaultApprove' ||
fieldName.toLowerCase().includes('upload') ||
fieldName.toLowerCase().includes('categorization')
) {
fieldSpan.checked = JSON.parse(fields[fieldName]);
}
fieldSpan.value = '';
fieldSpan.placeholder = fields[fieldName];
if (fieldName == 'youtubePrivacy') {
fieldSpan.value = fields[fieldName];
}
if (fieldName == 'delay' || fieldName == 'uploadFrequency') {
fieldSpan.placeholder += ' hours';
} else if (fieldName == 'minViewCount') {
fieldSpan.placeholder += ' views';
} else {
fieldSpan.value = fields[fieldName];
}
}
}
};
let checkFieldsAreValid = () => {
const hashtagRegex = /^\s*(#\w+\s)*#\w+\s*$/;
const commaSeparatedWithManyWordsAllowedRegex =
/^\s*((\w+\s*)*,\s*)*(\w+\s*)*$/;
// Ensure entire hashtags string is space separated and every tag starts with #
// let hashtagsValue = document.querySelector('#hashtags').value;
// if (hashtagsValue) {
// let hashtagsAreValid = hashtagRegex.test(hashtagsValue);
// if (!hashtagsAreValid) {
// SafeSwal.fire({
// icon: 'error',
// text: 'Looks like your hashtags are wrong, make sure to use the format: #tag #anothertag #athirdtag',
// });
// return false;
// }
// }
let hashtagsValue = document.querySelector('#youtubeHashtags').value;
if (hashtagsValue) {
let hashtagsAreValid = hashtagRegex.test(hashtagsValue);
if (!hashtagsAreValid) {
SafeSwal.fire({
icon: 'error',
text: 'Looks like your Youtube hashtags are wrong, make sure to use the format: #tag #anothertag #athirdtag',
});
return false;
}
}
hashtagsValue = document.querySelector('#tiktokHashtags').value;
if (hashtagsValue) {
let hashtagsAreValid = hashtagRegex.test(hashtagsValue);
if (!hashtagsAreValid) {
SafeSwal.fire({
icon: 'error',
text: 'Looks like your Tiktok hashtags are wrong, make sure to use the format: #tag #anothertag #athirdtag',
});
return false;
}
}
ytTagsValue = document.querySelector('#youtubeTags').value;
if (ytTagsValue) {
let ytTagsAreValid =
commaSeparatedWithManyWordsAllowedRegex.test(ytTagsValue);
if (!ytTagsAreValid) {
SafeSwal.fire({
icon: 'error',
text: 'Looks like your youtube tags are wrong, make sure to use the format: tag1, tag2, long tag 3, tag4',
});
return false;
}
}
let delayValue = document.querySelector('#delay').value;
if (delayValue) {
let delayIsValid = delayValue >= 0;
if (!delayIsValid) {
SafeSwal.fire({
icon: 'error',
text: 'Looks like your delay is incorrect, make sure to use a number greater than or equal to 0',
});
return false;
}
}
let minViewCountValue = document.querySelector('#minViewCount').value;
if (minViewCountValue) {
let minViewCountIsValid = minViewCountValue >= 0;
if (!minViewCountIsValid) {
SafeSwal.fire({
icon: 'error',
text: 'Looks like your Min View Count is incorrect, make sure to use a number greater than or equal to 0',
});
return false;
}
}
let uploadFrequencyValue = document.querySelector('#uploadFrequency').value;
if (uploadFrequencyValue) {
let uploadFrequencyIsValid = uploadFrequencyValue >= 0;
if (!uploadFrequencyIsValid) {
SafeSwal.fire({
icon: 'error',
text: 'Looks like your Upload Frequency is incorrect, make sure to use a number greater than or equal to 0',
});
return false;
}
}
return true;
};
// Set interval for querying backend to publish clips
// Call backend settings endpoint to get current settings
document.addEventListener('DOMContentLoaded', async function (event) {
let result = await fetch('http://localhost:42074/settings');
if (result.status == 200) {
result.json().then((settings) => {
updateFields(settings);
});
}
});
document.addEventListener('DOMContentLoaded', async function (event) {
// console.log( 'renderer: ' + ipcRenderer);
let ipcRenderer = window.ipcRenderer;
if (!ipcRenderer) {
try {
ipcRenderer = window.require('electron').ipcRenderer;
} catch {
try {
ipcRenderer = require('electron').ipcRenderer;
} catch {
console.log('rip');
}
}
}
});
// Call backend settings endpoint to get current settings
let descriptions = {
hashtags: `
Clipbot uses the title from your clip to create a description.
<br/><br/>
On top of this, you can add hashtags to your tiktok descriptions to help your clips get found!
<br/><br/>
We recommend ~3-6 hashtags. Even simple ones like #twitch or #twitchstreamer are good!
<br/><br/>
Format: A list of hashtags seperated by spaces<br/>
Example: "#twitchstreamer #streamer #twitchclip"
`,
delay: `
How many hours should we wait to upload your newest clip?
<br/><br/>
If you're an affiliate or partner, you're required to wait 24 hours before posting content to other platforms.
<br/><br/>
So, for most people, this should be set to 24 hours.
<br/><br/>
If you're not an affiliate and not a partner, you can set this to 0.
<br/><br/>
Format: Positive number (hours)<br/>
Example: 24
`,
minViewCount: `
How many views should a clip have before we upload it?
<br/><br/>
If you're popular and get lots of clips, you may not want to upload them all.
<br/><br/>
This setting allows you to require a video to have at least X views to be uploaded.
<br/><br/>
Most small streamers can leave this as 0.
<br/><br/>
Format: Positive number (views)<br/>
Example: 3
`,
uploadFrequency: `
How often should we upload to TikTok? (hours)
<br/><br/>
Every X hours, we will try to upload another clip to TikTok.
<br/><br/>
While you have control over this, be careful with this setting. If you you set it too low, you may get banned from TikTok.
<br/><br/>
We recommend uploading at most 3-4 tiktoks a day, and therefore setting this to either 6 or 8 hours.
<br/><br/>
Format: Positive number (hours)<br/>
Example: 6
`,
youtubeHashtags: `
Hashtags are keywords preceded by a # symbol. Hashtags allow creators to easily connect their content with other videos that share the same hashtag on YouTube. They also allow viewers to quickly find similar content that shares the same hashtag.
<br/><br/>
A list of hashtags seperated by spaces<br/>
Example: #twitchstreamer #roxkstar74 #twitchclip
`,
youtubeTags: `
Tags are descriptive keywords you can add to your video to help viewers find your content. Your video's title, thumbnail and description are more important pieces of metadata for your video's discovery. These main pieces of information help viewers to decide which videos to watch.
<br/><br/>
Separate yours tags by using a comma.
<br/>
Example: twitch,streamer
`,
youtubePrivacy: `
Update the privacy settings of your video to control where your video can appear and who can watch it.
<br/><br/>
`,
youtubeDescription: `
Update the description of your video with links to your twitch channel, tiktok, and whatever else!
`
};
document.addEventListener('DOMContentLoaded', async function (event) {
let fields = Object.keys(descriptions);
for (field of fields) {
let whatsThis = document.querySelector(
`#${field}-form > label > div > span.what`
);
if (whatsThis) {
let storedField = field;
let desc = descriptions[field];
whatsThis.addEventListener('click', () => {
SafeSwal.fire({
title: storedField
.split(/(?=[A-Z])/)
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(' '),
html: desc,
});
});
}
}
});
document.addEventListener('DOMContentLoaded', async function (event) {
document.querySelector('#close').addEventListener('click', () => {
window.close();
});
});
console.log('garbo');
let showSettingsNotSavedPopup = () => {
document.getElementById('notsaved').style.display = 'block';
};
let hideSettingsNotSavedPopup = () => {
document.getElementById('notsaved').style.display = 'none';
};