diff options
Diffstat (limited to 'html/popup.js')
-rw-r--r-- | html/popup.js | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/html/popup.js b/html/popup.js new file mode 100644 index 0000000..1efc4b0 --- /dev/null +++ b/html/popup.js @@ -0,0 +1,140 @@ +/** + * This file is part of Haketilo. + * + * Function: Show details of how Haketilo handled given page, drive popup. + * + * Copyright (C) 2021,2022 Wojtek Kosior + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * As additional permission under GNU GPL version 3 section 7, you + * may distribute forms of that code without the copy of the GNU + * GPL normally required by section 4, provided you include this + * license notice and, in case of non-source distribution, a URL + * through which recipients can access the Corresponding Source. + * If you modify file(s) with this exception, you may extend this + * exception to your version of the file(s), but you are not + * obligated to do so. If you do not wish to do so, delete this + * exception statement from your version. + * + * As a special exception to the GPL, any HTML file which merely + * makes function calls to this code, and for that purpose + * includes it by reference shall be deemed a separate work for + * copyright law purposes. If you modify this code, you may extend + * this exception to your version of the code, but you are not + * obligated to do so. If you do not wish to do so, delete this + * exception statement from your version. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + * + * I, Wojtek Kosior, thereby promise not to sue for violation of this file's + * license. Although I request that you do not make use of this code in a + * proprietary program, I am not going to enforce this in court. + */ + +#FROM common/browser.js IMPORT browser +#FROM common/misc.js IMPORT is_privileged_url +#FROM html/DOM_helpers.js IMPORT by_id +#FROM html/repo_query.js IMPORT RepoQueryView + +const tab_query = {currentWindow: true, active: true}; + +async function get_current_tab() { +#IF CHROMIUM + const callback = (cb) => browser.tabs.query(tab_query, tab => cb(tab)); + const promise = new Promise(callback); +#ELIF MOZILLA + const promise = browser.tabs.query(tab_query); +#ENDIF + + try { + return (await promise)[0]; + } catch(e) { + console.log(e); + } +} + +async function get_page_info(tab_id) { + return browser.tabs.sendMessage(tab_id, ["page_info"]); +} + +function show_page_info(page_info) { + by_id("loading_info").remove(); + by_id("info_form").classList.remove("hide"); + by_id("page_url").innerText = page_info.url; + + if (page_info.privileged) { + by_id("privileged_page_info").classList.remove("hide"); + } else { + by_id("unprivileged_page_info").classList.remove("hide"); + + by_id("scripts_blocked").innerText = page_info.allow ? "no" : "yes"; + + by_id("injected_payload").innerText = page_info.payload ? + page_info.payload.identifier : "None"; + + const scripts_fate = page_info.allow ? "allowed" : "blocked"; + + if (page_info.mapping === "~allow") + var mapping = `None (scripts ${scripts_fate} by a rule)`; + else if (page_info.mapping) + var mapping = page_info.mapping; + else + var mapping = `None (scripts ${scripts_fate} by default policy)`; + by_id("mapping_used").innerText = mapping; + } +} + +function repo_query_showing(show) { + for (const [id, i] of [["repo_query", 0], ["page_info", 1]]) + by_id(`${id}_container`).classList[["add", "remove"][show ^ i]]("hide"); +} + +function prepare_repo_query_view(tab_id, page_info) { + const repo_query_view = new RepoQueryView(tab_id, + () => repo_query_showing(true), + () => repo_query_showing(false)); + by_id("repo_query_container").prepend(repo_query_view.main_div); + + let search_cb = () => repo_query_view.show(page_info.url); + search_cb = repo_query_view.when_hidden(search_cb); + by_id("search_resources_but").addEventListener("click", search_cb); + by_id("search_resources_but").classList.remove("hide"); +} + +async function main() { + const settings_opener = (e) => browser.runtime.openOptionsPage(); + by_id("settings_but").addEventListener("click", settings_opener); + + try { + var tab = await get_current_tab(); + var tab_id = tab.id; + + if (is_privileged_url(tab.url)) + var page_info = {privileged: true, url: tab.url}; + else + var page_info = await get_page_info(tab_id); + } catch(e) { + console.error(e); + } + + if (page_info) { + show_page_info(page_info); + if (!page_info.privileged) + prepare_repo_query_view(tab_id, page_info); + } else { + by_id("loading_info").innerText = + "Page info not avaialable. Try reloading the page."; + } +} + +main(); |