This repository was archived by the owner on Mar 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
56 lines (51 loc) · 2.41 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="UTF-8">
<meta name="description" content="Simple Web Share example.">
<meta name="keywords" content="webshare, api, javascript">
</head>
<body>
<button onclick="webshare('440px-TimelessStoriesSVV1.jpg')">Share</button>
<script>
const URL = 'https://upload.wikimedia.org/wikipedia/commons/thumb/f/fb/TimelessStoriesSVV1.jpg/440px-TimelessStoriesSVV1.jpg';
const TYPE = 'image/jpeg';
const EXT = '.jpg';
const TITLE = "yourTitle";
const IS_SAFARI = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
function webshare(text) {
let navigator;
navigator = window.navigator;
let data = IS_SAFARI ? { files: [] } : { files: [], text: text, url: URL, title: TITLE };
var xhr = new XMLHttpRequest();
xhr.open('GET', URL, true);
xhr.responseType = 'arraybuffer';
xhr.onload = function() {
if (xhr.status === 200) {
var response = xhr.response;
//console.log(response);
var blob = new File([response], text + EXT, { type: TYPE });
data.files.push(blob);
//console.log(data);
if (navigator.canShare && navigator.canShare(data)) {
navigator.share(data)
.then(function() {
if (IS_SAFARI && (text !== undefined || URL !== undefined || TITLE !== undefined)) {
let dataText = { files: [], text: text, url: URL, title: TITLE };
navigator.share(dataText);
}
})
.catch(function(err) {
console.error('Unsuccessful share ' + err);
});
}
} else {
console.error('Failed to load ' + URL + ': ' + xhr.status);
}
};
xhr.send();
}
</script>
</body>
</html>