aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorWojtek Kosior <koszko@koszko.org>2021-12-22 16:39:34 +0100
committerWojtek Kosior <koszko@koszko.org>2021-12-22 16:39:34 +0100
commitb590eaa2f64ead3384eadc6fe58f6358aa1a0478 (patch)
tree8f1e9403c1a75246c2a9a0afc4ab30706ea7afbe /common
parentb7378a9994724750198e0d165c575be8538334fb (diff)
downloadbrowser-extension-b590eaa2f64ead3384eadc6fe58f6358aa1a0478.tar.gz
browser-extension-b590eaa2f64ead3384eadc6fe58f6358aa1a0478.zip
reworked build system; added missing license notices
Diffstat (limited to 'common')
-rw-r--r--common/ajax.js6
-rw-r--r--common/broadcast.js29
-rw-r--r--common/browser.js26
-rw-r--r--common/connection_types.js18
-rw-r--r--common/entities.js15
-rw-r--r--common/indexeddb.js63
-rw-r--r--common/lock.js17
-rw-r--r--common/message_server.js12
-rw-r--r--common/misc.js43
-rw-r--r--common/observables.js (renamed from common/observable.js)18
-rw-r--r--common/once.js6
-rw-r--r--common/patterns.js13
-rw-r--r--common/patterns_query_tree.js27
-rw-r--r--common/sanitize_JSON.js6
-rw-r--r--common/settings_query.js27
-rw-r--r--common/sha256.js8
-rw-r--r--common/storage_client.js21
-rw-r--r--common/storage_light.js39
-rw-r--r--common/storage_raw.js33
-rw-r--r--common/stored_types.js12
20 files changed, 161 insertions, 278 deletions
diff --git a/common/ajax.js b/common/ajax.js
index d61faa6..4d0e630 100644
--- a/common/ajax.js
+++ b/common/ajax.js
@@ -67,8 +67,4 @@ function make_ajax_request(method, url)
initiate_ajax_request(resolve, reject, method, url));
}
-/*
- * EXPORTS_START
- * EXPORT make_ajax_request
- * EXPORTS_END
- */
+#EXPORT make_ajax_request
diff --git a/common/broadcast.js b/common/broadcast.js
index cc11a20..b69f352 100644
--- a/common/broadcast.js
+++ b/common/broadcast.js
@@ -41,11 +41,9 @@
* proprietary program, I am not going to enforce this in court.
*/
-/*
- * IMPORTS_START
- * IMPORT CONNECTION_TYPE
- * IMPORTS_END
- */
+#IMPORT common/connection_types.js AS CONNECTION_TYPE
+
+#FROM common/browser.js IMPORT browser
function sender_connection()
{
@@ -53,11 +51,13 @@ function sender_connection()
port: browser.runtime.connect({name: CONNECTION_TYPE.BROADCAST_SEND})
};
}
+#EXPORT sender_connection
function out(sender_conn, channel_name, value)
{
sender_conn.port.postMessage(["broadcast", channel_name, value]);
}
+#EXPORT out
/*
* prepare()'d message will be broadcasted if the connection is closed or when
@@ -77,16 +77,19 @@ function prepare(sender_conn, channel_name, value, timeout=5000)
{
sender_conn.port.postMessage(["prepare", channel_name, value, timeout]);
}
+#EXPORT prepare
function discard(sender_conn)
{
sender_conn.port.postMessage(["discard"]);
}
+#EXPORT discard
function flush(sender_conn)
{
sender_conn.port.postMessage(["flush"]);
}
+#EXPORT flush
function listener_connection(cb)
{
@@ -98,30 +101,22 @@ function listener_connection(cb)
return conn;
}
+#EXPORT listener_connection
function subscribe(listener_conn, channel_name)
{
listener_conn.port.postMessage(["subscribe", channel_name]);
}
+#EXPORT subscribe
function unsubscribe(listener_conn, channel_name)
{
listener_conn.port.postMessage(["unsubscribe", channel_name]);
}
+#EXPORT unsubscribe
function close(conn)
{
conn.port.disconnect();
}
-
-const broadcast = {
- sender_connection, out, prepare, discard, flush,
- listener_connection, subscribe, unsubscribe,
- close
-};
-
-/*
- * EXPORTS_START
- * EXPORT broadcast
- * EXPORTS_END
- */
+#EXPORT close
diff --git a/common/browser.js b/common/browser.js
new file mode 100644
index 0000000..4830774
--- /dev/null
+++ b/common/browser.js
@@ -0,0 +1,26 @@
+/**
+ * This file is part of Haketilo.
+ *
+ * Function: Export the browser API object.
+ *
+ * Copyright (C) 2021 Wojtek Kosior
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the CC0 1.0 Universal License as published by
+ * the Creative Commons Corporation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * CC0 1.0 Universal License for more details.
+ */
+
+#IF MOZILLA
+
+#EXPORT globalThis.browser AS browser
+
+#ELIF CHROMIUM
+
+#EXPORT chrome AS browser
+
+#ENDIF
diff --git a/common/connection_types.js b/common/connection_types.js
index 9747e5c..0b83c2b 100644
--- a/common/connection_types.js
+++ b/common/connection_types.js
@@ -46,16 +46,8 @@
* to browser.runtime.connect()
*/
-const CONNECTION_TYPE = {
- REMOTE_STORAGE : "0",
- PAGE_ACTIONS : "1",
- ACTIVITY_INFO : "2",
- BROADCAST_SEND: "3",
- BROADCAST_LISTEN: "4"
-};
-
-/*
- * EXPORTS_START
- * EXPORT CONNECTION_TYPE
- * EXPORTS_END
- */
+#EXPORT "0" AS REMOTE_STORAGE
+#EXPORT "1" AS PAGE_ACTIONS
+#EXPORT "2" AS ACTIVITY_INFO
+#EXPORT "3" AS BROADCAST_SEND
+#EXPORT "4" AS BROADCAST_LISTEN
diff --git a/common/entities.js b/common/entities.js
index 3a1346a..29e130c 100644
--- a/common/entities.js
+++ b/common/entities.js
@@ -80,6 +80,7 @@ function get_newest_version(versioned_item)
const best_ver = max(Object.keys(versioned_item).map(parse_version));
return versioned_item[version_string(best_ver)];
}
+#EXPORT get_newest_version AS get_newest
/*
* item is a definition of a resource or mapping. Yield all file references
@@ -95,17 +96,9 @@ function* get_used_files(item)
yield file;
}
}
+#EXPORT get_used_files AS get_files
-const entities = {
- get_newest: get_newest_version,
- get_files: get_used_files
-};
-
-/*
- * EXPORTS_START
- * EXPORT entities
- * EXPORTS_END
- */
+#IF NEVER
/*
* Note: the functions below were overeagerly written and are not used now but
@@ -146,3 +139,5 @@ const version_reductor = (acc, n) => [...(n || acc.length ? [n] : []), ...acc];
* Returns a *new* array. Doesn't modify its argument.
*/
const normalize_version = ver => Array.reduceRight(ver, version_reductor, []);
+
+#ENDIF
diff --git a/common/indexeddb.js b/common/indexeddb.js
index 1741c91..c97c115 100644
--- a/common/indexeddb.js
+++ b/common/indexeddb.js
@@ -41,13 +41,12 @@
* proprietary program, I am not going to enforce this in court.
*/
-/*
- * IMPORTS_START
- * IMPORT initial_data
- * IMPORT entities
- * IMPORT broadcast
- * IMPORTS_END
- */
+#IMPORT common/entities.js
+#IMPORT common/broadcast.js
+
+let initial_data = (
+#INCLUDE_VERBATIM default_settings.json
+);
/* Update when changes are made to database schema. Must have 3 elements */
const db_version = [1, 0, 0];
@@ -79,6 +78,7 @@ async function idb_get(transaction, store_name, key)
const req = transaction.objectStore(store_name).get(key);
return (await wait_request(req)).target.result;
}
+#EXPORT idb_get
/* asynchronous wrapper for IDBObjectStore's put() method. */
async function idb_put(transaction, store_name, object)
@@ -132,6 +132,7 @@ async function get_db()
return db;
}
+#EXPORT get_db AS get
/* Helper function used by make_context(). */
function reject_discard(context)
@@ -177,10 +178,11 @@ function make_context(transaction, files)
*/
async function start_items_transaction(item_store_names, files)
{
- const db = await haketilodb.get();
+ const db = await get_db();
const scope = [...item_store_names, "files", "file_uses"];
return make_context(db.transaction(scope, "readwrite"), files);
}
+#EXPORT start_items_transaction
async function incr_file_uses(context, file_ref, by=1)
{
@@ -242,6 +244,7 @@ async function finalize_items_transaction(context)
return context.result;
}
+#EXPORT finalize_items_transaction
/*
* How a sample data argument to the function below might look like:
@@ -287,6 +290,7 @@ async function save_items(data)
return _save_items(data.resources, data.mappings, context);
}
+#EXPORT save_items
async function _save_items(resources, mappings, context)
{
@@ -322,6 +326,7 @@ async function save_item(item, context)
await _remove_item(store_name, item.identifier, context, false);
await idb_put(context.transaction, store_name, item);
}
+#EXPORT save_item
/* Helper function used by remove_item() and save_item(). */
async function _remove_item(store_name, identifier, context)
@@ -348,11 +353,16 @@ async function remove_item(store_name, identifier, context)
await idb_del(context.transaction, store_name, identifier);
}
+const remove_resource = (id, ctx) => remove_item("resources", id, ctx);
+#EXPORT remove_resource
+
+const remove_mapping = (id, ctx) => remove_item("mappings", id, ctx);
+#EXPORT remove_mapping
+
/* Callback used when listening to broadcasts while tracking db changes. */
async function track_change(tracking, identifier)
{
- const transaction =
- (await haketilodb.get()).transaction([tracking.store_name]);
+ const transaction = (await get_db()).transaction([tracking.store_name]);
const new_val = await idb_get(transaction, tracking.store_name, identifier);
tracking.onchange({identifier, new_val});
@@ -373,7 +383,7 @@ async function track_change(tracking, identifier)
* }
*
* Returns a [tracking, all_current_items] array where `tracking` is an object
- * that can be later passed to haketilodb.untrack() to stop tracking changes and
+ * that can be later passed to untrack() to stop tracking changes and
* `all_current_items` is an array of items currently present in the object
* store.
*
@@ -388,33 +398,18 @@ async function track(store_name, onchange)
broadcast.listener_connection(msg => track_change(tracking, msg[1]));
broadcast.subscribe(tracking.listener, `idb_changes_${store_name}`);
- const transaction = (await haketilodb.get()).transaction([store_name]);
+ const transaction = (await get_db()).transaction([store_name]);
const all_req = transaction.objectStore(store_name).getAll();
return [tracking, (await wait_request(all_req)).target.result];
}
-function untrack(tracking)
-{
- broadcast.close(tracking.listener);
-}
+const track_resources = onchange => track("resources", onchange);
+#EXPORT track_resources
-const haketilodb = {
- get: get_db,
- save_items,
- save_item,
- remove_resource: (id, ctx) => remove_item("resources", id, ctx),
- remove_mapping: (id, ctx) => remove_item("mappings", id, ctx),
- start_items_transaction,
- finalize_items_transaction,
- track_resources: onchange => track("resources", onchange),
- track_mappings: onchange => track("mappings", onchange),
- untrack
-};
+const track_mappings = onchange => track("mappings", onchange);
+#EXPORT track_mappings
+
+const untrack = tracking => broadcast.close(tracking.listener);
+#EXPORT untrack
-/*
- * EXPORTS_START
- * EXPORT haketilodb
- * EXPORT idb_get
- * EXPORTS_END
- */
diff --git a/common/lock.js b/common/lock.js
index d136469..56dad4f 100644
--- a/common/lock.js
+++ b/common/lock.js
@@ -55,9 +55,7 @@
* in a promise.
*/
-function make_lock() {
- return {free: true, queue: []};
-}
+#EXPORT () => ({free: true, queue: []}) AS make_lock
function _lock(lock, cb) {
if (lock.free) {
@@ -68,9 +66,7 @@ function _lock(lock, cb) {
}
}
-function lock(lock) {
- return new Promise((resolve, reject) => _lock(lock, resolve));
-}
+#EXPORT lock => new Promise(resolve => _lock(lock, resolve)) AS lock
function unlock(lock) {
if (lock.free)
@@ -84,11 +80,4 @@ function unlock(lock) {
setTimeout(cb);
}
}
-
-/*
- * EXPORTS_START
- * EXPORT make_lock
- * EXPORT lock
- * EXPORT unlock
- * EXPORTS_END
- */
+#EXPORT unlock
diff --git a/common/message_server.js b/common/message_server.js
index cd9a4d8..fd609c7 100644
--- a/common/message_server.js
+++ b/common/message_server.js
@@ -41,11 +41,7 @@
* proprietary program, I am not going to enforce this in court.
*/
-/*
- * IMPORTS_START
- * IMPORT browser
- * IMPORTS_END
- */
+#FROM common/browser.js IMPORT browser
var listeners = {};
@@ -66,8 +62,4 @@ function raw_listen(port)
browser.runtime.onConnect.addListener(raw_listen);
-/*
- * EXPORTS_START
- * EXPORT listen_for_connection
- * EXPORTS_END
- */
+#EXPORT listen_for_connection
diff --git a/common/misc.js b/common/misc.js
index 4d4b346..dc4a598 100644
--- a/common/misc.js
+++ b/common/misc.js
@@ -42,21 +42,8 @@
* proprietary program, I am not going to enforce this in court.
*/
-/*
- * IMPORTS_START
- * IMPORT browser
- * IMPORT TYPE_NAME
- * IMPORT TYPE_PREFIX
- * IMPORTS_END
- */
-
-/* Generate a random base64-encoded 128-bit sequence */
-function gen_nonce()
-{
- let randomData = new Uint8Array(16);
- crypto.getRandomValues(randomData);
- return btoa(String.fromCharCode.apply(null, randomData));
-}
+#FROM common/browser.js IMPORT browser
+#FROM common/stored_types.js IMPORT TYPE_NAME, TYPE_PREFIX
/*
* generating unique, per-site value that can be computed synchronously
@@ -78,6 +65,7 @@ function gen_nonce(length=16)
crypto.getRandomValues(randomData);
return Uint8toHex(randomData);
}
+#EXPORT gen_nonce
/* CSP rule that blocks scripts according to policy's needs. */
function make_csp_rule(policy)
@@ -88,19 +76,18 @@ function make_csp_rule(policy)
rule += ` script-src ${script_src}; script-src-elem ${script_src};`;
return rule;
}
+#EXPORT make_csp_rule
/* Check if some HTTP header might define CSP rules. */
const csp_header_regex =
/^\s*(content-security-policy|x-webkit-csp|x-content-security-policy)/i;
+#EXPORT csp_header_regex
/*
* Print item together with type, e.g.
* nice_name("s", "hello") → "hello (script)"
*/
-function nice_name(prefix, name)
-{
- return `${name} (${TYPE_NAME[prefix]})`;
-}
+#EXPORT (prefix, name) => `${name} (${TYPE_NAME[prefix]})` AS nice_name
/* Open settings tab with given item's editing already on. */
function open_in_settings(prefix, name)
@@ -109,6 +96,7 @@ function open_in_settings(prefix, name)
const url = browser.runtime.getURL("html/options.html#" + prefix + name);
window.open(url, "_blank");
}
+#EXPORT open_in_settings
/*
* Check if url corresponds to a browser's special page (or a directory index in
@@ -116,7 +104,7 @@ function open_in_settings(prefix, name)
*/
const privileged_reg =
/^(chrome(-extension)?|moz-extension):\/\/|^about:|^file:\/\/.*\/$/;
-const is_privileged_url = url => privileged_reg.test(url);
+#EXPORT url => privileged_reg.test(url) AS is_privileged_url
/* Parse a CSP header */
function parse_csp(csp) {
@@ -148,6 +136,7 @@ const matchers = {
nonempty_string_matcher
]
};
+#EXPORT matchers
/*
* Facilitates checking if there aren't any keys in object. This does *NOT*
@@ -159,16 +148,4 @@ function is_object_empty(object)
return false;
return true;
}
-
-/*
- * EXPORTS_START
- * EXPORT gen_nonce
- * EXPORT make_csp_rule
- * EXPORT csp_header_regex
- * EXPORT nice_name
- * EXPORT open_in_settings
- * EXPORT is_privileged_url
- * EXPORT matchers
- * EXPORT is_object_empty
- * EXPORTS_END
- */
+#EXPORT is_object_empty
diff --git a/common/observable.js b/common/observables.js
index 56d0ed6..f1db88c 100644
--- a/common/observable.js
+++ b/common/observables.js
@@ -41,13 +41,16 @@
* proprietary program, I am not going to enforce this in court.
*/
-const make = (value=undefined) => ({value, listeners: new Set()});
-const subscribe = (observable, cb) => observable.listeners.add(cb);
-const unsubscribe = (observable, cb) => observable.listeners.delete(cb);
+#EXPORT (value=undefined) => ({value, listeners: new Set()}) AS make
+#EXPORT (observable, cb) => observable.listeners.add(cb) AS subscribe
+#EXPORT (observable, cb) => observable.listeners.delete(cb) AS unsubscribe
const silent_set = (observable, value) => observable.value = value;
+#EXPORT silent_set
+
const broadcast = (observable, ...values) =>
observable.listeners.forEach(cb => cb(...values));
+#EXPORT broadcast
function set(observable, value)
{
@@ -55,11 +58,4 @@ function set(observable, value)
silent_set(observable, value);
broadcast(observable, value, old_value);
}
-
-const observables = {make, subscribe, unsubscribe, broadcast, silent_set, set};
-
-/*
- * EXPORTS_START
- * EXPORT observables
- * EXPORTS_END
- */
+#EXPORT set
diff --git a/common/once.js b/common/once.js
index 5a62b09..6e82528 100644
--- a/common/once.js
+++ b/common/once.js
@@ -71,8 +71,4 @@ function make_once(result_producer)
return () => get_result(state);
}
-/*
- * EXPORTS_START
- * EXPORT make_once
- * EXPORTS_END
- */
+#EXPORT make_once
diff --git a/common/patterns.js b/common/patterns.js
index 7d28dfe..0b1c3ad 100644
--- a/common/patterns.js
+++ b/common/patterns.js
@@ -72,9 +72,9 @@ function match_or_throw(regex, string, error_msg)
function deconstruct_url(url, use_limits=true)
{
- const max = MAX;
+ const max = Object.assign({}, MAX);
if (!use_limits) {
- for (key in MAX)
+ for (const key in MAX)
max[key] = Infinity;
}
@@ -129,6 +129,7 @@ function deconstruct_url(url, use_limits=true)
return deco;
}
+#EXPORT deconstruct_url
function* each_domain_pattern(deco)
{
@@ -183,10 +184,4 @@ function* each_url_pattern(url)
yield `${deco.proto}://${domain}${path}`;
}
}
-
-/*
- * EXPORTS_START
- * EXPORT each_url_pattern
- * EXPORT deconstruct_url
- * EXPORTS_END
- */
+#EXPORT each_url_pattern
diff --git a/common/patterns_query_tree.js b/common/patterns_query_tree.js
index 49205c5..1bbdb39 100644
--- a/common/patterns_query_tree.js
+++ b/common/patterns_query_tree.js
@@ -41,17 +41,16 @@
* proprietary program, I am not going to enforce this in court.
*/
-/*
- * IMPORTS_START
- * IMPORT deconstruct_url
- * IMPORTS_END
- */
+#FROM common/patterns.js IMPORT deconstruct_url
/* "Pattern Tree" is how we refer to the data structure used for querying
* Haketilo patterns. Those look like 'https://*.example.com/ab/***'. The goal
* is to make it possible for given URL to quickly retrieve all known patterns
* that match it.
*/
+const pattern_tree_make = () => ({})
+#EXPORT pattern_tree_make AS make
+
function empty_node() {
return {
wildcard_matches: [null, null, null],
@@ -134,8 +133,6 @@ function modify_sequence(tree_node, segments, item_modifier)
let removed = true;
for (var current_segment of segments) {
- wildcards = tree_node.wildcard_matches;
-
const child = tree_node.children[current_segment] || empty_node();
tree_node.children[current_segment] = child;
tree_node = child;
@@ -216,6 +213,7 @@ function pattern_tree_register(patterns_by_proto, pattern, item_name, item)
const add_item = obj => Object.assign(obj || {}, {[item_name]: item});
modify_tree(patterns_by_proto, pattern, add_item);
}
+#EXPORT pattern_tree_register AS register
/* Helper function for pattern_tree_deregister(). */
function _remove_item(obj, item_name)
@@ -240,6 +238,7 @@ function pattern_tree_deregister(patterns_by_proto, pattern, item_name)
const remove_item = obj => _remove_item(obj, item_name);
modify_tree(patterns_by_proto, pattern, remove_item);
}
+#EXPORT pattern_tree_deregister AS deregister
/*
* Yield registered items that match url. Each yielded value is an object with
@@ -281,16 +280,4 @@ function* pattern_tree_search(patterns_by_proto, url)
}
}
}
-
-const pattern_tree = {
- make: () => ({}),
- register: pattern_tree_register,
- deregister: pattern_tree_deregister,
- search: pattern_tree_search
-}
-
-/*
- * EXPORTS_START
- * EXPORT pattern_tree
- * EXPORTS_END
- */
+#EXPORT pattern_tree_search AS search
diff --git a/common/sanitize_JSON.js b/common/sanitize_JSON.js
index c775acb..58519b2 100644
--- a/common/sanitize_JSON.js
+++ b/common/sanitize_JSON.js
@@ -428,8 +428,4 @@ const checks = [
[eq("discard"), i => true, discard, "dummy"]
];
-/*
- * EXPORTS_START
- * EXPORT parse_json_with_schema
- * EXPORTS_END
- */
+#EXPORT parse_json_with_schema
diff --git a/common/settings_query.js b/common/settings_query.js
index 460f265..30e614f 100644
--- a/common/settings_query.js
+++ b/common/settings_query.js
@@ -41,12 +41,9 @@
* proprietary program, I am not going to enforce this in court.
*/
-/*
- * IMPORTS_START
- * IMPORT TYPE_PREFIX
- * IMPORT each_url_pattern
- * IMPORTS_END
- */
+
+#FROM common/stored_types.js IMPORT TYPE_PREFIX
+#FROM common/patterns.js IMPORT each_url_pattern
function query(storage, url, multiple)
{
@@ -65,19 +62,5 @@ function query(storage, url, multiple)
return multiple ? matched : [undefined, undefined];
}
-function query_best(storage, url)
-{
- return query(storage, url, false);
-}
-
-function query_all(storage, url)
-{
- return query(storage, url, true);
-}
-
-/*
- * EXPORTS_START
- * EXPORT query_best
- * EXPORT query_all
- * EXPORTS_END
- */
+#EXPORT (storage, url) => query(storage, url, false) AS query_best
+#EXPORT (storage, url) => query(storage, url, true) AS query_all
diff --git a/common/sha256.js b/common/sha256.js
index 3a02d7a..088473e 100644
--- a/common/sha256.js
+++ b/common/sha256.js
@@ -533,10 +533,4 @@ if (COMMON_JS) {
}
}
-const sha256 = fake_window.sha256;
-
-/*
- * EXPORTS_START
- * EXPORT sha256
- * EXPORTS_END
- */
+#EXPORT fake_window.sha256 AS sha256
diff --git a/common/storage_client.js b/common/storage_client.js
index f310648..4bc3c3c 100644
--- a/common/storage_client.js
+++ b/common/storage_client.js
@@ -41,14 +41,11 @@
* proprietary program, I am not going to enforce this in court.
*/
-/*
- * IMPORTS_START
- * IMPORT CONNECTION_TYPE
- * IMPORT list_prefixes
- * IMPORT make_once
- * IMPORT browser
- * IMPORTS_END
- */
+#IMPORT common/connection_types.js AS CONNECTION_TYPE
+
+#FROM common/browser.js IMPORT browser
+#FROM common/stored_types.js IMPORT list_prefixes
+#FROM common/once.js IMPORT make_once
var call_id = 0;
var port;
@@ -208,10 +205,4 @@ exports.get_all_it = function (prefix)
return list_entries_it(list_by_prefix[prefix]);
}
-const get_remote_storage = make_once(init);
-
-/*
- * EXPORTS_START
- * EXPORT get_remote_storage
- * EXPORTS_END
- */
+#EXPORT make_once(init) AS get_remote_storage
diff --git a/common/storage_light.js b/common/storage_light.js
index a315858..9ec3020 100644
--- a/common/storage_light.js
+++ b/common/storage_light.js
@@ -41,14 +41,10 @@
* proprietary program, I am not going to enforce this in court.
*/
-/*
- * IMPORTS_START
- * IMPORT TYPE_PREFIX
- * IMPORT raw_storage
- * IMPORT is_mozilla
- * IMPORT observables
- * IMPORTS_END
- */
+#IMPORT common/storage_raw.js AS raw_storage
+#IMPORT common/observables.js
+
+#FROM common/stored_types.js IMPORT TYPE_PREFIX
const reg_spec = new Set(["\\", "[", "]", "(", ")", "{", "}", ".", "*", "+"]);
const escape_reg_special = c => reg_spec.has(c) ? "\\" + c : c;
@@ -80,6 +76,7 @@ function listen(callback, prefix, name)
by_name.set(name, name_reg);
}
}
+#EXPORT listen
function no_listen(callback, prefix, name)
{
@@ -103,11 +100,16 @@ function no_listen(callback, prefix, name)
if (by_prefix.size === 0)
listeners_by_callback.delete(callback);
}
+#EXPORT no_listen
function storage_change_callback(changes, area)
{
- if (is_mozilla && area !== "local")
- {console.log("area", area);return;}
+#IF MOZILLA
+ if (area !== "local") {
+ console.warn("change in storage area", area);
+ return;
+ }
+#ENDIF
for (const item of Object.keys(changes)) {
for (const [callback, by_prefix] of listeners_by_callback.entries()) {
@@ -144,22 +146,17 @@ async function observe(prefix, name)
return observable;
}
+#EXPORT observe
-const observe_var = name => observe(TYPE_PREFIX.VAR, name);
+#EXPORT name => observe(TYPE_PREFIX.VAR, name) AS observe_var
function no_observe(observable)
{
no_listen(...created_observables.get(observable) || []);
created_observables.delete(observable);
}
+#EXPORT no_observe
-const light_storage = {};
-Object.assign(light_storage, raw_storage);
-Object.assign(light_storage,
- {listen, no_listen, observe, observe_var, no_observe});
-
-/*
- * EXPORTS_START
- * EXPORT light_storage
- * EXPORTS_END
- */
+#EXPORT raw_storage.set AS set
+#EXPORT raw_storage.set_var AS set_var
+#EXPORT raw_storage.get_var AS get_var
diff --git a/common/storage_raw.js b/common/storage_raw.js
index c79fe84..4009f13 100644
--- a/common/storage_raw.js
+++ b/common/storage_raw.js
@@ -41,23 +41,20 @@
* proprietary program, I am not going to enforce this in court.
*/
-/*
- * IMPORTS_START
- * IMPORT TYPE_PREFIX
- * IMPORT browser
- * IMPORT is_chrome
- * IMPORTS_END
- */
+#FROM common/browser.js IMPORT browser
+#FROM common/stored_types.js IMPORT TYPE_PREFIX
async function get(key)
{
- /* Fix for fact that Chrome does not use promises here */
- const promise = is_chrome ?
- new Promise(resolve => chrome.storage.local.get(key, resolve)) :
- browser.storage.local.get(key);
+#IF CHROMIUM
+ const promise = new Promise(cb => browser.storage.local.get(key, cb));
+#ELIF MOZILLA
+ const promise = browser.storage.local.get(key);
+#ENDIF
return (await promise)[key];
}
+#EXPORT get
async function set(key_or_object, value)
{
@@ -65,25 +62,21 @@ async function set(key_or_object, value)
key_or_object : {[key_or_object]: value};
return browser.storage.local.set(arg);
}
+#EXPORT set
async function set_var(name, value)
{
return set(TYPE_PREFIX.VAR + name, value);
}
+#EXPORT set_var
async function get_var(name)
{
return get(TYPE_PREFIX.VAR + name);
}
+#EXPORT get_var
const on_changed = browser.storage.onChanged || browser.storage.local.onChanged;
-const listen = cb => on_changed.addListener(cb);
-const no_listen = cb => on_changed.removeListener(cb);
-
-const raw_storage = {get, set, get_var, set_var, listen, no_listen};
-/*
- * EXPORTS_START
- * EXPORT raw_storage
- * EXPORTS_END
- */
+#EXPORT cb => on_changed.addListener(cb) AS listen
+#EXPORT cb => on_changed.removeListener(cb) AS no_listen
diff --git a/common/stored_types.js b/common/stored_types.js
index 6c69dd7..485969b 100644
--- a/common/stored_types.js
+++ b/common/stored_types.js
@@ -59,6 +59,8 @@ const TYPE_PREFIX = {
URL : "u"
};
+#EXPORT TYPE_PREFIX
+
const TYPE_NAME = {
[TYPE_PREFIX.REPO] : "repo",
[TYPE_PREFIX.PAGE] : "page",
@@ -66,6 +68,8 @@ const TYPE_NAME = {
[TYPE_PREFIX.SCRIPT] : "script"
}
+#EXPORT TYPE_NAME
+
const list_prefixes = [
TYPE_PREFIX.REPO,
TYPE_PREFIX.PAGE,
@@ -73,10 +77,4 @@ const list_prefixes = [
TYPE_PREFIX.SCRIPT
];
-/*
- * EXPORTS_START
- * EXPORT TYPE_PREFIX
- * EXPORT TYPE_NAME
- * EXPORT list_prefixes
- * EXPORTS_END
- */
+#EXPORT list_prefixes