-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
vhelper.html
393 lines (322 loc) · 12.2 KB
/
vhelper.html
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
---
layout: page
title: "Video Markdown & Thumbnail Tool"
permalink: /vhelper
description: ""
noindex: true
---
<style>
#vHelpLoader {
position: absolute;
inset: 0;
z-index: 1;
background-color: var(--bulma-grey);
color: var(--bulma-black);
font-weight: 600;
padding: 1rem;
border-radius: var(--bulma-radius);
display: none;
justify-content: center;
align-items: center;
}
#vHelpThumbnail {
aspect-ratio: 16/9;
width: 100%;
}
#vHelpDatePublished:user-invalid,
#vHelpDateUpdated:user-invalid {
outline: 2px solid var(--bulma-danger);
box-shadow: 0 0 15px var(--bulma-danger);
}
</style>
<section class="mt-0">
<div class="container is-max-tablet">
<div class="message is-warning">
<div class="message-header">
{{ page.title }}
</div>
<div class="message-body">
This form serves as a primitive CMS tool designed for administrators to create and manage video information in Markdown format <code>(*.md)</code> and download a thumbnail image from
YouTube. The files can be easily downloaded and copied to a specific folders (eg. <code>_clips</code>, <code>_guides</code>, <code>_surveillance-report</code> or <code>_techlore</code>).
</div>
</div>
{% include c_nojs-message.html title=page.title %}
<form id="vHelp" action="#" class="mt-5">
<div class="field">
<label class="label" for="vHelpTitle">Title <span class="has-text-danger">*</span></label>
<div class="control">
<input class="input" type="text" placeholder="Title" id="vHelpTitle" name="vHelpTitle" required>
</div>
<p class="help">Used as a title and the file name</p>
</div>
<div class="field">
<label class="label" for="vHelpDesc">Description</label>
<div class="control">
<textarea class="textarea" id="vHelpDesc" name="vHelpDesc"></textarea>
</div>
</div>
<div class="columns">
<div class="column is-half-tablet">
<div class="field">
<label class="label" for="vHelpDatePublished">datePublished <span class="has-text-danger">*</span></label>
<div class="control">
<input class="input" type="text" placeholder="YYYY-MM-DD" pattern="\d{4}-\d{2}-\d{2}"
maxlength="10" id="vHelpDatePublished" name="vHelpDatePublished">
</div>
<p class="help">format: YYYY-MM-DD</p>
</div>
</div>
<div class="column is-half-tablet">
<div class="field">
<label class="label" for="vHelpDateUpdated">dateUpdated <span class="has-text-danger">*</span></label>
<div class="control">
<input class="input" type="text" placeholder="YYYY-MM-DD" pattern="\d{4}-\d{2}-\d{2}"
maxlength="10" id="vHelpDateUpdated" name="vHelpDateUpdated">
</div>
<p class="help">format: YYYY-MM-DD</p>
</div>
</div>
</div>
<div class="field">
<label class="label" for="vHelpYouTube">YouTube link <span class="has-text-danger">*</span></label>
<div class="control">
<input class="input" type="text" placeholder="Text input" required id="vHelpYouTube" name="vHelpYouTube">
</div>
<p class="help"><span class="has-text-danger">Required for the video ID</span>; both domains works: youtube.com, youtu.be</p>
</div>
<div class="field">
<label class="label" for="vHelpForum">Forum link</label>
<div class="control">
<input class="input" type="text" placeholder="optional" id="vHelpForum" name="vHelpForum">
</div>
</div>
<div class="field">
<label class="label" for="vHelpPeerTube">PeerTube link</label>
<div class="control">
<input class="input" type="text" placeholder="optional" id="vHelpPeerTube" name="vHelpPeerTube">
</div>
</div>
<div class="field">
<label class="label" for="vHelpOdysee">Odysee link</label>
<div class="control">
<input class="input" type="text" placeholder="optional" id="vHelpOdysee" name="vHelpOdysee">
</div>
</div>
<div class="field">
<label class="label" for="vHelpTags">Tags</label>
<div class="control">
<input class="input" type="text" placeholder="optional" id="vHelpTags" name="vHelpTags">
</div>
<p class="help">Example: clips, privacy, Q&A</p>
</div>
<div class="field is-grouped is-grouped-multiline" id="vHelpTagButtons">
<!-- Filled with JS -->
</div>
<hr class="mt-6">
<div class="field">
<label class="label" for="vHelpMdFileName">1. Download *.md file</label>
</div>
<div class="field has-addons">
<div class="control is-expanded">
<input class="input" type="text" placeholder="File name will appear here" id="vHelpMdFileName" name="vHelpMdFileName" disabled>
</div>
<div class="control">
<button class="button is-primary" id="vHelpButtonCreate">
Download
</button>
</div>
</div>
<div class="field">
<div class="message is-warning">
<div class="message-body">
<p>Cross-Origin Resource Sharing (CORS) does not allow us to download YouTube thumbnails directly. Therefore:</p>
</div>
</div>
</div>
<div class="field">
<label class="label" for="vHelpThumbFileName">2. Copy file name</label>
<div class="control">
<input class="input" type="text" placeholder="File name will appear here" id="vHelpThumbFileName" name="vHelpThumbFileName">
</div>
</div>
<div class="field">
<label class="label">3. Right-click to save the thumbnail and paste the file name</label>
<div class="control is-relative">
<div id="vHelpLoader">Loading thumbnail from YouTube</div>
<img src="{% link assets/images/thumbnail-default.png %}" data-default="{% link assets/images/thumbnail-default.png %}" loading="lazy" alt="" id="vHelpThumbnail" class="has-radius-normal">
</div>
</div>
<hr class="mt-6">
<div class="field is-grouped is-grouped-centered">
<p class="control">
<button class="button is-danger" id="vHelpButtonReset">
Reset form and start again
</button>
</p>
</div>
</form>
</div>
</section>
<script type="application/javascript">
// --- helper functions
const slugifyMe = str =>
str
.toLowerCase()
.trim()
.replace(/[\s]+/g, '-')
.replace(/[^\w\-]+/g, '')
.replace(/--+/g, '-')
.replace(/^-+|-+$/g, '')
const getTodaysDate = () => {
const today = new Date();
const year = today.getFullYear()
const month = String(today.getMonth() + 1).padStart(2, '0')
const day = String(today.getDate()).padStart(2, '0')
return `${year}-${month}-${day}`
}
const copyDate = (dateFrom, dateTo) => {
if (!dateFrom || !dateTo) return
dateTo.value = dateFrom.value
}
const dowloadMarkdownFile = (info, fileName, date) => {
// --- md file
const blobMd = new Blob([info], {type: 'text/markdown'})
const urlMd = URL.createObjectURL(blobMd)
const linkMd = document.createElement('a')
linkMd.href = urlMd
linkMd.download = `${date}-${fileName}.md`
// --- append the link to the body (required for Firefox)
document.body.appendChild(linkMd)
linkMd.click()
// --- clean up and remove the link
document.body.removeChild(linkMd)
// --- free up memory
URL.revokeObjectURL(urlMd)
}
const extractVideoId = (url) => {
const regex = /(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/
const match = url.match(regex)
return match ? match[1] : null
}
const loadYouTubeThumbnail = (id) => {
if (!id) return
thumbnailLoader.style.display = 'flex'
thumbnailImage.src = `https://img.youtube.com/vi/${id}/maxresdefault.jpg`
thumbnailImage.onload = function () {
thumbnailLoader.style.display = 'none'
}
thumbnailImage.onerror = function () {
thumbnailLoader.innerText = 'Failed to load image. Check again the YouTube link.'
}
}
const updateNames = () => {
let fileNameDate = datePublished.value
let fileNameTitle = slugifyMe(title.value)
if (!fileNameTitle || !fileNameDate) return
mdFileNameInput.value = `${fileNameDate}-${fileNameTitle}.md`
thumbFileNameInput.value = `${fileNameDate}-${fileNameTitle}`
}
const doubleQuotesToHtmlEntity = (string) => {
return string.replace(/"/g, '"');
}
const addTags = (string) => {
if (!string) return
if (tags.value.includes(string)) return
if (!tags.value) {
tags.value = string
} else {
tags.value += `, ${string}`
}
}
// --- empty variables
let slugifiedTitle
let videoInfo
let videoId
// --- fields
const title = document.querySelector('#vHelpTitle')
const description = document.querySelector('#vHelpDesc')
const datePublished = document.querySelector('#vHelpDatePublished')
const dateUpdated = document.querySelector('#vHelpDateUpdated')
const linkYouTube = document.querySelector('#vHelpYouTube')
const linkForum = document.querySelector('#vHelpForum')
const linkPeerTube = document.querySelector('#vHelpPeerTube')
const linkOdysee = document.querySelector('#vHelpOdysee')
const tags = document.querySelector('#vHelpTags')
const thumbnailImage = document.querySelector('#vHelpThumbnail')
const thumbnailLoader = document.querySelector('#vHelpLoader')
const mdFileNameInput = document.querySelector('#vHelpMdFileName')
const thumbFileNameInput = document.querySelector('#vHelpThumbFileName')
// --- buttons
const buttonCreate = document.querySelector('#vHelpButtonCreate')
const buttonReset = document.querySelector('#vHelpButtonReset')
// --- initialization procedures
datePublished.value = getTodaysDate()
dateUpdated.value = getTodaysDate()
// --- create files button
buttonCreate.addEventListener('click', (e) => {
e.preventDefault()
// --- strings preparation
slugifiedTitle = slugifyMe(title.value)
const tagsSplit = tags.value.split(", ").map(item => item.trim());
const tagsStringified = JSON.stringify(tagsSplit);
// --- MD file content
videoInfo = `---\n`
videoInfo += `${title.value ? 'title: "' + doubleQuotesToHtmlEntity(title.value) + '"\n' : ''}`
videoInfo += `${description.value ? 'description: "' + doubleQuotesToHtmlEntity(description.value) + '"\n' : ''}`
videoInfo += `${datePublished.value ? 'datePublished: ' + datePublished.value + '\n' : ''}`
videoInfo += `${dateUpdated.value ? 'dateUpdated: ' + dateUpdated.value + '\n' : ''}`
videoInfo += `${linkYouTube.value ? 'linkYouTube: "' + linkYouTube.value + '"\n' : ''}`
videoInfo += `${linkForum.value ? 'linkForum: "' + linkForum.value + '"\n' : ''}`
videoInfo += `${linkPeerTube.value ? 'linkPeerTube: "' + linkPeerTube.value + '"\n' : ''}`
videoInfo += `${linkOdysee.value ? 'linkOdysee: "' + linkOdysee.value + '"\n' : ''}`
videoInfo += `${tags.value ? 'tags: ' + tagsStringified + '\n' : ''}`
videoInfo += `---`
dowloadMarkdownFile(videoInfo, slugifiedTitle, datePublished.value)
})
// --- handling file names
title.addEventListener('change', () => {
updateNames()
})
datePublished.addEventListener('change', () => {
updateNames()
copyDate(datePublished, dateUpdated)
})
thumbFileNameInput.addEventListener('focus', (e) => {
e.target.select()
})
// --- loading youtube thumbnail
linkYouTube.addEventListener('change', () => {
if (!linkYouTube.value || !linkYouTube.value.includes('yout')) return
videoId = extractVideoId(linkYouTube.value)
loadYouTubeThumbnail(videoId)
})
// --- reset form
buttonReset.addEventListener('click', () => {
document.querySelector('#vHelp').reset()
datePublished.value = getTodaysDate()
dateUpdated.value = getTodaysDate()
thumbnailImage.src = thumbnailImage.dataset.default
})
// --- generate tag buttons
const generateTagButtons = (tagButtonLabels, tagsContainer) => {
const container = document.getElementById(tagsContainer)
if (!container) return
container.innerHTML = '';
tagButtonLabels.forEach(label => {
const tagButtonWrapper = document.createElement('div')
const tagButton = document.createElement('button')
tagButtonWrapper.classList.add('control')
tagButton.setAttribute('class', 'button is-small')
tagButton.textContent = label
tagButton.setAttribute('data-label', label)
tagButtonWrapper.appendChild(tagButton)
tagButton.addEventListener('click', (e) => {
e.preventDefault()
addTags(label)
})
container.appendChild(tagButtonWrapper)
})
}
const tagLabels = ['SR', 'Q&A', 'Clips', 'Guide', 'Data breach', 'Security', 'Privacy', 'Interview', 'Review', 'Livestream']
generateTagButtons(tagLabels, 'vHelpTagButtons')
</script>