Project

General

Profile

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

haketilo / html / popup.js @ ea9df6c7

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
	let payload_text = "None";
83

    
84
	if (page_info.payload) {
85
	    if ("error" in page_info) {
86
		if (page_info.error.haketilo_error_type === "missing")
87
		    payload_text = `None (error: resource with id '${page_info.error.id}' missing from the database)`;
88
		else if (page_info.error.haketilo_error_type === "circular")
89
		    payload_text = `None (error: circular dependency of resource with id '${page_info.error.id}' on itself)`;
90
		else if (page_info.error.haketilo_error_type === "db")
91
		    payload_text = `None (error: failure reading Haketilo internal database)`;
92
		else if (page_info.error.haketilo_error_type === "other")
93
		    payload_text = `None (error: unknown failure occured)`;
94
	    } else {
95
		payload_text = page_info.payload.identifier;
96
	    }
97
	}
98

    
99
	by_id("injected_payload").innerText = payload_text;
100

    
101
	const scripts_fate = page_info.allow ? "allowed" : "blocked";
102

    
103
	let mapping_text;
104

    
105
	if (page_info.mapping === "~allow")
106
	    mapping_text = `None (scripts ${scripts_fate} by a rule)`;
107
	else if ("error" in page_info
108
		 && page_info.error.haketilo_error_type ==="deciding_policy")
109
	    mapping_text = `None (error occured when determining policy)`;
110
	else if (page_info.mapping)
111
	    mapping_text = page_info.mapping;
112
	else
113
	    mapping_text = `None (scripts ${scripts_fate} by default policy)`;
114

    
115
	by_id("mapping_used").innerText = mapping_text;
116
    }
117
}
118

    
119
function repo_query_showing(show) {
120
    for (const [id, i] of [["repo_query", 0], ["page_info", 1]])
121
	by_id(`${id}_container`).classList[["add", "remove"][show ^ i]]("hide");
122
}
123

    
124
function prepare_repo_query_view(tab_id, page_info) {
125
    const repo_query_view = new RepoQueryView(tab_id,
126
					      () => repo_query_showing(true),
127
					      () => repo_query_showing(false));
128
    by_id("repo_query_container").prepend(repo_query_view.main_div);
129

    
130
    let search_cb = () => repo_query_view.show(page_info.url);
131
    search_cb = repo_query_view.when_hidden(search_cb);
132
    by_id("search_resources_but").addEventListener("click", search_cb);
133
    by_id("search_resources_but").classList.remove("hide");
134
}
135

    
136
async function main() {
137
    const settings_opener = (e) => browser.runtime.openOptionsPage();
138
    by_id("settings_but").addEventListener("click", settings_opener);
139

    
140
    try {
141
	var tab = await get_current_tab();
142
	var tab_id = tab.id;
143

    
144
	if (is_privileged_url(tab.url))
145
	    var page_info = {privileged: true, url: tab.url};
146
	else
147
	    var page_info = await get_page_info(tab_id);
148
    } catch(e) {
149
	console.error(e);
150
    }
151

    
152
    if (page_info) {
153
	show_page_info(page_info);
154
	if (!page_info.privileged)
155
	    prepare_repo_query_view(tab_id, page_info);
156
    } else {
157
	by_id("loading_info").innerText =
158
	    "Page info not avaialable. Try reloading the page.";
159
    }
160
}
161

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