From d0ae39390fba3822aed4a498aafcdfa61289448b Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Fri, 2 Jul 2021 11:55:13 +0200 Subject: enable opening settings page with certain item immediately in edit mode --- html/options.html | 1 + html/options_main.js | 43 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/html/options.html b/html/options.html index 03978c7..092327a 100644 --- a/html/options.html +++ b/html/options.html @@ -123,6 +123,7 @@ + diff --git a/html/options_main.js b/html/options_main.js index 6f203fa..9c66a27 100644 --- a/html/options_main.js +++ b/html/options_main.js @@ -11,6 +11,7 @@ * IMPORT TYPE_PREFIX * IMPORT TYPE_NAME * IMPORT list_prefixes + * IMPORT url_extract_target * IMPORTS_END */ @@ -304,6 +305,12 @@ function edit_item(prefix, item) let ul = ul_by_prefix[prefix]; let li = by_id(item_li_id(prefix, item)); + + if (li === null) { + add_new_item(prefix, item); + return; + } + ul.reset_work_li(ul, item, storage.get(prefix, item)); ul.ul.insertBefore(ul.work_li, li); ul.ul.removeChild(li); @@ -357,7 +364,7 @@ function export_item(prefix, name) URL.revokeObjectURL(url); } -function add_new_item(prefix) +function add_new_item(prefix, name) { cancel_work(prefix); @@ -366,6 +373,8 @@ function add_new_item(prefix) ul.work_li.classList.remove("hide"); ul.ul.appendChild(ul.work_li); + if (name !== undefined) + ul.work_name_input.value = name; ul.state = UL_STATE.ADDING_ENTRY; } @@ -672,6 +681,36 @@ function initialize_import_facility() cancel_import_but.addEventListener("click", hide_import_window); } +/* + * If url has a target appended, e.g. + * chrome-extension://hnhmbnpohhlmhehionjgongbnfdnabdl/html/options.html#smyhax + * that target will be split into prefix and item name (e.g. "s" and "myhax") + * and editing of that respective item will be started. + * + * We don't need to worry about the state of the page (e.g. some editing being + * in progress) in jump_to_item() - this function is called at the beginning, + * before callbacks are assigned to buttons, so it is safe to assume lists are + * initialized with items and page is in its virgin state with regard to + * everything else. + */ +function jump_to_item(url_with_item) +{ + const parsed_url = url_extract_target(url_with_item); + + if (parsed_url.target === undefined) + return; + + const prefix = parsed_url.target.substring(1, 2); + + if (!list_prefixes.includes(prefix)) { + history.replaceState(null, "", parsed_url.base_url); + return; + } + + by_id(`show_${TYPE_NAME[prefix]}s`).checked = true; + edit_item(prefix, decodeURIComponent(parsed_url.target.substring(2))); +} + async function main() { storage = await get_remote_storage(); @@ -683,6 +722,8 @@ async function main() add_radio_li(prefix, item); } + jump_to_item(document.URL); + let name = TYPE_NAME[prefix]; let add_but = by_id(`add_${name}_but`); -- cgit v1.2.3