From 42fe44050661ed59198fb166672bfdaa119d4333 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Wed, 26 Jan 2022 11:38:21 +0100 Subject: add new extension's popup page --- html/popup.html | 111 ++++++++++++++++++++++ html/popup.js | 140 ++++++++++++++++++++++++++++ html/repo_query.html | 15 +-- html/repo_query.js | 15 +-- test/extension_crafting.py | 4 + test/unit/conftest.py | 1 + test/unit/test_popup.py | 215 +++++++++++++++++++++++++++++++++++++++++++ test/unit/test_repo_query.py | 10 +- 8 files changed, 491 insertions(+), 20 deletions(-) create mode 100644 html/popup.html create mode 100644 html/popup.js create mode 100644 test/unit/test_popup.py diff --git a/html/popup.html b/html/popup.html new file mode 100644 index 0000000..ad6c258 --- /dev/null +++ b/html/popup.html @@ -0,0 +1,111 @@ + + + + + + Haketilo popup +#LOADCSS html/reset.css +#LOADCSS html/base.css +#LOADCSS html/grid.css + + + + +#INCLUDE html/repo_query.html +
+
+ Loading page info... +
+
+ + + +
+ + + + + + +
+
+
+ +
+
+ +
+
+
+ +
+#LOADJS html/popup.js + + 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 . + * + * 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(); diff --git a/html/repo_query.html b/html/repo_query.html index 73b0f00..b9c9269 100644 --- a/html/repo_query.html +++ b/html/repo_query.html @@ -38,14 +38,16 @@ #LOADCSS html/reset.css #LOADCSS html/base.css -#LOADCSS html/grid.css