-
Notifications
You must be signed in to change notification settings - Fork 0
/
copyURLToClipboard.js
60 lines (53 loc) · 1.6 KB
/
copyURLToClipboard.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
function fallbackCopyTextToClipboard(text) {
var textArea = document.createElement("textarea");
textArea.value = text;
// Avoid scrolling to bottom
textArea.style.top = "0";
textArea.style.left = "0";
textArea.style.position = "fixed";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try { document.execCommand('copy'); }
finally { document.body.removeChild(textArea); }
}
function copyTextToClipboard(text) {
if (!navigator.clipboard) {
fallbackCopyTextToClipboard(text);
return;
}
navigator.clipboard.writeText(text);
}
function downloadText(name, text) {
var bb = new Blob([text], { type: 'text/plain' });
var a = document.createElement('a');
a.download = name;
a.href = window.URL.createObjectURL(bb);
a.click();
}
function getFileFromURL(url)
{
fetch(url)
.then((res) => { return res.blob(); })
.then((data) => {
var a = document.createElement("a");
var name = url.substring(url.lastIndexOf('/')+1)
a.href = window.URL.createObjectURL(data);
a.download = name;
a.click();
});
}
function getTextFromURL(url) {
http = new XMLHttpRequest();
http.open("GET", url, true);
http.onreadystatechange = function () {
// console.log("state changed!");
if (http.readyState === XMLHttpRequest.DONE) {
var status = http.status;
if (status === 0 || (status >= 200 && status < 400)) {
copyTextToClipboard(http.responseText);
}
}
}
http.send(null);
}