Project

General

Profile

« Previous | Next » 

Revision 5957fbee

Added by koszko about 2 years ago

make settings_query.js use storage object passed as an argument

View differences:

background/page_actions_server.js
13 13
 * IMPORT browser
14 14
 * IMPORT listen_for_connection
15 15
 * IMPORT sha256
16
 * IMPORT get_query_best
16
 * IMPORT query_best
17 17
 * IMPORT make_ajax_request
18 18
 * IMPORTS_END
19 19
 */
20 20

  
21 21
var storage;
22
var query_best;
23 22
var handler;
24 23

  
25 24
function send_actions(url, port)
26 25
{
27
    const [pattern, settings] = query_best(url);
26
    const [pattern, settings] = query_best(storage, url);
28 27
    const repos = storage.get_all(TYPE_PREFIX.REPO);
29 28

  
30 29
    port.postMessage(["settings", [pattern, settings, repos]]);
......
126 125
async function start_page_actions_server()
127 126
{
128 127
    storage = await get_storage();
129
    query_best = await get_query_best();
130 128

  
131 129
    listen_for_connection(CONNECTION_TYPE.PAGE_ACTIONS, new_connection);
132 130
}
background/page_info_server.js
10 10
 * IMPORTS_START
11 11
 * IMPORT listen_for_connection
12 12
 * IMPORT get_storage
13
 * IMPORT get_query_all
13
 * IMPORT query_all
14 14
 * IMPORT TYPE_PREFIX
15 15
 * IMPORT CONNECTION_TYPE
16 16
 * IMPORT url_matches
......
18 18
 */
19 19

  
20 20
var storage;
21
var query_all;
22 21

  
23 22
function handle_change(connection_data, change)
24 23
{
......
37 36
    }
38 37

  
39 38
    connection_data.subscribed.add(url);
40
    connection_data.port.postMessage(["new_url", query_all(url)]);
39
    connection_data.port.postMessage(["new_url", query_all(storage, url)]);
41 40
}
42 41

  
43 42
function new_connection(port)
......
62 61
async function start_page_info_server()
63 62
{
64 63
    storage = await get_storage();
65
    query_all = await get_query_all();
66 64

  
67 65
    listen_for_connection(CONNECTION_TYPE.PAGE_INFO, new_connection);
68 66
}
background/policy_injector.js
19 19
 * IMPORT url_item
20 20
 * IMPORT url_extract_target
21 21
 * IMPORT sign_policy
22
 * IMPORT get_query_best
22
 * IMPORT query_best
23 23
 * IMPORT csp_rule
24 24
 * IMPORTS_END
25 25
 */
26 26

  
27 27
var storage;
28
var query_best;
29 28

  
30 29
const csp_header_names = {
31 30
    "content-security-policy" : true,
......
53 52
    if (targets.policy)
54 53
	targets.target = "";
55 54

  
56
    let [pattern, settings] = query_best(targets.base_url);
55
    let [pattern, settings] = query_best(storage, targets.base_url);
57 56
    /* Defaults */
58 57
    if (!pattern)
59 58
	settings = {};
......
106 105
async function start_policy_injector()
107 106
{
108 107
    storage = await get_storage();
109
    query_best = await get_query_best();
110 108

  
111 109
    let extra_opts = ["blocking", "responseHeaders"];
112 110
    if (is_chrome)
background/settings_query.js
1
/**
2
 * Hachette querying page settings with regard to wildcard records
3
 *
4
 * Copyright (C) 2021 Wojtek Kosior
5
 * Redistribution terms are gathered in the `copyright' file.
6
 */
7

  
8
/*
9
 * IMPORTS_START
10
 * IMPORT make_once
11
 * IMPORT get_storage
12
 * IMPORT TYPE_PREFIX
13
 * IMPORT for_each_possible_pattern
14
 * IMPORTS_END
15
 */
16

  
17
var storage;
18

  
19
async function init(fun)
20
{
21
    storage = await get_storage();
22

  
23
    return fun;
24
}
25

  
26
function check_pattern(pattern, multiple, matched)
27
{
28
    const settings = storage.get(TYPE_PREFIX.PAGE, pattern);
29

  
30
    if (settings === undefined)
31
	return;
32

  
33
    matched.push([pattern, settings]);
34

  
35
    if (!multiple)
36
	return false;
37
}
38

  
39
function query(url, multiple)
40
{
41
    const matched = [];
42
    for_each_possible_pattern(url, p => check_pattern(p, multiple, matched));
43

  
44
    return multiple ? matched : (matched[0] || [undefined, undefined]);
45
}
46

  
47
function query_best(url)
48
{
49
    return query(url, false);
50
}
51

  
52
function query_all(url)
53
{
54
    return query(url, true);
55
}
56

  
57
const get_query_best = make_once(() => init(query_best));
58
const get_query_all = make_once(() => init(query_all));
59

  
60
/*
61
 * EXPORTS_START
62
 * EXPORT get_query_best
63
 * EXPORT get_query_all
64
 * EXPORTS_END
65
 */
common/settings_query.js
1
/**
2
 * Hachette querying page settings with regard to wildcard records
3
 *
4
 * Copyright (C) 2021 Wojtek Kosior
5
 * Redistribution terms are gathered in the `copyright' file.
6
 */
7

  
8
/*
9
 * IMPORTS_START
10
 * IMPORT TYPE_PREFIX
11
 * IMPORT for_each_possible_pattern
12
 * IMPORTS_END
13
 */
14

  
15
function check_pattern(storage, pattern, multiple, matched)
16
{
17
    const settings = storage.get(TYPE_PREFIX.PAGE, pattern);
18

  
19
    if (settings === undefined)
20
	return;
21

  
22
    matched.push([pattern, settings]);
23

  
24
    if (!multiple)
25
	return false;
26
}
27

  
28
function query(storage, url, multiple)
29
{
30
    const matched = [];
31
    const cb = p => check_pattern(storage, p, multiple, matched);
32
    for_each_possible_pattern(url, cb);
33

  
34
    return multiple ? matched : (matched[0] || [undefined, undefined]);
35
}
36

  
37
function query_best(storage, url)
38
{
39
    return query(storage, url, false);
40
}
41

  
42
function query_all(storage, url)
43
{
44
    return query(storage, url, true);
45
}
46

  
47
/*
48
 * EXPORTS_START
49
 * EXPORT query_best
50
 * EXPORT query_all
51
 * EXPORTS_END
52
 */

Also available in: Unified diff