Only try to use navigator.share on mobile

This commit is contained in:
Lynn
2022-01-24 17:35:56 +01:00
parent 3a4a7767cc
commit 672b7143ee

View File

@@ -99,21 +99,26 @@ function Game(props: GameProps) {
}; };
async function share(url: string, copiedHint: string, text?: string) { async function share(url: string, copiedHint: string, text?: string) {
try { if (
await navigator.share({ url, text }); /android|iphone|ipad|ipod|webos/i.test(navigator.userAgent) &&
return; !/firefox/i.test(navigator.userAgent)
} catch (e) { ) {
console.warn("navigator.share failed:", e);
try { try {
const body = url + (text ? "\n\n" + text : ""); await navigator.share({ url, text });
await navigator.clipboard.writeText(body);
setHint(copiedHint);
return; return;
} catch (e2) { } catch (e) {
console.warn("navigator.clipboard.writeText failed:", e2); console.warn("navigator.share failed:", e);
setHint(url);
} }
} }
try {
const body = url + (text ? "\n\n" + text : "");
await navigator.clipboard.writeText(body);
setHint(copiedHint);
return;
} catch (e) {
console.warn("navigator.clipboard.writeText failed:", e);
}
setHint(url);
} }
const onKey = (key: string) => { const onKey = (key: string) => {
@@ -287,8 +292,7 @@ function Game(props: GameProps) {
}} }}
> >
Challenge a friend to this word Challenge a friend to this word
</button> </button>{" "}
{" "}
<button <button
onClick={() => { onClick={() => {
share( share(