From 26e4800ddf9b4384a083f066f2a396b8e5e6c079 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Tue, 1 Feb 2022 08:22:34 +0100 Subject: more improvements for abrowser&librewolf --- common/broadcast.js | 2 +- common/indexeddb.js | 43 ++++++++++++++------------- configure | 2 +- html/popup.html | 4 +++ html/popup.js | 23 +++++++++++---- test/conftest.py | 9 +++--- test/extension_crafting.py | 2 +- test/unit/test_patterns_query_manager.py | 13 +++++---- test/unit/test_popup.py | 50 +++++++++----------------------- 9 files changed, 70 insertions(+), 78 deletions(-) diff --git a/common/broadcast.js b/common/broadcast.js index 4fc5237..12b365a 100644 --- a/common/broadcast.js +++ b/common/broadcast.js @@ -119,6 +119,6 @@ function close(conn) { if (conn.type === "sender") flush(conn); - conn.port.disconnect(); + setTimeout(conn.port.disconnect()); } #EXPORT close diff --git a/common/indexeddb.js b/common/indexeddb.js index f916162..bdf71e5 100644 --- a/common/indexeddb.js +++ b/common/indexeddb.js @@ -99,9 +99,25 @@ async function idb_del(transaction, store_name, key) return wait_request(transaction.objectStore(store_name).delete(key)); } +async function perform_upgrade(event) { + const opened_db = event.target.result; + + /* When we move to a new database schema, we will add upgrade logic here. */ + if (event.oldVersion > 0) + throw "bad db version: " + event.oldVersion; + + let store; + for (const [store_name, key_mode] of stores) + store = opened_db.createObjectStore(store_name, key_mode); + + const ctx = make_context(store.transaction, initial_data.files); + await _save_items(initial_data.resources, initial_data.mappings, ctx); + + return opened_db; +} + /* Open haketilo database, asynchronously return an IDBDatabase object. */ -async function get_db() -{ +async function get_db() { if (db) return db; @@ -109,28 +125,11 @@ async function get_db() const waiter = new Promise((...cbs) => [resolve, reject] = cbs); const request = indexedDB.open("haketilo", version_nr(db_version)); - request.onsuccess = resolve; + request.onsuccess = ev => resolve(ev.target.result); request.onerror = ev => reject("db error: " + ev.target.errorCode); - request.onupgradeneeded = resolve; - - const event = await waiter; - const opened_db = event.target.result; + request.onupgradeneeded = ev => perform_upgrade(ev).then(resolve, reject); - if (event instanceof IDBVersionChangeEvent) { - /* - * When we move to a new database schema, we will add upgrade logic - * here. - */ - if (event.oldVersion > 0) - throw "bad db version: " + event.oldVersion; - - let store; - for (const [store_name, key_mode] of stores) - store = opened_db.createObjectStore(store_name, key_mode); - - const ctx = make_context(store.transaction, initial_data.files); - await _save_items(initial_data.resources, initial_data.mappings, ctx); - } + const opened_db = await waiter; if (db) opened_db.close(); diff --git a/configure b/configure index bd7800f..50f75dc 100755 --- a/configure +++ b/configure @@ -88,7 +88,7 @@ if [ "x$BROWSER_BINARY" = x ]; then if [ "x$TARGET" = xabrowser ]; then # Trisquel's path to Abrowser BROWSER_BINARY=/usr/lib/abrowser/abrowser - if [ "x$TARGET" = xabrowser ]; then + elif [ "x$TARGET" = xlibrewolf ]; then # Debian's path to Librewolf BROWSER_BINARY=/usr/share/librewolf/librewolf elif [ "x$TARGET" = xicecat ]; then diff --git a/html/popup.html b/html/popup.html index bb30425..d0a6cb9 100644 --- a/html/popup.html +++ b/html/popup.html @@ -70,6 +70,10 @@ #info_form label+span, .top_but_container { padding-bottom: 0.5em; } + + #info_form .long_msg { + white-space: normal; + } diff --git a/html/popup.js b/html/popup.js index 532feba..ddc0f51 100644 --- a/html/popup.js +++ b/html/popup.js @@ -83,6 +83,8 @@ function show_page_info(page_info) { if (page_info.payload) { if ("error" in page_info) { + let long_msg = true; + if (page_info.error.haketilo_error_type === "missing") payload_text = `None (error: resource with id '${page_info.error.id}' missing from the database)`; else if (page_info.error.haketilo_error_type === "circular") @@ -91,6 +93,11 @@ function show_page_info(page_info) { payload_text = `None (error: failure reading Haketilo internal database)`; else if (page_info.error.haketilo_error_type === "other") payload_text = `None (error: unknown failure occured)`; + else + long_msg = false; + + if (long_msg) + by_id("injected_payload").classList.add("long_msg"); } else { payload_text = page_info.payload.identifier; } @@ -100,19 +107,23 @@ function show_page_info(page_info) { const scripts_fate = page_info.allow ? "allowed" : "blocked"; - let mapping_text; + let mapping_text, long_msg = true; - if (page_info.mapping === "~allow") + if (page_info.mapping === "~allow") { mapping_text = `None (scripts ${scripts_fate} by a rule)`; - else if ("error" in page_info - && page_info.error.haketilo_error_type ==="deciding_policy") + } else if ("error" in page_info && + page_info.error.haketilo_error_type === "deciding_policy") { mapping_text = `None (error occured when determining policy)`; - else if (page_info.mapping) + } else if (page_info.mapping) { mapping_text = page_info.mapping; - else + long_msg = false; + } else { mapping_text = `None (scripts ${scripts_fate} by default policy)`; + } by_id("mapping_used").innerText = mapping_text; + if (long_msg) + by_id("mapping_used").classList.add("long_msg"); } } diff --git a/test/conftest.py b/test/conftest.py index 4eea714..b9c46b0 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -78,13 +78,12 @@ def webextension(driver, request): driver.get('https://gotmyowndoma.in/') ext_path = make_extension(Path(driver.firefox_profile.path), **ext_data) addon_id = driver.install_addon(str(ext_path), temporary=True) - WebDriverWait(driver, 10).until( - EC.url_matches('^moz-extension://.*') - ) + get_url = lambda d: d.execute_script('return window.ext_page_url;') + ext_page_url = WebDriverWait(driver, 10).until(get_url) + driver.get(ext_page_url) if navigate_to is not None: - testpage_url = driver.execute_script('return window.location.href;') - driver.get(testpage_url.replace('testpage.html', navigate_to)) + driver.get(driver.current_url.replace('testpage.html', navigate_to)) yield diff --git a/test/extension_crafting.py b/test/extension_crafting.py index ed5792f..bf54691 100644 --- a/test/extension_crafting.py +++ b/test/extension_crafting.py @@ -136,7 +136,7 @@ default_test_page = ''' open_test_page_script = '''(() => { const page_url = browser.runtime.getURL("testpage.html"); const execute_details = { - code: `window.location.href=${JSON.stringify(page_url)};` + code: `window.wrappedJSObject.ext_page_url=${JSON.stringify(page_url)};` }; browser.tabs.query({currentWindow: true, active: true}) .then(t => browser.tabs.executeScript(t.id, execute_details)); diff --git a/test/unit/test_patterns_query_manager.py b/test/unit/test_patterns_query_manager.py index 4662e8a..13d0add 100644 --- a/test/unit/test_patterns_query_manager.py +++ b/test/unit/test_patterns_query_manager.py @@ -265,7 +265,7 @@ def test_pqm_script_injection(driver, execute_in_page): json_txt = run_content_script() if json_txt and json.loads(json_txt) == {}: break; - assert attempt != 2 + assert attempt != 1 driver.switch_to.window(windows[0]) execute_in_page(load_script('common/indexeddb.js')) @@ -283,7 +283,7 @@ def test_pqm_script_injection(driver, execute_in_page): json.loads(tree_json) if all([m['identifier'] in tree_json for m in sample_mappings]): break - assert attempt != 2 + assert attempt != 1 driver.switch_to.window(windows[0]) execute_in_page( @@ -300,7 +300,8 @@ def test_pqm_script_injection(driver, execute_in_page): }''', [sm['identifier'] for sm in sample_mappings]) - for attempt in range(10): - if json.loads(run_content_script()) == {}: - break - assert attempt != 9 + for attempt in range(2): + json_txt = run_content_script() + if json_txt and json.loads(json_txt) == {}: + break; + assert attempt != 1 diff --git a/test/unit/test_popup.py b/test/unit/test_popup.py index da125ec..3163adb 100644 --- a/test/unit/test_popup.py +++ b/test/unit/test_popup.py @@ -20,21 +20,11 @@ Haketilo unit tests - repository querying import pytest import json from selenium.webdriver.support.ui import WebDriverWait -from selenium.common.exceptions import ElementNotInteractableException from ..extension_crafting import ExtraHTML from ..script_loader import load_script from .utils import * -def reload_with_target(driver, target): - current_url = driver.execute_script('return location.href') - driver.execute_script( - ''' - window.location.href = arguments[0]; - window.location.reload(); - ''', - f'{current_url}#{target}') - unprivileged_page_info = { 'url': 'https://example_a.com/something', 'allow': False @@ -145,18 +135,16 @@ def test_popup_display(driver, execute_in_page, page_info_key): possible values of page_info object passed in message from the content script. """ - reload_with_target(driver, f'mock_page_info-{page_info_key}') + initial_url = driver.current_url + driver.get('about:blank') + driver.get(f'{initial_url}#mock_page_info-{page_info_key}') - def get_nodes_by_id(driver): - by_id = driver.execute_script( - ''' - const nodes = [...document.querySelectorAll("[id]")]; - const reductor = (ob, node) => Object.assign(ob, {[node.id]: node}); - return nodes.reduce(reductor, {}); - '''); - return by_id if by_id and 'repo_query_container' in by_id else None - - by_id = WebDriverWait(driver, 10).until(get_nodes_by_id) + by_id = driver.execute_script( + ''' + const nodes = [...document.querySelectorAll("[id]")]; + const reductor = (ob, node) => Object.assign(ob, {[node.id]: node}); + return nodes.reduce(reductor, {}); + ''') if page_info_key == '': error_msg = 'Page info not avaialable. Try reloading the page.' @@ -218,23 +206,13 @@ def test_popup_repo_query(driver, execute_in_page): """ Test opening and closing the repo query view in popup. """ - reload_with_target(driver, f'mock_page_info-blocked_rule') + initial_url = driver.current_url + driver.get('about:blank') + driver.get(f'{initial_url}#mock_page_info-blocked_rule') - driver.implicitly_wait(10) search_but = driver.find_element_by_id("search_resources_but") - driver.implicitly_wait(0) - - # For unknown reasons waiting for search_but.is_displayed() to return True - # does not guarantee the button will be interactable afterwards under newer - # browsers. Hence, this workaround. - def click_search_but(driver): - try: - search_but.click() - return True - except ElementNotInteractableException: - pass - - WebDriverWait(driver, 10).until(click_search_but) + WebDriverWait(driver, 10).until(lambda d: search_but.is_displayed()) + search_but.click() containers = dict([(name, driver.find_element_by_id(f'{name}_container')) for name in ('page_info', 'repo_query')]) -- cgit v1.2.3