1 |
046b8a7b
|
Wojtek Kosior
|
/**
|
2 |
|
|
* This file is part of Haketilo.
|
3 |
|
|
*
|
4 |
|
|
* Function: Caching responses from remote repositories and serving them to
|
5 |
|
|
* popup pages.
|
6 |
|
|
*
|
7 |
|
|
* Copyright (C) 2022 Wojtek Kosior
|
8 |
|
|
*
|
9 |
|
|
* This program is free software: you can redistribute it and/or modify
|
10 |
|
|
* it under the terms of the GNU General Public License as published by
|
11 |
|
|
* the Free Software Foundation, either version 3 of the License, or
|
12 |
|
|
* (at your option) any later version.
|
13 |
|
|
*
|
14 |
|
|
* This program is distributed in the hope that it will be useful,
|
15 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17 |
|
|
* GNU General Public License for more details.
|
18 |
|
|
*
|
19 |
|
|
* As additional permission under GNU GPL version 3 section 7, you
|
20 |
|
|
* may distribute forms of that code without the copy of the GNU
|
21 |
|
|
* GPL normally required by section 4, provided you include this
|
22 |
|
|
* license notice and, in case of non-source distribution, a URL
|
23 |
|
|
* through which recipients can access the Corresponding Source.
|
24 |
|
|
* If you modify file(s) with this exception, you may extend this
|
25 |
|
|
* exception to your version of the file(s), but you are not
|
26 |
|
|
* obligated to do so. If you do not wish to do so, delete this
|
27 |
|
|
* exception statement from your version.
|
28 |
|
|
*
|
29 |
|
|
* As a special exception to the GPL, any HTML file which merely
|
30 |
|
|
* makes function calls to this code, and for that purpose
|
31 |
|
|
* includes it by reference shall be deemed a separate work for
|
32 |
|
|
* copyright law purposes. If you modify this code, you may extend
|
33 |
|
|
* this exception to your version of the code, but you are not
|
34 |
|
|
* obligated to do so. If you do not wish to do so, delete this
|
35 |
|
|
* exception statement from your version.
|
36 |
|
|
*
|
37 |
|
|
* You should have received a copy of the GNU General Public License
|
38 |
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
39 |
|
|
*
|
40 |
|
|
* I, Wojtek Kosior, thereby promise not to sue for violation of this file's
|
41 |
|
|
* license. Although I request that you do not make use of this code in a
|
42 |
|
|
* proprietary program, I am not going to enforce this in court.
|
43 |
|
|
*/
|
44 |
|
|
|
45 |
|
|
/*
|
46 |
|
|
* Map URLs to objects containing parsed responses, error info or promises
|
47 |
|
|
* resolving to those.
|
48 |
|
|
*/
|
49 |
|
|
const cache = new Map();
|
50 |
|
|
|
51 |
|
|
async function perform_download(url) {
|
52 |
|
|
if (cache.has(url))
|
53 |
|
|
return cache.get(url);
|
54 |
|
|
|
55 |
|
|
let resolve_cb;
|
56 |
|
|
cache.set(url, new Promise(cb => resolve_cb = cb));
|
57 |
|
|
|
58 |
|
|
try {
|
59 |
|
|
const opts = {url, to_get: ["ok", "status"], to_call: ["json"]};
|
60 |
|
|
var result = await browser.runtime.sendMessage(["CORS_bypass", opts]);
|
61 |
|
|
if (result === undefined)
|
62 |
|
|
result = {error: "Couldn't communicate with background script."};
|
63 |
|
|
} catch(e) {
|
64 |
|
|
var result = {error: e + ""};
|
65 |
|
|
}
|
66 |
|
|
|
67 |
|
|
cache.set(url, result);
|
68 |
|
|
resolve_cb(result);
|
69 |
|
|
|
70 |
|
|
return result;
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
function on_repo_query_request([type, url], sender, respond_cb) {
|
74 |
|
|
if (type !== "repo_query")
|
75 |
|
|
return;
|
76 |
|
|
|
77 |
|
|
perform_download(url).then(respond_cb);
|
78 |
|
|
|
79 |
|
|
return true;
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
function start() {
|
83 |
|
|
browser.runtime.onMessage.addListener(on_repo_query_request);
|
84 |
|
|
}
|
85 |
4c6a2323
|
Wojtek Kosior
|
#EXPORT start
|