aboutsummaryrefslogtreecommitdiff
path: root/html
diff options
context:
space:
mode:
authorWojtek Kosior <koszko@koszko.org>2021-07-27 11:41:43 +0200
committerWojtek Kosior <koszko@koszko.org>2021-07-27 11:41:43 +0200
commit2fa41a54acfa5e25b5ccad5b3c91210cc42ce00d (patch)
tree05564233adc57a489f0e07276c85e9e33df1d993 /html
parent64afd5b9415d62c1f178ca78a8358bd3503d5855 (diff)
downloadbrowser-extension-2fa41a54acfa5e25b5ccad5b3c91210cc42ce00d.tar.gz
browser-extension-2fa41a54acfa5e25b5ccad5b3c91210cc42ce00d.zip
validate settings on import
Diffstat (limited to 'html')
-rw-r--r--html/options_main.js44
1 files changed, 30 insertions, 14 deletions
diff --git a/html/options_main.js b/html/options_main.js
index 026b9ba..6aed8bb 100644
--- a/html/options_main.js
+++ b/html/options_main.js
@@ -12,6 +12,7 @@
* IMPORT TYPE_NAME
* IMPORT list_prefixes
* IMPORT nice_name
+ * IMPORT parse_json_with_schema
* IMPORTS_END
*/
@@ -223,6 +224,7 @@ function reset_work_bag_li(ul, item, components)
ul.work_li.insertBefore(bag_components_ul, old_components_ul);
ul.work_li.removeChild(old_components_ul);
+ console.log("bag components", components);
add_bag_components(components);
}
@@ -543,6 +545,32 @@ function read_file(file)
_read_file(file, resolve, reject));
}
+const url_regex = /^[a-z0-9]+:\/\/[^/]+\.[^/]{2}(\/[^?#]*)?$/;
+const sha256_regex = /^[0-9a-f]{64}$/;
+const component_schema = [
+ new RegExp(`^[${TYPE_PREFIX.SCRIPT}${TYPE_PREFIX.BAG}]$`),
+ /.+/
+];
+
+const settings_schema = [
+ [{}, "matchentry", "minentries", 1,
+ new RegExp(`^${TYPE_PREFIX.SCRIPT}`), {
+ /* script data */
+ "url": ["optional", url_regex],
+ "sha256": ["optional", sha256_regex],
+ "text": ["optional", "string"]
+ },
+ new RegExp(`^${TYPE_PREFIX.BAG}`), [
+ "optional",
+ [component_schema, "repeat"],
+ "default", undefined
+ ],
+ new RegExp(`^${TYPE_PREFIX.PAGE}`), {
+ /* page data */
+ "components": ["optional", component_schema]
+ }], "repeat"
+]
+
async function import_from_file(event)
{
let files = event.target.files;
@@ -555,30 +583,18 @@ async function import_from_file(event)
let result = undefined;
try {
- result = JSON.parse(await read_file(files[0]));
+ result = parse_json_with_schema(settings_schema,
+ await read_file(files[0]));
} catch(e) {
bad_file_errormsg.textContent = "" + e;
import_failed_radio.checked = true;
return;
}
- let errormsg = validate_settings(result);
- if (errormsg !== false) {
- bad_file_errormsg.textContent = errormsg;
- import_failed_radio.checked = true;
- return;
- }
-
populate_import_list(result);
import_selection_radio.checked = true;
}
-function validate_settings(settings)
-{
- // TODO
- return false;
-}
-
function import_li_id(prefix, item)
{
return `ili_${prefix}_${item}`;