verify QR paste JSON and allow non-JSON pastes
This commit is contained in:
parent
896d43ade7
commit
03eb04e8f9
|
@ -239,6 +239,28 @@ export class QuickReplySet {
|
|||
this.save();
|
||||
return qr;
|
||||
}
|
||||
addQuickReplyFromText(qrJson) {
|
||||
let data;
|
||||
try {
|
||||
data = JSON.parse(qrJson ?? '{}');
|
||||
delete data.id;
|
||||
} catch {
|
||||
// not JSON data
|
||||
}
|
||||
if (data) {
|
||||
// JSON data
|
||||
if (data.label === undefined || data.message === undefined) {
|
||||
// not a QR
|
||||
toastr.error('Not a QR.');
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// no JSON, use plaintext as QR message
|
||||
data = { message: qrJson };
|
||||
}
|
||||
const newQr = this.addQuickReply(data);
|
||||
return newQr;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -250,11 +272,8 @@ export class QuickReplySet {
|
|||
qr.onDelete = ()=>this.removeQuickReply(qr);
|
||||
qr.onUpdate = ()=>this.save();
|
||||
qr.onInsertBefore = (qrJson)=>{
|
||||
const data = JSON.parse(qrJson ?? '{}');
|
||||
delete data.id;
|
||||
log('onInsertBefore', data);
|
||||
const newQr = this.addQuickReply(data);
|
||||
this.qrList.pop();
|
||||
this.addQuickReplyFromText(qrJson);
|
||||
const newQr = this.qrList.pop();
|
||||
this.qrList.splice(this.qrList.indexOf(qr), 0, newQr);
|
||||
if (qr.settingsDom) {
|
||||
qr.settingsDom.insertAdjacentElement('beforebegin', newQr.settingsDom);
|
||||
|
|
|
@ -121,7 +121,7 @@ export class SettingsUi {
|
|||
});
|
||||
this.dom.querySelector('#qr--set-paste').addEventListener('click', async()=>{
|
||||
const text = await navigator.clipboard.readText();
|
||||
this.currentQrSet.addQuickReply(JSON.parse(text));
|
||||
this.currentQrSet.addQuickReplyFromText(text);
|
||||
});
|
||||
this.dom.querySelector('#qr--set-importQr').addEventListener('click', async()=>{
|
||||
const inp = document.createElement('input'); {
|
||||
|
|
Loading…
Reference in New Issue