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,22 +99,27 @@ function Game(props: GameProps) {
}; };
async function share(url: string, copiedHint: string, text?: string) { async function share(url: string, copiedHint: string, text?: string) {
if (
/android|iphone|ipad|ipod|webos/i.test(navigator.userAgent) &&
!/firefox/i.test(navigator.userAgent)
) {
try { try {
await navigator.share({ url, text }); await navigator.share({ url, text });
return; return;
} catch (e) { } catch (e) {
console.warn("navigator.share failed:", e); console.warn("navigator.share failed:", e);
}
}
try { try {
const body = url + (text ? "\n\n" + text : ""); const body = url + (text ? "\n\n" + text : "");
await navigator.clipboard.writeText(body); await navigator.clipboard.writeText(body);
setHint(copiedHint); setHint(copiedHint);
return; return;
} catch (e2) { } catch (e) {
console.warn("navigator.clipboard.writeText failed:", e2); console.warn("navigator.clipboard.writeText failed:", e);
}
setHint(url); setHint(url);
} }
}
}
const onKey = (key: string) => { const onKey = (key: string) => {
if (gameState !== GameState.Playing) { if (gameState !== GameState.Playing) {
@@ -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(