Project

General

Profile

« Previous | Next » 

Revision b7e2870f

Added by koszko about 2 years ago

show some settings of the current page in the popup

View differences:

background/settings_query.js
10 10
 * IMPORT make_once
11 11
 * IMPORT get_storage
12 12
 * IMPORT TYPE_PREFIX
13
 * IMPORT for_each_possible_pattern
13 14
 * IMPORTS_END
14 15
 */
15 16

  
16 17
var storage;
17 18

  
18
var exports = {};
19

  
20 19
async function init(fun)
21 20
{
22 21
    storage = await get_storage();
......
24 23
    return fun;
25 24
}
26 25

  
27
// TODO: also support urls with specified ports
28
function query(url, multiple)
26
function check_pattern(pattern, multiple, matched)
29 27
{
30
    let proto_re = "[a-zA-Z]*:\/\/";
31
    let domain_re = "[^/?#]+";
32
    let segments_re = "/[^?#]*";
33
    let query_re = "\\?[^#]*";
34

  
35
    let url_regex = new RegExp(`\
36
^\
37
(${proto_re})\
38
(${domain_re})\
39
(${segments_re})?\
40
(${query_re})?\
41
#?.*\$\
42
`);
43

  
44
    let regex_match = url_regex.exec(url);
45
    if (regex_match === null) {
46
	console.log("bad url format", url);
47
	return multiple ? [] : [undefined, undefined];
48
    }
49

  
50
    let [_, proto, domain, segments, query] = regex_match;
51

  
52
    domain = domain.split(".");
53
    let segments_trailing_dash =
54
	segments && segments[segments.length - 1] === "/";
55
    segments = (segments || "").split("/").filter(s => s !== "");
56
    segments.unshift("");
57

  
58
    let matched = [];
28
    const settings = storage.get(TYPE_PREFIX.PAGE, pattern);
59 29

  
60
    for (let d_slice = 0; d_slice < domain.length; d_slice++) {
61
	let domain_part = domain.slice(d_slice).join(".");
62
	let domain_wildcards = [];
63
	if (d_slice === 0)
64
	    domain_wildcards.push("");
65
	if (d_slice === 1)
66
	    domain_wildcards.push("*.");
67
	if (d_slice > 0)
68
	    domain_wildcards.push("**.");
69
	domain_wildcards.push("***.");
30
    if (settings === undefined)
31
	return;
70 32

  
71
	for (let domain_wildcard of domain_wildcards) {
72
	    let domain_pattern = domain_wildcard + domain_part;
33
    matched.push([pattern, settings]);
73 34

  
74
	    for (let s_slice = segments.length; s_slice > 0; s_slice--) {
75
		let segments_part = segments.slice(0, s_slice).join("/");
76
		let segments_wildcards = [];
77
		if (s_slice === segments.length) {
78
		    if (segments_trailing_dash)
79
			segments_wildcards.push("/");
80
		    segments_wildcards.push("");
81
		}
82
		if (s_slice === segments.length - 1) {
83
		    if (segments[s_slice] !== "*")
84
			segments_wildcards.push("/*");
85
		}
86
		if (s_slice < segments.length &&
87
		    (segments[s_slice] !== "**" ||
88
		     s_slice < segments.length - 1))
89
		    segments_wildcards.push("/**");
90
		if (segments[s_slice] !== "***" ||
91
		    s_slice < segments.length)
92
		    segments_wildcards.push("/***");
93

  
94
		for (let segments_wildcard of segments_wildcards) {
95
		    let segments_pattern =
96
			segments_part + segments_wildcard;
97

  
98
		    let pattern = proto + domain_pattern + segments_pattern;
99
		    console.log("trying", pattern);
100
		    let settings = storage.get(TYPE_PREFIX.PAGE, pattern);
101

  
102
		    if (settings === undefined)
103
			continue;
104

  
105
		    if (!multiple)
106
			return [pattern, settings];
35
    if (!multiple)
36
	return false;
37
}
107 38

  
108
		    matched.push([pattern, settings]);
109
		}
110
	    }
111
	}
112
    }
39
function query(url, multiple)
40
{
41
    const matched = [];
42
    for_each_possible_pattern(url, p => check_pattern(p, multiple, matched));
113 43

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

  
117 47
function query_best(url)

Also available in: Unified diff