interface.js: add json-b64 datatype
This commit is contained in:
parent
caa1fccea3
commit
c6a6908a82
@ -25,7 +25,7 @@ class InterfaceElement {
|
|||||||
|
|
||||||
class Form extends InterfaceElement {
|
class Form extends InterfaceElement {
|
||||||
constructor({id, tag}) {
|
constructor({id, tag}) {
|
||||||
super(id, tag);
|
super({id, tag});
|
||||||
|
|
||||||
if (tag === undefined) {
|
if (tag === undefined) {
|
||||||
this.handle = document.createElement("div");
|
this.handle = document.createElement("div");
|
||||||
@ -90,7 +90,7 @@ class FormElement extends InterfaceElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
constructor({id, type, form, tag, label="", dataType="plaintext", advanced=false, enabled=true}) {
|
constructor({id, type, form, tag, label="", dataType="plaintext", advanced=false, enabled=true}) {
|
||||||
super(id);
|
super({id});
|
||||||
this.id = id;
|
this.id = id;
|
||||||
|
|
||||||
this.advanced = advanced;
|
this.advanced = advanced;
|
||||||
@ -150,6 +150,7 @@ class FormElement extends InterfaceElement {
|
|||||||
|
|
||||||
// plaintext is string data
|
// plaintext is string data
|
||||||
// b64 is raw ArrayBuffer data
|
// b64 is raw ArrayBuffer data
|
||||||
|
// json-b64 is Object data
|
||||||
// or none, which gives undefined
|
// or none, which gives undefined
|
||||||
#dataType = "none";
|
#dataType = "none";
|
||||||
get dataType() {
|
get dataType() {
|
||||||
@ -163,6 +164,7 @@ class FormElement extends InterfaceElement {
|
|||||||
switch (x) {
|
switch (x) {
|
||||||
case "plaintext":
|
case "plaintext":
|
||||||
case "b64":
|
case "b64":
|
||||||
|
case "json-b64":
|
||||||
let valid = ["textbox", "password", "textarea", "output"];
|
let valid = ["textbox", "password", "textarea", "output"];
|
||||||
if (!valid.includes(this.type)) err(this.type, x);
|
if (!valid.includes(this.type)) err(this.type, x);
|
||||||
break;
|
break;
|
||||||
@ -188,6 +190,20 @@ class FormElement extends InterfaceElement {
|
|||||||
this.alertBox("alert-error", "Invalid base64 value.");
|
this.alertBox("alert-error", "Invalid base64 value.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
case "json-b64":
|
||||||
|
let jsonString;
|
||||||
|
try {
|
||||||
|
jsonString = atob(this.handle.value);
|
||||||
|
} catch (e) {
|
||||||
|
this.alertBox("alert-error", "Invalid base64 value.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return JSON.parse(jsonString);
|
||||||
|
} catch (e) {
|
||||||
|
this.alertBox("alert-error", "Invalid JSON encoding.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
case "none":
|
case "none":
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
@ -196,8 +212,13 @@ class FormElement extends InterfaceElement {
|
|||||||
switch (this.dataType) {
|
switch (this.dataType) {
|
||||||
case "plaintext":
|
case "plaintext":
|
||||||
this.handle.value = x;
|
this.handle.value = x;
|
||||||
|
break;
|
||||||
case "b64":
|
case "b64":
|
||||||
this.handle.value = bufToB64(x);
|
this.handle.value = bufToB64(x);
|
||||||
|
break;
|
||||||
|
case "json-b64":
|
||||||
|
this.handle.value = btoa(JSON.stringify(x));
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user