Project

General

Profile

« Previous | Next » 

Revision 8708ddd3

Added by koszko about 2 years ago

move parsing of url with targets to misc.js

View differences:

common/misc.js
41 41
    return match[1];
42 42
}
43 43

  
44
/*
45
 * Assume a url like: https://example.com/green?illuminati=confirmed#tinky#winky
46
 * This function will make it into an object like:
47
 * {
48
 *     "base_url" : "https://example.com/green?illuminati=confirmed",
49
 *     "target" : "#tinky",
50
 *     "target2" : "#winky"
51
 * }
52
 * In case url doesn't have 2 #'s, target2 and target can be set to undefined.
53
 */
54
function url_extract_target(url)
55
{
56
    let url_re = /^([^#]*)((#[^#]*)(#.*)?)?$/;
57
    let match = url_re.exec(url);
58
    return {
59
	base_url : match[1],
60
	target : match[3],
61
	target2 : match[4]
62
    };
63
}
64

  
44 65
/* csp rule that blocks all scripts except for those injected by us */
45 66
function csp_rule(nonce)
46 67
{
......
54 75
 * EXPORTS_START
55 76
 * EXPORT gen_unique
56 77
 * EXPORT url_item
78
 * EXPORT url_extract_target
57 79
 * EXPORT csp_rule
58 80
 * EXPORTS_END
59 81
 */
content/main.js
9 9
 * IMPORTS_START
10 10
 * IMPORT handle_page_actions
11 11
 * IMPORT url_item
12
 * IMPORT url_extract_target
12 13
 * IMPORT gen_unique
13 14
 * IMPORT csp_rule
14 15
 * IMPORT sanitize_attributes
......
39 40
    if (url.startsWith("https://") || url.startsWith("http://"))
40 41
	return false;
41 42

  
42
    let url_re = /^([^#]*)((#[^#]*)(#.*)?)?$/;
43
    let match = url_re.exec(document.URL);
44
    let base_url = match[1];
45
    let first_target = match[3];
46
    let second_target = match[4];
43
    const parsed_url = url_extract_target(document.URL);
47 44

  
48
    if (first_target !== undefined &&
49
	first_target === '#' + unique) {
50
	if (second_target !== undefined)
51
	    window.location.href = base_url + second_target;
45
    if (parsed_url.target !== undefined &&
46
	parsed_url.target === '#' + unique) {
47
	if (parsed_url.target2 !== undefined)
48
	    window.location.href = parsed_url.base_url + parsed_url.target2;
52 49
	else
53
	    history.replaceState(null, "", base_url);
50
	    history.replaceState(null, "", parsed_url.base_url);
54 51

  
55 52
	console.log(["allowing whitelisted", document.URL]);
56 53
	return false;

Also available in: Unified diff