Revision c483ae19
Added by koszko about 2 years ago
background/page_actions_server.js | ||
---|---|---|
14 | 14 |
* IMPORT listen_for_connection |
15 | 15 |
* IMPORT sha256 |
16 | 16 |
* IMPORT get_query_best |
17 |
* IMPORT make_ajax_request |
|
17 | 18 |
* IMPORTS_END |
18 | 19 |
*/ |
19 | 20 |
|
... | ... | |
23 | 24 |
|
24 | 25 |
function send_actions(url, port) |
25 | 26 |
{ |
26 |
let [pattern, settings] = query_best(url); |
|
27 |
const [pattern, settings] = query_best(url); |
|
28 |
const repos = storage.get_all(TYPE_PREFIX.REPO); |
|
27 | 29 |
|
28 |
port.postMessage(["settings", [pattern, settings]]); |
|
30 |
port.postMessage(["settings", [pattern, settings, repos]]);
|
|
29 | 31 |
|
30 | 32 |
if (settings === undefined) |
31 | 33 |
return; |
... | ... | |
85 | 87 |
} |
86 | 88 |
} |
87 | 89 |
|
88 |
function ajax_callback() |
|
89 |
{ |
|
90 |
if (this.readyState == 4) |
|
91 |
this.resolve_callback(this); |
|
92 |
} |
|
93 |
|
|
94 |
function initiate_ajax_request(resolve, method, url) |
|
95 |
{ |
|
96 |
var xhttp = new XMLHttpRequest(); |
|
97 |
xhttp.resolve_callback = resolve; |
|
98 |
xhttp.onreadystatechange = ajax_callback; |
|
99 |
xhttp.open(method, url, true); |
|
100 |
xhttp.send(); |
|
101 |
} |
|
102 |
|
|
103 |
function make_ajax_request(method, url) |
|
104 |
{ |
|
105 |
return new Promise((resolve, reject) => |
|
106 |
initiate_ajax_request(resolve, method, url)); |
|
107 |
} |
|
108 |
|
|
109 | 90 |
async function fetch_remote_script(script_data) |
110 | 91 |
{ |
111 | 92 |
try { |
common/ajax.js | ||
---|---|---|
1 |
/** |
|
2 |
* part of Hachette |
|
3 |
* Wrapping XMLHttpRequest into a Promise. |
|
4 |
* |
|
5 |
* Copyright (C) 2021 Wojtek Kosior |
|
6 |
* Redistribution terms are gathered in the `copyright' file. |
|
7 |
*/ |
|
8 |
|
|
9 |
function ajax_callback() |
|
10 |
{ |
|
11 |
if (this.readyState == 4) |
|
12 |
this.resolve_callback(this); |
|
13 |
} |
|
14 |
|
|
15 |
function initiate_ajax_request(resolve, reject, method, url) |
|
16 |
{ |
|
17 |
const xhttp = new XMLHttpRequest(); |
|
18 |
xhttp.resolve_callback = resolve; |
|
19 |
xhttp.onreadystatechange = ajax_callback; |
|
20 |
xhttp.open(method, url, true); |
|
21 |
try { |
|
22 |
xhttp.send(); |
|
23 |
} catch(e) { |
|
24 |
console.log(e); |
|
25 |
setTimeout(reject, 0); |
|
26 |
} |
|
27 |
} |
|
28 |
|
|
29 |
function make_ajax_request(method, url) |
|
30 |
{ |
|
31 |
return new Promise((resolve, reject) => |
|
32 |
initiate_ajax_request(resolve, reject, method, url)); |
|
33 |
} |
|
34 |
|
|
35 |
/* |
|
36 |
* EXPORTS_START |
|
37 |
* EXPORT make_ajax_request |
|
38 |
* EXPORTS_END |
|
39 |
*/ |
content/activity_info_server.js | ||
---|---|---|
11 | 11 |
* IMPORTS_START |
12 | 12 |
* IMPORT listen_for_connection |
13 | 13 |
* IMPORT CONNECTION_TYPE |
14 |
* IMPORT set_repo_query_repos |
|
15 |
* IMPORT set_repo_query_callback |
|
14 | 16 |
* IMPORTS_END |
15 | 17 |
*/ |
16 | 18 |
|
... | ... | |
33 | 35 |
|
34 | 36 |
function report_settings(settings) |
35 | 37 |
{ |
38 |
const [pattern, settings_data, repos] = settings; |
|
39 |
set_repo_query_repos(repos); |
|
40 |
|
|
36 | 41 |
report_activity("settings", settings); |
37 | 42 |
} |
38 | 43 |
|
44 |
function report_repo_query_result(result) |
|
45 |
{ |
|
46 |
report_activity("repo_query_result", result); |
|
47 |
} |
|
48 |
|
|
49 |
function trigger_repo_query() |
|
50 |
{ |
|
51 |
set_repo_query_callback(report_repo_query_result); |
|
52 |
} |
|
53 |
|
|
39 | 54 |
function new_connection(port) |
40 | 55 |
{ |
41 | 56 |
console.log("new activity info connection!"); |
... | ... | |
44 | 59 |
|
45 | 60 |
for (const activity of activities) |
46 | 61 |
port.postMessage(activity); |
62 |
|
|
63 |
/* |
|
64 |
* So far the only thing we expect to receive is repo query order. Once more |
|
65 |
* possibilities arrive, we will need to complicate this listener. |
|
66 |
*/ |
|
67 |
port.onMessage.addListener(trigger_repo_query); |
|
68 |
|
|
69 |
port.onDisconnect.addListener(() => ports.delete(port)); |
|
47 | 70 |
} |
48 | 71 |
|
49 | 72 |
function start_activity_info_server() |
content/main.js | ||
---|---|---|
20 | 20 |
* IMPORT is_chrome |
21 | 21 |
* IMPORT is_mozilla |
22 | 22 |
* IMPORT start_activity_info_server |
23 |
* IMPORT set_repo_query_url |
|
23 | 24 |
* IMPORTS_END |
24 | 25 |
*/ |
25 | 26 |
|
... | ... | |
129 | 130 |
} |
130 | 131 |
|
131 | 132 |
start_activity_info_server(); |
133 |
|
|
134 |
set_repo_query_url(document.URL); |
|
132 | 135 |
} |
content/repo_query.js | ||
---|---|---|
1 |
/** |
|
2 |
* part of Hachette |
|
3 |
* Getting available content for site from remote repositories. |
|
4 |
* |
|
5 |
* Copyright (C) 2021 Wojtek Kosior |
|
6 |
* Redistribution terms are gathered in the `copyright' file. |
|
7 |
*/ |
|
8 |
|
|
9 |
/* |
|
10 |
* IMPORTS_START |
|
11 |
* IMPORT make_ajax_request |
|
12 |
* IMPORTS_END |
|
13 |
*/ |
|
14 |
|
|
15 |
var query_started = false; |
|
16 |
|
|
17 |
var url = undefined; |
|
18 |
var repos = undefined; |
|
19 |
var callback = undefined; |
|
20 |
|
|
21 |
async function query(repo) |
|
22 |
{ |
|
23 |
const [repo_url, data] = repo; |
|
24 |
|
|
25 |
let response = "Query failed"; |
|
26 |
const query_url = `${repo_url}/query?n=${encodeURIComponent(url)}`; |
|
27 |
|
|
28 |
try { |
|
29 |
let xhttp = await make_ajax_request("GET", query_url); |
|
30 |
if (xhttp.status === 200) |
|
31 |
response = xhttp.responseText; |
|
32 |
console.log(xhttp); |
|
33 |
} catch (e) { |
|
34 |
console.log(e); |
|
35 |
} |
|
36 |
|
|
37 |
callback([repo_url, response]); |
|
38 |
} |
|
39 |
|
|
40 |
function start_query() |
|
41 |
{ |
|
42 |
if (query_started || !url || !repos || !callback) |
|
43 |
return; |
|
44 |
|
|
45 |
query_started = true; |
|
46 |
|
|
47 |
console.log(`about to query ${url} from ${repos}`); |
|
48 |
|
|
49 |
for (const repo of repos) |
|
50 |
query(repo); |
|
51 |
} |
|
52 |
|
|
53 |
function set_repo_query_url(_url) |
|
54 |
{ |
|
55 |
url = _url; |
|
56 |
|
|
57 |
start_query(); |
|
58 |
} |
|
59 |
|
|
60 |
function set_repo_query_repos(_repos) |
|
61 |
{ |
|
62 |
repos = _repos; |
|
63 |
|
|
64 |
start_query(); |
|
65 |
} |
|
66 |
|
|
67 |
function set_repo_query_callback(_callback) |
|
68 |
{ |
|
69 |
callback = _callback; |
|
70 |
|
|
71 |
start_query(); |
|
72 |
} |
|
73 |
|
|
74 |
/* |
|
75 |
* EXPORTS_START |
|
76 |
* EXPORT set_repo_query_url |
|
77 |
* EXPORT set_repo_query_repos |
|
78 |
* EXPORT set_repo_query_callback |
|
79 |
* EXPORTS_END |
|
80 |
*/ |
html/display-panel.html | ||
---|---|---|
21 | 21 |
display: none; |
22 | 22 |
} |
23 | 23 |
|
24 |
.show_hide_next2:not(:checked)+* { |
|
25 |
display: none; |
|
26 |
} |
|
27 |
|
|
28 |
.show_hide_next2:checked+*+* { |
|
29 |
display: none; |
|
30 |
} |
|
31 |
|
|
24 | 32 |
.hide { |
25 | 33 |
display: none; |
26 | 34 |
} |
... | ... | |
34 | 42 |
#container_for_injected>#none_injected:not(:last-child) { |
35 | 43 |
display: none; |
36 | 44 |
} |
37 |
|
|
38 |
input#connected_chbx:checked+div+h3 { |
|
39 |
display: none; |
|
40 |
} |
|
41 | 45 |
</style> |
42 | 46 |
</head> |
43 | 47 |
<body> |
... | ... | |
65 | 69 |
</label> |
66 | 70 |
<ul id="possible_patterns"></ul> |
67 | 71 |
|
68 |
<input id="connected_chbx" type="checkbox" class="show_next"></input>
|
|
72 |
<input id="connected_chbx" type="checkbox" class="show_hide_next2"></input>
|
|
69 | 73 |
<div> |
70 | 74 |
<h3> |
71 | 75 |
Matched pattern: <span id="pattern">...</span> |
72 | 76 |
<button id="view_pattern" class="hide"> |
73 |
View in settings |
|
77 |
View in settings |
|
78 |
</button> |
|
79 |
<input id="query_started_chbx" type="checkbox" class="show_hide_next2"></input> |
|
80 |
<div id="container_for_repo_responses"> |
|
81 |
<h3>Queried from repositories</h3> |
|
82 |
</div> |
|
83 |
<button id="query_pattern"> |
|
84 |
Search for matching patterns |
|
74 | 85 |
</button> |
75 | 86 |
</h3> |
76 | 87 |
<h3> |
html/display-panel.js | ||
---|---|---|
144 | 144 |
} |
145 | 145 |
|
146 | 146 |
const connected_chbx = by_id("connected_chbx"); |
147 |
const query_pattern_but = by_id("query_pattern"); |
|
147 | 148 |
|
148 | 149 |
function try_to_connect(tab_id) |
149 | 150 |
{ |
... | ... | |
151 | 152 |
const connect_info = {name: CONNECTION_TYPE.ACTIVITY_INFO, frameId: 0}; |
152 | 153 |
const port = browser.tabs.connect(tab_id, connect_info); |
153 | 154 |
|
154 |
port.onDisconnect.addListener(port => handle_disconnect(tab_id)); |
|
155 |
const button_cb = (e) => start_querying_repos(port); |
|
156 |
|
|
157 |
port.onDisconnect.addListener(port => handle_disconnect(tab_id, button_cb)); |
|
155 | 158 |
port.onMessage.addListener(handle_activity_report); |
156 | 159 |
|
160 |
query_pattern_but.addEventListener("click", button_cb); |
|
161 |
|
|
157 | 162 |
if (is_mozilla) |
158 | 163 |
setTimeout(() => monitor_connecting(port, tab_id), 1000); |
159 | 164 |
} |
160 | 165 |
|
166 |
const query_started_chbx = by_id("query_started_chbx"); |
|
167 |
|
|
168 |
function start_querying_repos(port) |
|
169 |
{ |
|
170 |
port.postMessage("dummy (trigger repo querying)"); |
|
171 |
query_started_chbx.checked = true; |
|
172 |
} |
|
173 |
|
|
161 | 174 |
const loading_chbx = by_id("loading_chbx"); |
162 | 175 |
|
163 |
function handle_disconnect(tab_id) |
|
176 |
function handle_disconnect(tab_id, button_cb)
|
|
164 | 177 |
{ |
178 |
query_pattern_but.removeEventListener("click", button_cb); |
|
179 |
|
|
165 | 180 |
if (is_chrome && !browser.runtime.lastError) |
166 | 181 |
return; |
167 | 182 |
|
168 |
/* return if there was no connection initialization failure */
|
|
183 |
/* return if error was not during connection initialization */
|
|
169 | 184 |
if (connected_chbx.checked) |
170 | 185 |
return; |
171 | 186 |
|
... | ... | |
189 | 204 |
const payload_span = by_id("payload"); |
190 | 205 |
const view_payload_but = by_id("view_payload"); |
191 | 206 |
const container_for_injected = by_id("container_for_injected"); |
207 |
const container_for_repo_responses = by_id("container_for_repo_responses"); |
|
192 | 208 |
|
193 | 209 |
function handle_activity_report(message) |
194 | 210 |
{ |
... | ... | |
197 | 213 |
const [type, data] = message; |
198 | 214 |
|
199 | 215 |
if (type === "settings") { |
200 |
let [pattern, settings] = data; |
|
216 |
let [pattern, settings, repos] = data;
|
|
201 | 217 |
|
202 | 218 |
settings = settings || {}; |
203 | 219 |
blocked_span.textContent = settings.allow ? "no" : "yes"; |
... | ... | |
231 | 247 |
container_for_injected.appendChild(h4); |
232 | 248 |
container_for_injected.appendChild(pre); |
233 | 249 |
} |
250 |
if (type === "repo_query_result") { |
|
251 |
const [repo_url, response_text] = data; |
|
252 |
|
|
253 |
const h4 = document.createElement("h4"); |
|
254 |
const pre = document.createElement("pre"); |
|
255 |
h4.textContent = repo_url; |
|
256 |
pre.textContent = response_text; |
|
257 |
|
|
258 |
container_for_repo_responses.appendChild(h4); |
|
259 |
container_for_repo_responses.appendChild(pre); |
|
260 |
} |
|
234 | 261 |
} |
235 | 262 |
|
236 | 263 |
by_id("settings_but") |
Also available in: Unified diff
add ability to query page content from repo and display it in the popup