Project

General

Profile

Download (4.82 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / html / popup.js @ 9d825eaa

1
/**
2
 * This file is part of Haketilo.
3
 *
4
 * Function: Show details of how Haketilo handled given page, drive popup.
5
 *
6
 * Copyright (C) 2021,2022 Wojtek Kosior
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * As additional permission under GNU GPL version 3 section 7, you
19
 * may distribute forms of that code without the copy of the GNU
20
 * GPL normally required by section 4, provided you include this
21
 * license notice and, in case of non-source distribution, a URL
22
 * through which recipients can access the Corresponding Source.
23
 * If you modify file(s) with this exception, you may extend this
24
 * exception to your version of the file(s), but you are not
25
 * obligated to do so. If you do not wish to do so, delete this
26
 * exception statement from your version.
27
 *
28
 * As a special exception to the GPL, any HTML file which merely
29
 * makes function calls to this code, and for that purpose
30
 * includes it by reference shall be deemed a separate work for
31
 * copyright law purposes. If you modify this code, you may extend
32
 * this exception to your version of the code, but you are not
33
 * obligated to do so. If you do not wish to do so, delete this
34
 * exception statement from your version.
35
 *
36
 * You should have received a copy of the GNU General Public License
37
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
38
 *
39
 * I, Wojtek Kosior, thereby promise not to sue for violation of this file's
40
 * license. Although I request that you do not make use of this code in a
41
 * proprietary program, I am not going to enforce this in court.
42
 */
43

    
44
#FROM common/browser.js   IMPORT browser
45
#FROM common/misc.js      IMPORT is_privileged_url
46
#FROM html/DOM_helpers.js IMPORT by_id
47
#FROM html/repo_query.js  IMPORT RepoQueryView
48

    
49
const tab_query = {currentWindow: true, active: true};
50

    
51
async function get_current_tab() {
52
#IF CHROMIUM
53
    const callback = (cb) => browser.tabs.query(tab_query, tab => cb(tab));
54
    const promise = new Promise(callback);
55
#ELIF MOZILLA
56
    const promise = browser.tabs.query(tab_query);
57
#ENDIF
58

    
59
    try {
60
	return (await promise)[0];
61
    } catch(e) {
62
	console.log(e);
63
    }
64
}
65

    
66
async function get_page_info(tab_id) {
67
    return browser.tabs.sendMessage(tab_id, ["page_info"]);
68
}
69

    
70
function show_page_info(page_info) {
71
    by_id("loading_info").remove();
72
    by_id("info_form").classList.remove("hide");
73
    by_id("page_url").innerText = page_info.url;
74

    
75
    if (page_info.privileged) {
76
	by_id("privileged_page_info").classList.remove("hide");
77
    } else {
78
	by_id("unprivileged_page_info").classList.remove("hide");
79

    
80
	by_id("scripts_blocked").innerText = page_info.allow ? "no" : "yes";
81

    
82
	by_id("injected_payload").innerText = page_info.payload ?
83
	    page_info.payload.identifier : "None";
84

    
85
	const scripts_fate = page_info.allow ? "allowed" : "blocked";
86

    
87
	if (page_info.mapping === "~allow")
88
	    var mapping = `None (scripts ${scripts_fate} by a rule)`;
89
	else if (page_info.mapping)
90
	    var mapping = page_info.mapping;
91
	else if (page_info.error)
92
	    var mapping = `None (error occured when determining policy)`;
93
	by_id("mapping_used").innerText = mapping;
94
    }
95
}
96

    
97
function repo_query_showing(show) {
98
    for (const [id, i] of [["repo_query", 0], ["page_info", 1]])
99
	by_id(`${id}_container`).classList[["add", "remove"][show ^ i]]("hide");
100
}
101

    
102
function prepare_repo_query_view(tab_id, page_info) {
103
    const repo_query_view = new RepoQueryView(tab_id,
104
					      () => repo_query_showing(true),
105
					      () => repo_query_showing(false));
106
    by_id("repo_query_container").prepend(repo_query_view.main_div);
107

    
108
    let search_cb = () => repo_query_view.show(page_info.url);
109
    search_cb = repo_query_view.when_hidden(search_cb);
110
    by_id("search_resources_but").addEventListener("click", search_cb);
111
    by_id("search_resources_but").classList.remove("hide");
112
}
113

    
114
async function main() {
115
    const settings_opener = (e) => browser.runtime.openOptionsPage();
116
    by_id("settings_but").addEventListener("click", settings_opener);
117

    
118
    try {
119
	var tab = await get_current_tab();
120
	var tab_id = tab.id;
121

    
122
	if (is_privileged_url(tab.url))
123
	    var page_info = {privileged: true, url: tab.url};
124
	else
125
	    var page_info = await get_page_info(tab_id);
126
    } catch(e) {
127
	console.error(e);
128
    }
129

    
130
    if (page_info) {
131
	show_page_info(page_info);
132
	if (!page_info.privileged)
133
	    prepare_repo_query_view(tab_id, page_info);
134
    } else {
135
	by_id("loading_info").innerText =
136
	    "Page info not avaialable. Try reloading the page.";
137
    }
138
}
139

    
140
main();
(25-25/33)