Skip to content

Commit bfee33b

Browse files
committed
Add room code clipboard fallback
1 parent 20bf20d commit bfee33b

1 file changed

Lines changed: 50 additions & 4 deletions

File tree

static/index.html

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,12 +1180,58 @@ <h1>{{gameTypeName(selectedGameType)}}大厅</h1>
11801180
code = String(code || '');
11811181
if (!code) return;
11821182
var done = function () { layer.msg('房号已复制:' + code); };
1183-
if (navigator.clipboard && navigator.clipboard.writeText) {
1184-
navigator.clipboard.writeText(code).then(done).catch(function () {
1185-
layer.msg('房号:' + code);
1183+
var escapeHtml = function (text) {
1184+
return String(text).replace(/[&<>"']/g, function (ch) {
1185+
return ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' })[ch];
1186+
});
1187+
};
1188+
var showManualCopy = function () {
1189+
layer.msg('复制失败,请手动复制房号:' + code);
1190+
layer.open({
1191+
type: 1,
1192+
title: '手动复制房号',
1193+
shadeClose: true,
1194+
area: ['min(320px, 90vw)', 'auto'],
1195+
content: '<div style="padding:18px;text-align:center;">' +
1196+
'<div style="margin-bottom:10px;color:#666;">复制失败,请手动复制房号:</div>' +
1197+
'<textarea readonly style="box-sizing:border-box;width:100%;min-height:52px;padding:12px;text-align:center;font-size:22px;font-weight:bold;letter-spacing:2px;resize:none;" onclick="this.select()">' + escapeHtml(code) + '</textarea>' +
1198+
'</div>',
1199+
success: function (layero) {
1200+
var field = layero && layero[0] ? layero[0].querySelector('textarea') : null;
1201+
if (field) {
1202+
field.focus();
1203+
field.select();
1204+
}
1205+
}
11861206
});
1207+
};
1208+
var fallbackCopy = function () {
1209+
var field = document.createElement('textarea');
1210+
field.value = code;
1211+
field.setAttribute('readonly', 'readonly');
1212+
field.style.position = 'fixed';
1213+
field.style.left = '-9999px';
1214+
field.style.top = '0';
1215+
document.body.appendChild(field);
1216+
field.focus();
1217+
field.select();
1218+
var ok = false;
1219+
try {
1220+
ok = document.execCommand('copy');
1221+
} catch (e) {
1222+
ok = false;
1223+
}
1224+
document.body.removeChild(field);
1225+
if (ok) {
1226+
done();
1227+
} else {
1228+
showManualCopy();
1229+
}
1230+
};
1231+
if (navigator.clipboard && navigator.clipboard.writeText) {
1232+
navigator.clipboard.writeText(code).then(done).catch(fallbackCopy);
11871233
} else {
1188-
layer.msg('房号:' + code);
1234+
fallbackCopy();
11891235
}
11901236
},
11911237
openRules: function () {

0 commit comments

Comments
 (0)