forked from sunggook/hello-pwa
-
Notifications
You must be signed in to change notification settings - Fork 1
/
share.js
98 lines (80 loc) · 2.69 KB
/
share.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
// Base code comes from http://wicg.github.io/web-share/demos/share-files.html
'use strict';
function sleep(delay) {
return new Promise(resolve => {
setTimeout(resolve, delay);
});
}
function logText(message, isError) {
if (isError)
console.error(message);
else
console.log(message);
const p = document.createElement('p');
if (isError)
p.setAttribute('class', 'error');
document.querySelector('#output').appendChild(p);
p.appendChild(document.createTextNode(message));
}
function logError(message) {
logText(message, true);
}
function checkboxChanged(e) {
const checkbox = e.target;
const textfield = document.querySelector('#' + checkbox.id.split('_')[0]);
textfield.disabled = !checkbox.checked;
if (!checkbox.checked)
textfield.value = '';
}
async function testWebShare() {
if (navigator.share === undefined) {
logError('Error: Unsupported feature: navigator.share()');
return;
}
const title_input = document.querySelector('#title');
const text_input = document.querySelector('#text');
const url_input = document.querySelector('#url');
const file_input = document.querySelector('#files');
const title = title_input.disabled ? undefined : title_input.value;
const text = text_input.disabled ? undefined : text_input.value;
const url = url_input.disabled ? undefined : url_input.value;
const files = file_input.disabled ? undefined : file_input.files;
if (files && files.length > 0) {
if (!navigator.canShare || !navigator.canShare({files})) {
logError('Error: Unsupported feature: navigator.canShare()');
return;
}
}
try {
await navigator.share({
files: files,
title: title,
text: text,
url: url
});
logText('Successfully sent share and receive');
} catch (error) {
logError('Error sharing: ' + error);
}
}
async function testWebShareDelay() {
await sleep(6000);
testWebShare();
}
function onLoad() {
// Checkboxes disable and delete textfields.
document.querySelector('#title_checkbox').addEventListener('click',
checkboxChanged);
document.querySelector('#text_checkbox').addEventListener('click',
checkboxChanged);
document.querySelector('#url_checkbox').addEventListener('click',
checkboxChanged);
document.querySelector('#share').addEventListener('click', testWebShare);
document.querySelector('#share-no-gesture').addEventListener('click',
testWebShareDelay);
if (navigator.share === undefined) {
logError('Error: You need to use a browser that supports this draft ' +
'proposal.');
}
}
window.addEventListener('load', onLoad);