Project

General

Profile

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

haketilo / html / popup.js @ 1f9ccef9

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("Haketilo:", 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
		let long_msg = true;
87

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

    
99
		if (long_msg)
100
		    by_id("injected_payload").classList.add("long_msg");
101
	    } else {
102
		payload_text = page_info.payload.identifier;
103
	    }
104
	}
105

    
106
	by_id("injected_payload").innerText = payload_text;
107

    
108
	const scripts_fate = page_info.allow ? "allowed" : "blocked";
109

    
110
	let mapping_text, long_msg = true;
111

    
112
	if (page_info.mapping === "~allow") {
113
	    mapping_text = `None (scripts ${scripts_fate} by a rule)`;
114
	} else if ("error" in page_info &&
115
		   page_info.error.haketilo_error_type === "deciding_policy") {
116
	    mapping_text = `None (error occured when determining policy)`;
117
	} else if (page_info.mapping) {
118
	    mapping_text = page_info.mapping;
119
	    long_msg = false;
120
	} else {
121
	    mapping_text = `None (scripts ${scripts_fate} by default policy)`;
122
	}
123

    
124
	by_id("mapping_used").innerText = mapping_text;
125
	if (long_msg)
126
	    by_id("mapping_used").classList.add("long_msg");
127
    }
128
}
129

    
130
function repo_query_showing(show) {
131
    for (const [id, i] of [["repo_query", 0], ["page_info", 1]])
132
	by_id(`${id}_container`).classList[["add", "remove"][show ^ i]]("hide");
133
}
134

    
135
function prepare_repo_query_view(tab, page_info) {
136
    const repo_query_view = new RepoQueryView(tab,
137
					      () => repo_query_showing(true),
138
					      () => repo_query_showing(false));
139
    by_id("repo_query_container").prepend(repo_query_view.main_div);
140

    
141
    let search_cb = () => repo_query_view.show(page_info.url);
142
    search_cb = repo_query_view.when_hidden(search_cb);
143
    by_id("search_resources_but").addEventListener("click", search_cb);
144
    by_id("search_resources_but").classList.remove("hide");
145
}
146

    
147
async function main() {
148
    const settings_opener = (e) => browser.runtime.openOptionsPage();
149
    by_id("settings_but").addEventListener("click", settings_opener);
150

    
151
    try {
152
	var tab = await get_current_tab();
153
	var tab_id = tab.id;
154

    
155
	if (is_privileged_url(tab.url))
156
	    var page_info = {privileged: true, url: tab.url};
157
	else
158
	    var page_info = await get_page_info(tab_id);
159
    } catch(e) {
160
	console.error("Haketilo:", e);
161
    }
162

    
163
    if (page_info) {
164
	show_page_info(page_info);
165
	if (!page_info.privileged)
166
	    prepare_repo_query_view(tab, page_info);
167
    } else {
168
	by_id("loading_info").innerText =
169
	    "Page info not avaialable. Try reloading the page.";
170
    }
171
}
172

    
173
main();
(19-19/28)