remove base64 event listeners manually on hide instead of once: true

This commit is contained in:
schlagmichdoch 2023-03-06 12:20:30 +01:00
parent 36e152dc7c
commit c0d504f6a8
2 changed files with 38 additions and 22 deletions

View file

@ -1301,13 +1301,15 @@ class Base64ZipDialog extends Dialog {
preparePasting(type) { preparePasting(type) {
if (navigator.clipboard.readText) { if (navigator.clipboard.readText) {
this.$pasteBtn.innerText = `Tap here to paste ${type}`; this.$pasteBtn.innerText = `Tap here to paste ${type}`;
this.$pasteBtn.addEventListener('click', _ => this.processClipboard(type), { once: true }); this._clickCallback = _ => this.processClipboard(type);
this.$pasteBtn.addEventListener('click', _ => this._clickCallback());
} else { } else {
console.log("`navigator.clipboard.readText()` is not available on your browser.\nOn Firefox you can set `dom.events.asyncClipboard.readText` to true under `about:config` for convenience.") console.log("`navigator.clipboard.readText()` is not available on your browser.\nOn Firefox you can set `dom.events.asyncClipboard.readText` to true under `about:config` for convenience.")
this.$pasteBtn.setAttribute('hidden', ''); this.$pasteBtn.setAttribute('hidden', '');
this.$fallbackTextarea.setAttribute('placeholder', `Paste here to send ${type}`); this.$fallbackTextarea.setAttribute('placeholder', `Paste here to send ${type}`);
this.$fallbackTextarea.removeAttribute('hidden'); this.$fallbackTextarea.removeAttribute('hidden');
this.$fallbackTextarea.addEventListener('input', _ => this.processInput(type), { once: true }); this._inputCallback = _ => this.processInput(type);
this.$fallbackTextarea.addEventListener('input', _ => this._inputCallback());
this.$fallbackTextarea.focus(); this.$fallbackTextarea.focus();
} }
} }
@ -1315,23 +1317,28 @@ class Base64ZipDialog extends Dialog {
async processInput(type) { async processInput(type) {
const base64 = this.$fallbackTextarea.textContent; const base64 = this.$fallbackTextarea.textContent;
this.$fallbackTextarea.textContent = ''; this.$fallbackTextarea.textContent = '';
try { await this.processBase64(type, base64);
// check if input is base64 encoded
window.atob(base64);
await this.processBase64(type, base64);
} catch (e) {
// input is not base64 string. Do nothing.
}
} }
async processClipboard(type) { async processClipboard(type) {
this._setPasteBtnToProcessing();
const base64 = await navigator.clipboard.readText(); const base64 = await navigator.clipboard.readText();
await this.processBase64(type, base64); await this.processBase64(type, base64);
} }
isValidBase64(base64) {
try {
// check if input is base64 encoded
window.atob(base64);
return true;
} catch (e) {
// input is not base64 string.
return false;
}
}
async processBase64(type, base64) { async processBase64(type, base64) {
if (!base64) return; if (!base64 || !this.isValidBase64(base64)) return;
this._setPasteBtnToProcessing();
try { try {
if (type === "text") { if (type === "text") {
await this.processBase64Text(base64); await this.processBase64Text(base64);
@ -1378,6 +1385,8 @@ class Base64ZipDialog extends Dialog {
hide() { hide() {
this.clearBrowserHistory(); this.clearBrowserHistory();
this.$pasteBtn.removeEventListener('click', _ => this._clickCallback());
this.$fallbackTextarea.removeEventListener('input', _ => this._inputCallback());
super.hide(); super.hide();
} }
} }

View file

@ -1302,13 +1302,15 @@ class Base64ZipDialog extends Dialog {
preparePasting(type) { preparePasting(type) {
if (navigator.clipboard.readText) { if (navigator.clipboard.readText) {
this.$pasteBtn.innerText = `Tap here to paste ${type}`; this.$pasteBtn.innerText = `Tap here to paste ${type}`;
this.$pasteBtn.addEventListener('click', _ => this.processClipboard(type), { once: true }); this._clickCallback = _ => this.processClipboard(type);
this.$pasteBtn.addEventListener('click', _ => this._clickCallback());
} else { } else {
console.log("`navigator.clipboard.readText()` is not available on your browser.\nOn Firefox you can set `dom.events.asyncClipboard.readText` to true under `about:config` for convenience.") console.log("`navigator.clipboard.readText()` is not available on your browser.\nOn Firefox you can set `dom.events.asyncClipboard.readText` to true under `about:config` for convenience.")
this.$pasteBtn.setAttribute('hidden', ''); this.$pasteBtn.setAttribute('hidden', '');
this.$fallbackTextarea.setAttribute('placeholder', `Paste here to send ${type}`); this.$fallbackTextarea.setAttribute('placeholder', `Paste here to send ${type}`);
this.$fallbackTextarea.removeAttribute('hidden'); this.$fallbackTextarea.removeAttribute('hidden');
this.$fallbackTextarea.addEventListener('input', _ => this.processInput(type), { once: true }); this._inputCallback = _ => this.processInput(type);
this.$fallbackTextarea.addEventListener('input', _ => this._inputCallback());
this.$fallbackTextarea.focus(); this.$fallbackTextarea.focus();
} }
} }
@ -1316,23 +1318,28 @@ class Base64ZipDialog extends Dialog {
async processInput(type) { async processInput(type) {
const base64 = this.$fallbackTextarea.textContent; const base64 = this.$fallbackTextarea.textContent;
this.$fallbackTextarea.textContent = ''; this.$fallbackTextarea.textContent = '';
try { await this.processBase64(type, base64);
// check if input is base64 encoded
window.atob(base64);
await this.processBase64(type, base64);
} catch (e) {
// input is not base64 string. Do nothing.
}
} }
async processClipboard(type) { async processClipboard(type) {
this._setPasteBtnToProcessing();
const base64 = await navigator.clipboard.readText(); const base64 = await navigator.clipboard.readText();
await this.processBase64(type, base64); await this.processBase64(type, base64);
} }
isValidBase64(base64) {
try {
// check if input is base64 encoded
window.atob(base64);
return true;
} catch (e) {
// input is not base64 string.
return false;
}
}
async processBase64(type, base64) { async processBase64(type, base64) {
if (!base64) return; if (!base64 || !this.isValidBase64(base64)) return;
this._setPasteBtnToProcessing();
try { try {
if (type === "text") { if (type === "text") {
await this.processBase64Text(base64); await this.processBase64Text(base64);