Skip to content

Commit 6685668

Browse files
authored
Demo: Disable buttons during share (#192)
While the promise has not yet resolved or rejected, we disable the share button to prevent additional share attempts. This avoids confusion arising from [[sharePromise]] internal slot (#113)
1 parent 16b339e commit 6685668

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

‎demos/share-files.html

+10-5
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ <h1>Web Share Test</h1>
6969
logText(message, true);
7070
}
7171

72+
function setShareButtonsEnabled(enabled) {
73+
document.querySelector('#share').disabled = !enabled;
74+
document.querySelector('#share-no-gesture').disabled = !enabled;
75+
}
76+
7277
function checkboxChanged(e) {
7378
const checkbox = e.target;
7479
const textfield = document.querySelector('#' + checkbox.id.split('_')[0]);
@@ -79,11 +84,6 @@ <h1>Web Share Test</h1>
7984
}
8085

8186
async function testWebShare() {
82-
if (navigator.share === undefined) {
83-
logError('Error: Unsupported feature: navigator.share()');
84-
return;
85-
}
86-
8787
const title_input = document.querySelector('#title');
8888
const text_input = document.querySelector('#text');
8989
const url_input = document.querySelector('#url');
@@ -97,19 +97,23 @@ <h1>Web Share Test</h1>
9797
if (files && files.length > 0) {
9898
if (!navigator.canShare || !navigator.canShare({files})) {
9999
logError('Error: Unsupported feature: navigator.canShare()');
100+
setShareButtonsEnabled(true);
100101
return;
101102
}
102103
}
103104

105+
setShareButtonsEnabled(false);
104106
try {
105107
await navigator.share({files, title, text, url});
106108
logText('Successfully sent share');
107109
} catch (error) {
108110
logError('Error sharing: ' + error);
109111
}
112+
setShareButtonsEnabled(true);
110113
}
111114

112115
async function testWebShareDelay() {
116+
setShareButtonsEnabled(false);
113117
await sleep(6000);
114118
testWebShare();
115119
}
@@ -128,6 +132,7 @@ <h1>Web Share Test</h1>
128132
testWebShareDelay);
129133

130134
if (navigator.share === undefined) {
135+
setShareButtonsEnabled(false);
131136
if (window.location.protocol === 'http:') {
132137
// navigator.share() is only available in secure contexts.
133138
window.location.replace(window.location.href.replace(/^http:/, 'https:'));

‎demos/share.html

+9-5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ <h1>Web Share Test</h1>
6565
logText(message, true);
6666
}
6767

68+
function setShareButtonsEnabled(enabled) {
69+
document.querySelector('#share').disabled = !enabled;
70+
document.querySelector('#share-no-gesture').disabled = !enabled;
71+
}
72+
6873
function checkboxChanged(e) {
6974
const checkbox = e.target;
7075
const textfield = document.querySelector('#' + checkbox.id.split('_')[0]);
@@ -75,27 +80,25 @@ <h1>Web Share Test</h1>
7580
}
7681

7782
async function testWebShare() {
78-
if (navigator.share === undefined) {
79-
logError('Error: Unsupported feature: navigator.share');
80-
return;
81-
}
82-
8383
const title_input = document.querySelector('#title');
8484
const text_input = document.querySelector('#text');
8585
const url_input = document.querySelector('#url');
8686

8787
const title = title_input.disabled ? undefined : title_input.value;
8888
const text = text_input.disabled ? undefined : text_input.value;
8989
const url = url_input.disabled ? undefined : url_input.value;
90+
setShareButtonsEnabled(false);
9091
try {
9192
await navigator.share({title, text, url});
9293
logText('Successfully sent share');
9394
} catch (error) {
9495
logError('Error sharing: ' + error);
9596
}
97+
setShareButtonsEnabled(true);
9698
}
9799

98100
async function testWebShareDelay() {
101+
setShareButtonsEnabled(false);
99102
await sleep(6000);
100103
testWebShare();
101104
}
@@ -114,6 +117,7 @@ <h1>Web Share Test</h1>
114117
testWebShareDelay);
115118

116119
if (navigator.share === undefined) {
120+
setShareButtonsEnabled(false);
117121
if (window.location.protocol === 'http:') {
118122
// navigator.share() is only available in secure contexts.
119123
window.location.replace(window.location.href.replace(/^http:/, 'https:'));

0 commit comments

Comments
 (0)