diff --git a/map4.js b/map4.js index 0c20e36..48d60ed 100644 --- a/map4.js +++ b/map4.js @@ -36,9 +36,8 @@ let stationMarkers = []; /* ------------------------------------------------------ - スマホ対応:招待リンクコピー機能(textarea方式) + iPhone完全対応:確実にコピーできる招待リンク機能 ------------------------------------------------------- */ - const inviteBtn = document.getElementById("inviteBtn"); if (inviteBtn) { @@ -62,27 +61,38 @@ } const pass = data.password; - const inviteURL = `https://www.yatex.org/gitbucket/KoekiGameDesign/2025-shino/pages/join_group.html` + `?group=${encodeURIComponent(currentGroup)}&pass=${encodeURIComponent(pass)}`; - // ---- スマホでも確実にコピーできる処理 ---- - const temp = document.createElement("textarea"); - temp.value = inviteURL; - document.body.appendChild(temp); - - temp.select(); - temp.setSelectionRange(0, 99999); // iPhone対応 - - try { - document.execCommand("copy"); - alert("📋 招待リンクをコピーしました!"); - } catch (e) { - alert("コピーに失敗しました…"); + /* ---------- ① まず clipboard API を試す ---------- */ + if (navigator.clipboard && navigator.clipboard.writeText) { + try { + await navigator.clipboard.writeText(inviteURL); + alert("📋 招待リンクをコピーしました!"); + return; + } catch (e) { + console.log("clipboard API 失敗 → フォールバックへ", e); + } } - document.body.removeChild(temp); + /* ---------- ② iPhone用:選択して手動コピーしてもらう ---------- */ + + // 一時的な入力ボックスを表示 + const tempInput = document.createElement("input"); + tempInput.value = inviteURL; + tempInput.style.position = "fixed"; + tempInput.style.top = "-9999px"; + document.body.appendChild(tempInput); + + // iPhone用:必ず選択状態にする + tempInput.focus(); + tempInput.select(); + tempInput.setSelectionRange(0, 99999); + + alert("📱 コピーに対応していないブラウザです。\n\nリンクが選択されたので、もう一度『コピー』をタップしてください。"); + + document.body.removeChild(tempInput); }); } // ===================================