-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.js
161 lines (142 loc) · 5.13 KB
/
url.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
function validateUrl(url1) {
var url = String(url1);
if (url.startsWith("http://") || url.startsWith("https://")) {
return true;
} else {
return false;
}
}
function func() {
var x = document.getElementById("input-longurl").value;
var url = `https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=${x}`;
var img = document.getElementById("img");
img.src = url;
}
const init = function () {
document.getElementById('button-send').addEventListener('click', send);
}
const send = function (ev) {
ev.preventDefault();
ev.stopPropagation();
var longUrl = document.getElementById('input-longurl').value;
func();
if (validateUrl(longUrl)) {
const Http = new XMLHttpRequest();
const baseUrl = `https://tinyurl.com/api-create.php?url=`;
let url = baseUrl + longUrl;
Http.open("GET", url);
Http.send();
Http.onreadystatechange = function () {
document.getElementById('msg1').textContent = 'Long Url :';
document.getElementById('longurl').textContent = longUrl;
document.getElementById('longurl').href = longUrl;
document.getElementById('msg2').textContent = 'Short Url :';
document.getElementById('shorturl').textContent = this.responseText;
document.getElementById('shorturl').href = this.responseText;
}
}
else {
document.getElementById('msg1').textContent = 'Note :';
document.getElementById('longurl').textContent = 'Invadlid Link';
document.getElementById('longurl').removeAttribute("href");
document.getElementById('msg2').textContent = '';
document.getElementById('shorturl').textContent = 'https:// or http:// format only';
document.getElementById('shorturl').removeAttribute("href");
}
}
window.addEventListener('load', () => {
document.getElementById('button-send')
.addEventListener('click', triggerDownload);
document.getElementById('input-longurl')
.addEventListener('keypress', e => {
if (e.key === 'Enter') {
triggerDownload(e);
}
})
});
const triggerDownload = async e => {
let url = document.getElementById('img').src;
url = new URL(url);
let response = await fetch(url);
let blob = await response.blob();
const blobUrl = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = blobUrl;
const extension = extensions[blob.type];
link.download = extension != null ? `QR.` + extension : 'file';
link.style.display = 'none';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
const extensions = {
'text/html': 'html',
'text/css': 'css',
'text/xml': 'xml',
'image/gif': 'gif',
'image/jpeg': 'jpeg',
'application/x-javascript': 'js',
'application/atom+xml': 'atom',
'application/rss+xml': 'rss',
'text/mathml': 'mml',
'text/plain': 'txt',
'text/vnd.sun.j2me.app-descriptor': 'jad',
'text/vnd.wap.wml': 'wml',
'text/x-component': 'htc',
'image/png': 'png',
'image/tiff': 'tif',
'image/vnd.wap.wbmp': 'wbmp',
'image/x-icon': 'ico',
'image/x-jng': 'jng',
'image/x-ms-bmp': 'bmp',
'image/svg+xml': 'svg',
'image/webp': 'webp',
'application/java-archive': 'jar',
'application/mac-binhex40': 'hqx',
'application/msword': 'doc',
'application/pdf': 'pdf',
'application/postscript': 'ps',
'application/rtf': 'rtf',
'application/vnd.ms-excel': 'xls',
'application/vnd.ms-powerpoint': 'ppt',
'application/vnd.wap.wmlc': 'wmlc',
'application/vnd.google-earth.kml+xml': 'kml',
'application/vnd.google-earth.kmz': 'kmz',
'application/x-7z-compressed': '7z',
'application/x-cocoa': 'cco',
'application/x-java-archive-diff': 'jardiff',
'application/x-java-jnlp-file': 'jnlp',
'application/x-makeself': 'run',
'application/x-perl': 'pl',
'application/x-pilot': 'prc',
'application/x-rar-compressed': 'rar',
'application/x-redhat-package-manager': 'rpm',
'application/x-sea': 'sea',
'application/x-shockwave-flash': 'swf',
'application/x-stuffit': 'sit',
'application/x-tcl': 'tcl',
'application/x-x509-ca-cert': 'der',
'application/x-xpinstall': 'xpi',
'application/xhtml+xml': 'xhtml',
'application/zip': 'zip',
'application/octet-stream': 'bin',
'application/octet-stream': 'deb',
'application/octet-stream': 'dmg',
'application/octet-stream': 'eot',
'application/octet-stream': 'iso',
'application/octet-stream': 'msi',
'audio/midi': 'mid',
'audio/mpeg': 'mp3',
'audio/ogg': 'ogg',
'audio/x-realaudio': 'ra',
'video/3gpp': '3gpp',
'video/mpeg': 'mpeg',
'video/quicktime': 'mov',
'video/x-flv': 'flv',
'video/x-mng': 'mng',
'video/x-ms-asf': 'asx',
'video/x-ms-wmv': 'wmv',
'video/x-msvideo': 'avi',
'video/mp4': 'm4v'
};
document.addEventListener('DOMContentLoaded', init);