Project

General

Profile

« Previous | Next » 

Revision 749f1c85

Added by koszko over 1 year ago

prepare for exposing APIs to injected scripts

View differences:

content/content.js
42 42
 */
43 43

  
44 44
#IMPORT content/repo_query_cacher.js
45
#IMPORT content/haketilo_apis.js
45 46

  
46 47
#FROM common/browser.js           IMPORT browser
47 48
#FROM common/misc.js              IMPORT is_privileged_url
......
132 133
	    resolve_page_info(Object.assign(page_info, script_response));
133 134
	    return;
134 135
	} else {
135
	    for (const script_contents of script_response.files) {
136
	    haketilo_apis.start();
137

  
138
	    const version = browser.runtime.getManifest().version;
139
	    const scripts = [
140
		`window.haketilo_version = ${JSON.stringify(version)};`,
141
		...script_response.files
142
	    ];
143
	    for (const script_contents of scripts) {
136 144
		const html_ns = "http://www.w3.org/1999/xhtml";
137 145
		const script = document.createElementNS(html_ns, "script");
138 146

  
content/haketilo_apis.js
1
/**
2
 * This file is part of Haketilo.
3
 *
4
 * Function: Expose some special functionalities to injected scripts using
5
 *           CustomEvent's to communicate with them.
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
#FROM common/browser.js IMPORT browser
46

  
47
function start() {
48
}
49
#EXPORT start
test/haketilo_test/unit/test_content.py
88 88
    }
89 89

  
90 90
    repo_query_cacher.start = () => data_set("cacher_started", true);
91
    haketilo_apis.start     = () => data_set("apis_started", true);
91 92

  
92 93
    enforce_blocking = policy => data_set("enforcing", policy);
93 94

  
......
118 119

  
119 120
@pytest.mark.ext_data({'content_script': content_script})
120 121
@pytest.mark.usefixtures('webextension')
121
@pytest.mark.parametrize('target1', ['dynamic_before'])#, 'dynamic_after'])
122
@pytest.mark.parametrize('target1', ['dynamic_before', 'dynamic_after'])
122 123
@pytest.mark.parametrize('target2', [
123 124
    'scripts_blocked',
124 125
    'payload_error',
......
144 145
    assert data['bad_request_returned'] == False
145 146

  
146 147
    assert data['cacher_started'] == True
148
    assert data.get('apis_started', False) == (target2 == 'payload_ok')
147 149

  
148 150
    for obj in (data['good_request_result'], data['enforcing']):
149 151
        assert obj['allow'] == False
......
162 164

  
163 165
    def vars_made_by_payload(driver):
164 166
        vars_values = driver.execute_script(
165
            'return [1, 2].map(n => window[`hak_injected_${n}`]);'
166
        )
167
        if vars_values != [None, None]:
167
            '''
168
            return [
169
                ...[1, 2].map(n => window[`hak_injected_${n}`]),
170
                window.haketilo_version
171
            ];
172
            ''')
173
        if vars_values != [None, None, None]:
168 174
            return vars_values
169 175

  
170 176
    if target2 == 'payload_error':
......
174 180
        }
175 181
    elif target2 == 'payload_ok':
176 182
        vars_values = WebDriverWait(driver, 10).until(vars_made_by_payload)
177
        assert vars_values == [1, 2]
183
        assert vars_values[:2] == [1, 2]
184
        assert type(vars_values[2]) == str
178 185

  
179 186
@pytest.mark.ext_data({'content_script': content_script})
180 187
@pytest.mark.usefixtures('webextension')

Also available in: Unified diff