aboutsummaryrefslogtreecommitdiff
path: root/html/display-panel.js
blob: 9ae35fd9962a12d4b257217990685114adbcc559 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
 * Myext display panel logic
 *
 * Copyright (C) 2021 Wojtek Kosior
 * Redistribution terms are gathered in the `copyright' file.
 */

/*
 * IMPORTS_START
 * IMPORT browser
 * IMPORT is_chrome
 * IMPORT is_mozilla
 * IMPORT CONNECTION_TYPE
 * IMPORT url_item
 * IMPORT is_privileged_url
 * IMPORT TYPE_PREFIX
 * IMPORT nice_name
 * IMPORT open_in_settings
 * IMPORT for_each_possible_pattern
 * IMPORTS_END
 */

function by_id(id)
{
    return document.getElementById(id);
}

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

async function get_current_tab()
{
    /* Fix for fact that Chrome does not use promises here */
    const promise = is_chrome ?
	  new Promise((resolve, reject) =>
		      browser.tabs.query(tab_query, tab => resolve(tab))) :
	  browser.tabs.query(tab_query);

    try {
	return (await promise)[0];
    } catch(e) {
	console.log(e);
    }
}

const page_url_heading = by_id("page_url_heading");
const show_privileged_notice_chbx = by_id("show_privileged_notice_chbx");
const show_page_state_chbx = by_id("show_page_state_chbx");

async function show_page_activity_info()
{
    const tab = await get_current_tab();

    if (tab === undefined) {
	page_url_heading.textContent = "unknown page";
	return;
    }

    const url = url_item(tab.url);
    page_url_heading.textContent = url;
    if (is_privileged_url(url)) {
	show_privileged_notice_chbx.checked = true;
	return;
    }

    populate_possible_patterns_list(url);
    show_page_state_chbx.checked = true;

    try_to_connect(tab.id);
}

function populate_possible_patterns_list(url)
{
    for_each_possible_pattern(url, add_pattern_to_list);

    const port = browser.runtime.connect({name: CONNECTION_TYPE.PAGE_INFO});
    port.onMessage.addListener(handle_page_info);
    port.postMessage(["subscribe", url]);
}

const possible_patterns_ul = by_id("possible_patterns");
const pattern_li_template = by_id("pattern_li_template");
pattern_li_template.removeAttribute("id");
const known_patterns = new Map();

function add_pattern_to_list(pattern)
{
    const li = pattern_li_template.cloneNode(true);
    li.id = `pattern_li_${known_patterns.size}`;
    known_patterns.set(pattern, li.id);

    const span = li.firstElementChild;
    span.textContent = pattern;

    const button = span.nextElementSibling;
    const settings_opener = () => open_in_settings(TYPE_PREFIX.PAGE, pattern);
    button.addEventListener("click", settings_opener);

    possible_patterns_ul.appendChild(li)

    return li.id;
}

function ensure_pattern_exists(pattern)
{
    let id = known_patterns.get(pattern);
    /*
     * As long as pattern computation works well, we should never get into this
     * conditional block. This is just a safety measure. To be removed as part
     * of a bigger rework when we start taking iframes into account.
     */
    if (id === undefined) {
	console.log(`unknown pattern: ${pattern}`);
	id = add_pattern_to_list(pattern);
    }

    return id;
}

function set_pattern_li_button_text(li_id, text)
{
    by_id(li_id).firstElementChild.nextElementSibling.textContent = text;
}

function handle_page_info(message)
{
    const [type, data] = message;

    if (type === "change") {
	const li_id = ensure_pattern_exists(data.item);
	if (data.old_val === undefined)
	    set_pattern_li_button_text(li_id, "Edit in settings");
	if (data.new_val === undefined)
	    set_pattern_li_button_text(li_id, "Add setting");
    }

    if (type === "new_url") {
	for (const li_id of known_patterns.values())
	    set_pattern_li_button_text(li_id, "Add setting");
	for (const [pattern, settings] of data) {
	    set_pattern_li_button_text(ensure_pattern_exists(pattern),
				       "Edit in settings")
	}
    }
}

const connected_chbx = by_id("connected_chbx");

function try_to_connect(tab_id)
{
    /* This won't connect to iframes. We'll add support for them later */
    const connect_info = {name: CONNECTION_TYPE.ACTIVITY_INFO, frameId: 0};
    const port = browser.tabs.connect(tab_id, connect_info);

    port.onDisconnect.addListener(port => handle_disconnect(tab_id));
    port.onMessage.addListener(handle_activity_report);

    if (is_mozilla)
	setTimeout(() => monitor_connecting(port, tab_id), 1000);
}

const loading_chbx = by_id("loading_chbx");

function handle_disconnect(tab_id)
{
    if (is_chrome && !browser.runtime.lastError)
	return;

    /* return if there was no connection initialization failure */
    if (connected_chbx.checked)
	return;

    loading_chbx.checked = !loading_chbx.checked;
    setTimeout(() => try_to_connect(tab_id), 1000);
}

function monitor_connecting(port, tab_id)
{
    if (connected_chbx.checked)
	return;

    port.disconnect();
    loading_chbx.checked = !loading_chbx.checked;
    try_to_connect(tab_id);
}

const pattern_span = by_id("pattern");
const view_pattern_but = by_id("view_pattern");
const blocked_span = by_id("blocked");
const payload_span = by_id("payload");
const view_payload_but = by_id("view_payload");
const container_for_injected = by_id("container_for_injected");

function handle_activity_report(message)
{
    connected_chbx.checked = true;

    const [type, data] = message;

    if (type === "settings") {
	let [pattern, settings] = data;

	settings = settings || {};
	blocked_span.textContent = settings.allow ? "no" : "yes";

	if (pattern) {
	    pattern_span.textContent = pattern;
	    const settings_opener =
		  () => open_in_settings(TYPE_PREFIX.PAGE, pattern);
	    view_pattern_but.classList.remove("hide");
	    view_pattern_but.addEventListener("click", settings_opener);
	} else {
	    pattern_span.textContent = "none";
	}

	const components = settings.components;
	if (components) {
	    payload_span.textContent = nice_name(...components);
	    const settings_opener = () => open_in_settings(...components);
	    view_payload_but.classList.remove("hide");
	    view_payload_but.addEventListener("click", settings_opener);
	} else {
	    payload_span.textContent = "none";
	}
    }
    if (type === "script") {
	const h4 = document.createElement("h4");
	const pre = document.createElement("pre");
	h4.textContent = "script";
	pre.textContent = data;

	container_for_injected.appendChild(h4);
	container_for_injected.appendChild(pre);
    }
}

by_id("settings_but")
    .addEventListener("click", (e) => browser.runtime.openOptionsPage());

show_page_activity_info();