-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
523 lines (492 loc) · 33.1 KB
/
worker.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
const CACHE_EXPIRED_TIME = 14400; //Cache Expiration Time (in seconds)
const CACHE_MAX_SIZE = 300 * 1024 * 1024;
const DEFAULT_LANG = 'en'; // English set as the default language.
const JQUERY_SRC = 'https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js';
const TOAST_SRC = 'https://cdn.jsdelivr.net/npm/[email protected]/dist/bulma-toast.min.js';
const BULMA_CSS_SRC = 'https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css';
const ANIMATE_CSS_SRC = 'https://cdn.jsdelivr.net/npm/[email protected]/animate.min.css';
/**
* Multilingual Support
*/
const LANGUAGES = {
en: {
title: 'Download Link Generator',
enterURL: 'Enter URL:',
enterFileName: 'Enter File Name:',
generateLink: 'Generate Accelerated Link',
reset: 'Reset',
generateUrlLabel: 'Generate URL:',
copyLink: 'Copy link',
downloadLink: 'Download link',
copiedSuccess: 'Text copied to clipboard',
failedToCopy: 'Failed to copy text:',
isInvilidUrl: 'Is invalid url',
githubText: 'Powered by Cloudflare-Workers-Cache',
disclaimerText: `
<p>Disclaimer</p>
<p>Before using the tool, please read and understand this statement carefully. Your use of the tool constitutes acceptance of this disclaimer.</p>
<p>I. Reliability of Service</p>
<p>The tool is developed based on the Cloudflare Workers platform and relies on the cloud computing resources provided by the platform. Therefore, the tool is subject to the reliability of the Cloudflare platform service. If the Cloudflare platform service is interrupted, malfunctioning or other technical issues, it may cause the tool to be unusable. Users who use the tool need to take their own risks and responsibilities.</p>
<p>II. Legality of Cached Content</p>
<p>The cache function of the tool is only used for caching the content of the specified URL webpage, and cannot guarantee the authenticity, legality, and accuracy of the webpage content. If the webpage content violates relevant laws and regulations or infringes on the legitimate rights and interests of others, users who use the tool to cache the content need to take corresponding legal responsibilities and compensation responsibilities.</p>
<p>III. Other Responsibilities</p>
<p>The developer of the tool provides possible assistance for the legitimate use of the tool, but does not make any promise to satisfy the specific needs of users, be free of errors or uninterrupted. Any risks and consequences arising from the use of the tool are borne by the user.</p>
<p>IV. Precautions</p>
<ol>
<li>Please use the tool reasonably and comply with the regulations of the Cloudflare platform and relevant laws and regulations.</li>
<li>Before using the tool, please ensure that you have sufficient computer technical knowledge and experience to ensure that users use the tool correctly as instructed.</li>
<li>If errors or security vulnerabilities are found in the tool, please contact the developer in time and actively cooperate with the relevant departments for security monitoring and investigation work.</li>
<li>The developer reserves the right to modify this disclaimer and the terms of use of the tool at any time, and the final interpretation right belongs to the developer.</li>
</ol>
<p>If any terms of this statement are inconsistent with relevant laws and regulations, these terms will be re-interpreted in accordance with the provisions of laws and regulations, while other terms will remain valid.</p>
`
},
zh: {
title: '下载链接生成器',
enterURL: '输入URL:',
enterFileName: '输入文件名:',
generateLink: '生成加速链接',
reset: '重置',
generateUrlLabel: '生成链接地址:',
copyLink: '复制链接',
downloadLink: '下载链接',
copiedSuccess: '已复制到剪贴板',
failedToCopy: '复制失败:',
isInvilidUrl: 'URL 地址无效',
githubText: '由 Cloudflare-Workers-Cache 提供支持',
disclaimerText: `
<p>免责声明</p>
<p>在使用本工具之前,请仔细阅读并理解本声明。您的使用将视为接受本免责声明。</p>
<p>I. 服务可靠性</p>
<p>该工具是基于Cloudflare Workers平台开发,并依赖于平台提供的云计算资源。因此,该工具受Cloudflare平台服务可靠性的影响。如果Cloudflare平台服务中断、故障或出现其他技术问题,可能导致该工具无法使用。使用该工具的用户需要自行承担风险和责任。</p>
<p>II. 缓存内容的合法性</p>
<p>该工具的缓存功能仅用于缓存指定URL网页的内容,不能保证网页内容的真实性、合法性和准确性。如果网页内容违反相关法律法规或侵犯他人合法权益,使用该工具缓存内容的用户需要承担相应的法律责任和赔偿责任。</p>
<p>III. 其他责任</p>
<p>该工具的开发者为合法使用该工具提供可能的协助,但不对满足用户特定需求、无错或无中断作出任何承诺。使用该工具导致的任何风险和后果由用户承担。</p>
<p>IV. 注意事项</p>
<ol>
<li>请合理使用工具并遵守Cloudflare平台的规定和相关法律法规。</li>
<li>在使用该工具之前,请确保您具有足够的计算机技术知识和经验,以确保用户按照指示正确使用该工具。</li>
<li>如果发现该工具存在错误或安全漏洞,请及时联系开发者,并积极配合相关部门进行安全监测和调查工作。</li>
<li>开发者保留随时修改本免责声明和该工具使用条款的权利,最终解释权归开发者所有。</li>
</ol>
<p>如果本声明任何条款与相关法律法规不一致,则这些条款将按照法律法规的规定重新解释,而其他条款仍然有效。</p>
`
},
fr: {
title: 'Générateur de lien de téléchargement',
enterURL: 'Entrez l\'URL :',
enterFileName: 'Entrez le nom de fichier :',
generateLink: 'Générer un lien accéléré',
reset: 'Réinitialiser',
generateUrlLabel: 'Générer l\'URL :',
copyLink: 'Copier le lien',
downloadLink: 'Télécharger le lien',
copiedSuccess: 'Texte copié dans le presse-papiers',
failedToCopy: 'Impossible de copier le texte :',
githubText: 'Propulsé par Cloudflare-Workers-Cache',
disclaimerText: `
<p>Disclaimer</p>
<p>Before using the tool, please read and understand this statement carefully. Your use of the tool constitutes acceptance of this disclaimer.</p>
<p>I. Reliability of Service</p>
<p>The tool is developed based on the Cloudflare Workers platform and relies on the cloud computing resources provided by the platform. Therefore, the tool is subject to the reliability of the Cloudflare platform service. If the Cloudflare platform service is interrupted, malfunctioning or other technical issues, it may cause the tool to be unusable. Users who use the tool need to take their own risks and responsibilities.</p>
<p>II. Legality of Cached Content</p>
<p>The cache function of the tool is only used for caching the content of the specified URL webpage, and cannot guarantee the authenticity, legality, and accuracy of the webpage content. If the webpage content violates relevant laws and regulations or infringes on the legitimate rights and interests of others, users who use the tool to cache the content need to take corresponding legal responsibilities and compensation responsibilities.</p>
<p>III. Other Responsibilities</p>
<p>The developer of the tool provides possible assistance for the legitimate use of the tool, but does not make any promise to satisfy the specific needs of users, be free of errors or uninterrupted. Any risks and consequences arising from the use of the tool are borne by the user.</p>
<p>IV. Precautions</p>
<ol>
<li>Please use the tool reasonably and comply with the regulations of the Cloudflare platform and relevant laws and regulations.</li>
<li>Before using the tool, please ensure that you have sufficient computer technical knowledge and experience to ensure that users use the tool correctly as instructed.</li>
<li>If errors or security vulnerabilities are found in the tool, please contact the developer in time and actively cooperate with the relevant departments for security monitoring and investigation work.</li>
<li>The developer reserves the right to modify this disclaimer and the terms of use of the tool at any time, and the final interpretation right belongs to the developer.</li>
</ol>
<p>If any terms of this statement are inconsistent with relevant laws and regulations, these terms will be re-interpreted in accordance with the provisions of laws and regulations, while other terms will remain valid.</p>
`
},
es: {
title: 'Generador de enlaces de descarga',
enterURL: 'Introduce la URL:',
enterFileName: 'Introduce el nombre de archivo:',
generateLink: 'Generar enlace acelerado',
reset: 'Reiniciar',
generateUrlLabel: 'Generar URL:',
copyLink: 'Copiar enlace',
downloadLink: 'Enlace de descarga',
copiedSuccess: 'Texto copiado al portapapeles',
failedToCopy: 'Error al copiar el texto: ',
githubText: 'Desarrollado por Cloudflare-Workers-Cache',
disclaimerText: `
<p>Disclaimer</p>
<p>Before using the tool, please read and understand this statement carefully. Your use of the tool constitutes acceptance of this disclaimer.</p>
<p>I. Reliability of Service</p>
<p>The tool is developed based on the Cloudflare Workers platform and relies on the cloud computing resources provided by the platform. Therefore, the tool is subject to the reliability of the Cloudflare platform service. If the Cloudflare platform service is interrupted, malfunctioning or other technical issues, it may cause the tool to be unusable. Users who use the tool need to take their own risks and responsibilities.</p>
<p>II. Legality of Cached Content</p>
<p>The cache function of the tool is only used for caching the content of the specified URL webpage, and cannot guarantee the authenticity, legality, and accuracy of the webpage content. If the webpage content violates relevant laws and regulations or infringes on the legitimate rights and interests of others, users who use the tool to cache the content need to take corresponding legal responsibilities and compensation responsibilities.</p>
<p>III. Other Responsibilities</p>
<p>The developer of the tool provides possible assistance for the legitimate use of the tool, but does not make any promise to satisfy the specific needs of users, be free of errors or uninterrupted. Any risks and consequences arising from the use of the tool are borne by the user.</p>
<p>IV. Precautions</p>
<ol>
<li>Please use the tool reasonably and comply with the regulations of the Cloudflare platform and relevant laws and regulations.</li>
<li>Before using the tool, please ensure that you have sufficient computer technical knowledge and experience to ensure that users use the tool correctly as instructed.</li>
<li>If errors or security vulnerabilities are found in the tool, please contact the developer in time and actively cooperate with the relevant departments for security monitoring and investigation work.</li>
<li>The developer reserves the right to modify this disclaimer and the terms of use of the tool at any time, and the final interpretation right belongs to the developer.</li>
</ol>
<p>If any terms of this statement are inconsistent with relevant laws and regulations, these terms will be re-interpreted in accordance with the provisions of laws and regulations, while other terms will remain valid.</p>
`
},
de: {
title: 'Download-Link-Generator',
enterURL: 'URL eingeben:',
enterFileName: 'Dateiname eingeben:',
generateLink: 'Beschleunigten Link generieren',
reset: 'Zurücksetzen',
generateUrlLabel: 'Generierte URL:',
copyLink: 'Link kopieren',
downloadLink: 'Download-Link',
copiedSuccess: 'Text in die Zwischenablage kopiert',
failedToCopy: 'Fehler beim Kopieren des Textes: ',
githubText: 'Betrieben von Cloudflare-Workers-Cache',
disclaimerText: `
<p>Disclaimer</p>
<p>Before using the tool, please read and understand this statement carefully. Your use of the tool constitutes acceptance of this disclaimer.</p>
<p>I. Reliability of Service</p>
<p>The tool is developed based on the Cloudflare Workers platform and relies on the cloud computing resources provided by the platform. Therefore, the tool is subject to the reliability of the Cloudflare platform service. If the Cloudflare platform service is interrupted, malfunctioning or other technical issues, it may cause the tool to be unusable. Users who use the tool need to take their own risks and responsibilities.</p>
<p>II. Legality of Cached Content</p>
<p>The cache function of the tool is only used for caching the content of the specified URL webpage, and cannot guarantee the authenticity, legality, and accuracy of the webpage content. If the webpage content violates relevant laws and regulations or infringes on the legitimate rights and interests of others, users who use the tool to cache the content need to take corresponding legal responsibilities and compensation responsibilities.</p>
<p>III. Other Responsibilities</p>
<p>The developer of the tool provides possible assistance for the legitimate use of the tool, but does not make any promise to satisfy the specific needs of users, be free of errors or uninterrupted. Any risks and consequences arising from the use of the tool are borne by the user.</p>
<p>IV. Precautions</p>
<ol>
<li>Please use the tool reasonably and comply with the regulations of the Cloudflare platform and relevant laws and regulations.</li>
<li>Before using the tool, please ensure that you have sufficient computer technical knowledge and experience to ensure that users use the tool correctly as instructed.</li>
<li>If errors or security vulnerabilities are found in the tool, please contact the developer in time and actively cooperate with the relevant departments for security monitoring and investigation work.</li>
<li>The developer reserves the right to modify this disclaimer and the terms of use of the tool at any time, and the final interpretation right belongs to the developer.</li>
</ol>
<p>If any terms of this statement are inconsistent with relevant laws and regulations, these terms will be re-interpreted in accordance with the provisions of laws and regulations, while other terms will remain valid.</p>
`
},
ar: {
title: 'مولد روابط التنزيل',
enterURL: 'أدخل الرابط:',
enterFileName: 'أدخل اسم الملف:',
generateLink: 'إنشاء رابط مسرع',
reset: 'إعادة تعيين',
generateUrlLabel: 'إنشاء رابط',
copyLink: 'نسخ الرابط',
downloadLink: 'رابط التنزيل',
copiedSuccess: 'تم نسخ النص إلى الحافظة',
failedToCopy: 'فشل نسخ النص: ',
isInvilidUrl: 'الرابط غير صالح',
githubText: 'مشغل بواسطة Cloudflare-Workers-Cache',
disclaimerText: `
<p>Disclaimer</p>
<p>Before using the tool, please read and understand this statement carefully. Your use of the tool constitutes acceptance of this disclaimer.</p>
<p>I. Reliability of Service</p>
<p>The tool is developed based on the Cloudflare Workers platform and relies on the cloud computing resources provided by the platform. Therefore, the tool is subject to the reliability of the Cloudflare platform service. If the Cloudflare platform service is interrupted, malfunctioning or other technical issues, it may cause the tool to be unusable. Users who use the tool need to take their own risks and responsibilities.</p>
<p>II. Legality of Cached Content</p>
<p>The cache function of the tool is only used for caching the content of the specified URL webpage, and cannot guarantee the authenticity, legality, and accuracy of the webpage content. If the webpage content violates relevant laws and regulations or infringes on the legitimate rights and interests of others, users who use the tool to cache the content need to take corresponding legal responsibilities and compensation responsibilities.</p>
<p>III. Other Responsibilities</p>
<p>The developer of the tool provides possible assistance for the legitimate use of the tool, but does not make any promise to satisfy the specific needs of users, be free of errors or uninterrupted. Any risks and consequences arising from the use of the tool are borne by the user.</p>
<p>IV. Precautions</p>
<ol>
<li>Please use the tool reasonably and comply with the regulations of the Cloudflare platform and relevant laws and regulations.</li>
<li>Before using the tool, please ensure that you have sufficient computer technical knowledge and experience to ensure that users use the tool correctly as instructed.</li>
<li>If errors or security vulnerabilities are found in the tool, please contact the developer in time and actively cooperate with the relevant departments for security monitoring and investigation work.</li>
<li>The developer reserves the right to modify this disclaimer and the terms of use of the tool at any time, and the final interpretation right belongs to the developer.</li>
</ol>
<p>If any terms of this statement are inconsistent with relevant laws and regulations, these terms will be re-interpreted in accordance with the provisions of laws and regulations, while other terms will remain valid.</p>
`
},
ja: {
title: 'ダウンロードリンクジェネレーター',
enterURL: 'URL を入力してください:',
enterFileName: 'ファイル名を入力してください:',
generateLink: '加速リンクを生成',
reset: 'リセット',
generateUrlLabel: 'URL を生成',
copyLink: 'リンクをコピー',
downloadLink: 'ダウンロードリンク',
copiedSuccess: 'テキストがクリップボードにコピーされました',
failedToCopy: 'テキストのコピーに失敗しました:',
isInvilidUrl: '無効なURLです',
githubText: 'Cloudflare-Workers-Cacheによって支援されています',
disclaimerText: `
<p>Disclaimer</p>
<p>Before using the tool, please read and understand this statement carefully. Your use of the tool constitutes acceptance of this disclaimer.</p>
<p>I. Reliability of Service</p>
<p>The tool is developed based on the Cloudflare Workers platform and relies on the cloud computing resources provided by the platform. Therefore, the tool is subject to the reliability of the Cloudflare platform service. If the Cloudflare platform service is interrupted, malfunctioning or other technical issues, it may cause the tool to be unusable. Users who use the tool need to take their own risks and responsibilities.</p>
<p>II. Legality of Cached Content</p>
<p>The cache function of the tool is only used for caching the content of the specified URL webpage, and cannot guarantee the authenticity, legality, and accuracy of the webpage content. If the webpage content violates relevant laws and regulations or infringes on the legitimate rights and interests of others, users who use the tool to cache the content need to take corresponding legal responsibilities and compensation responsibilities.</p>
<p>III. Other Responsibilities</p>
<p>The developer of the tool provides possible assistance for the legitimate use of the tool, but does not make any promise to satisfy the specific needs of users, be free of errors or uninterrupted. Any risks and consequences arising from the use of the tool are borne by the user.</p>
<p>IV. Precautions</p>
<ol>
<li>Please use the tool reasonably and comply with the regulations of the Cloudflare platform and relevant laws and regulations.</li>
<li>Before using the tool, please ensure that you have sufficient computer technical knowledge and experience to ensure that users use the tool correctly as instructed.</li>
<li>If errors or security vulnerabilities are found in the tool, please contact the developer in time and actively cooperate with the relevant departments for security monitoring and investigation work.</li>
<li>The developer reserves the right to modify this disclaimer and the terms of use of the tool at any time, and the final interpretation right belongs to the developer.</li>
</ol>
<p>If any terms of this statement are inconsistent with relevant laws and regulations, these terms will be re-interpreted in accordance with the provisions of laws and regulations, while other terms will remain valid.</p>
`
},
ko: {
title: '다운로드 링크 생성기',
enterURL: 'URL을 입력하십시오 :',
enterFileName: '파일 이름을 입력하세요 :',
generateLink: '가속 링크 생성',
reset: '초기화',
generateUrlLabel: 'URL 생성 :',
copyLink: '링크 복사',
downloadLink: '다운로드 링크',
copiedSuccess: '텍스트가 클립 보드에 복사되었습니다',
failedToCopy: '텍스트 복사 실패 :',
isInvilidUrl: '잘못된 URL 입니다',
githubText: 'Cloudflare-Workers-Cache의 지원으로 작동됨',
disclaimerText: `
<p>Disclaimer</p>
<p>Before using the tool, please read and understand this statement carefully. Your use of the tool constitutes acceptance of this disclaimer.</p>
<p>I. Reliability of Service</p>
<p>The tool is developed based on the Cloudflare Workers platform and relies on the cloud computing resources provided by the platform. Therefore, the tool is subject to the reliability of the Cloudflare platform service. If the Cloudflare platform service is interrupted, malfunctioning or other technical issues, it may cause the tool to be unusable. Users who use the tool need to take their own risks and responsibilities.</p>
<p>II. Legality of Cached Content</p>
<p>The cache function of the tool is only used for caching the content of the specified URL webpage, and cannot guarantee the authenticity, legality, and accuracy of the webpage content. If the webpage content violates relevant laws and regulations or infringes on the legitimate rights and interests of others, users who use the tool to cache the content need to take corresponding legal responsibilities and compensation responsibilities.</p>
<p>III. Other Responsibilities</p>
<p>The developer of the tool provides possible assistance for the legitimate use of the tool, but does not make any promise to satisfy the specific needs of users, be free of errors or uninterrupted. Any risks and consequences arising from the use of the tool are borne by the user.</p>
<p>IV. Precautions</p>
<ol>
<li>Please use the tool reasonably and comply with the regulations of the Cloudflare platform and relevant laws and regulations.</li>
<li>Before using the tool, please ensure that you have sufficient computer technical knowledge and experience to ensure that users use the tool correctly as instructed.</li>
<li>If errors or security vulnerabilities are found in the tool, please contact the developer in time and actively cooperate with the relevant departments for security monitoring and investigation work.</li>
<li>The developer reserves the right to modify this disclaimer and the terms of use of the tool at any time, and the final interpretation right belongs to the developer.</li>
</ol>
<p>If any terms of this statement are inconsistent with relevant laws and regulations, these terms will be re-interpreted in accordance with the provisions of laws and regulations, while other terms will remain valid.</p>
`
}
}
const HTML = (lang) => `
<!DOCTYPE html>
<html lang="${lang}">
<head>
<meta charset="UTF-8">
<title>${LANGUAGES[lang].title}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="${BULMA_CSS_SRC}">
<link rel="stylesheet" href="${ANIMATE_CSS_SRC}"/>
<script defer src="https://use.fontawesome.com/releases/v5.14.0/js/all.js"></script>
<script src="${JQUERY_SRC}"></script>
<script src="${TOAST_SRC}"></script>
<style>
#result {
margin-top: 20px;
font-size: 1rem;
}
.input {
margin-top: .5rem;
}
.button {
margin-top: .5rem;
}
.github-link {
display: flex;
justify-content: center;
align-items: center;
margin-top: 30px;
font-size: 1rem;
color: #4a4a4a;
text-decoration: none;
}
.github-icon {
font-size: 1.2rem;
margin-right: 5px;
}
</style>
</head>
<body>
<section class="section">
<div class="container">
<h1 class="title has-text-centered">
<i class="fas fa-cloud-download-alt"></i>
${LANGUAGES[lang].title}
</h1>
<div class="columns is-centered">
<div class="column">
<form>
<div class="field">
<label class="label" for="url">${LANGUAGES[lang].enterURL}</label>
<div class="control">
<textarea class="textarea is-primary" type="text" id="url" name="url" rows="3" placeholder="https://example.com" autofocus></textarea>
</div>
</div>
<div class="field">
<label class="label" for="filename">${LANGUAGES[lang].enterFileName}</label>
<div class="control">
<input class="input is-primary" type="text" id="filename" name="filename" value="">
</div>
</div>
<div class="field is-grouped">
<div class="control">
<button class="button is-primary" type="submit">${LANGUAGES[lang].generateLink}</button>
</div>
<div class="control">
<button class="button is-link is-light" type="reset">${LANGUAGES[lang].reset}</button>
</div>
</div>
</form>
</div>
</div>
<div class="columns is-centered">
<div class="column">
<div id="result"></div>
</div>
</div>
<div class="content has-text-centered">
<div class="github-link">
<a href="https://github.com/chenzd123456/Cloudflare-Workers-Cache" target="_blank" rel="noopener" class="has-text-weight-bold">
<i class="fab fa-github github-icon"></i> ${LANGUAGES[lang].githubText}
</a>
</div>
</div>
<footer class="footer">
<div class="content disclaimer">
<p>${LANGUAGES[lang].disclaimerText}</p>
</div>
</footer>
</div>
</section>
<script>
var $form = $('form');
var $resultDiv = $('#result');
function getFileName(url) {
var regex = new RegExp('\/([^\/?#]+)[^\/]*$');
var match = url.match(regex);
return match ? match[1] : 'download';
}
function isValidUrl(url) {
try {
new URL(url);
return true;
} catch (error) {
return false;
}
}
function toBase64(str) {
try {
return btoa(str);
} catch (error) {
console.error(error);
return null;
}
}
function getAcceleratedUrl(apiUrl, url, filename = '') {
var base64 = toBase64(url);
if (!base64) {
return null;
}
var generateUrl = apiUrl + filename + '?url=' + base64;
return generateUrl;
}
function copyToClipboard() {
var generateUrlInput = $('#generate-url');
var textToCopy = generateUrlInput.val();
var textarea = document.createElement('textarea');
textarea.value = textToCopy;
textarea.setAttribute('style', 'opacity:0;');
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
bulmaToast.toast({
message: '${LANGUAGES[lang].copiedSuccess}',
type: 'is-success',
position: 'top-center',
duration: 3000,
animate: {in: 'fadeIn', out: 'fadeOut'}
});
}
function addResult(generateUrl) {
var $box = $('<div class="box has-background-light"></div>');
var $label = $('<label class="label" for="url"></label>').text('${LANGUAGES[lang].generateUrlLabel}');
var $textarea = $('<textarea class="textarea is-primary is-static" id="generate-url" rows="3" readonly></textarea>').val(generateUrl);
var $buttonGroup = $('<div class="field is-grouped mt-3 buttons"></div>');
var $copyButton = $('<button class="button is-info"></button>').text('${LANGUAGES[lang].copyLink}').click(copyToClipboard);
var $downloadButton = $('<a class="button is-info" href="' + generateUrl + '" target="_blank"></a>').text('${LANGUAGES[lang].downloadLink}');
$box.append($label, $textarea, $buttonGroup);
$buttonGroup.append($copyButton, $downloadButton);
$resultDiv.html($box);
}
function removeResult() {
$resultDiv.html('');
}
$form.on('submit', event => {
event.preventDefault();
removeResult();
var url = $('#url').val().trim();
if(!isValidUrl(url)) {
bulmaToast.toast({
message: '${LANGUAGES[lang].isInvilidUrl}',
type: 'is-danger',
position: 'top-center',
duration: 3000,
animate: {in: 'fadeIn', out: 'fadeOut'}
});
return;
}
var filename = $('#filename').val().trim();
if (!filename) {
filename = getFileName(url);
}
var apiUrl = window.location.href;
var generateUrl = getAcceleratedUrl(apiUrl, url, filename);
console.log(generateUrl);
removeResult();
addResult(generateUrl);
});
$form.on('reset', event => {
removeResult();
});
</script>
</body>
</html>
`;
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
})
async function handleRequest(request) {
// Check if the query parameters contain a URL encoded in base64.
const urlParam = new URL(request.url).searchParams.get('url')
const language = request.headers.get('accept-language');
const langCode = language ? language.split(',')[0].substring(0, 2) : DEFAULT_LANG;
if (!urlParam) {
const response = new Response(HTML(langCode), {
headers: {
'Content-Type': 'text/html;charset=UTF-8',
},
})
return response;
}
// Decode the URL encoded in base64 to its original URL.
const url = atob(urlParam);
// Send a request for the original resource.
const cache = caches.default;
let response = await cache.match(url);
let fileSize = 0;
if (!response) {
response = await fetch(url)
fileSize = Number(response.headers.get('content-length'));
if(fileSize <= CACHE_MAX_SIZE) {
await cache.put(url, response.clone())
}
}
let headers_override = {}
headers_override['Cache-Control'] = `public, max-age=${CACHE_EXPIRED_TIME}`; // Set the cache duration for the resource.
if(fileSize != 0) {
headers_override['Content-Length'] = `${fileSize}`;
}
return new Response(response.body, {
status: response.status,
headers: {
...response.headers,
...headers_override
}
})
}