From 7f368d46ea06164da025c1ac4ed9a65ad23b25ef Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Wed, 12 May 2021 16:00:09 +0200 Subject: stop using js modules --- TODOS.org | 4 +- background/background.html | 14 +- background/main.js | 168 ++++++++++++ background/main.mjs | 166 ------------ background/message_server.js | 35 +++ background/message_server.mjs | 31 --- background/page_actions_server.js | 149 +++++++++++ background/page_actions_server.mjs | 145 ---------- background/policy_smuggler.js | 64 +++++ background/policy_smuggler.mjs | 60 ----- background/reverse_use_info.js | 96 +++++++ background/reverse_use_info.mjs | 93 ------- background/sha256.js | 523 ++++++++++++++++++++++++++++++++++++ background/sha256.mjs | 524 ------------------------------------- background/storage.js | 397 ++++++++++++++++++++++++++++ background/storage.mjs | 380 --------------------------- background/storage_server.js | 65 +++++ background/storage_server.mjs | 58 ---- background/url_item.js | 22 ++ background/url_item.mjs | 18 -- common/browser.js | 27 ++ common/browser.mjs | 18 -- common/connection_types.js | 25 ++ common/connection_types.mjs | 21 -- common/is_background.mjs | 17 -- common/lock.js | 61 +++++ common/lock.mjs | 55 ---- common/once.js | 44 ++++ common/once.mjs | 42 --- common/storage_client.js | 189 +++++++++++++ common/storage_client.mjs | 186 ------------- common/stored_types.js | 44 ++++ common/stored_types.mjs | 38 --- content/main.js | 162 ++++++------ content/page_actions.js | 63 +++++ content/page_actions.mjs | 58 ---- html/display-panel.html | 3 +- html/display-panel.js | 18 ++ html/display-panel.mjs | 16 -- html/options.html | 7 +- html/options_main.js | 399 ++++++++++++++++++++++++++++ html/options_main.mjs | 398 ---------------------------- manifest.json | 7 +- 43 files changed, 2498 insertions(+), 2412 deletions(-) create mode 100644 background/main.js delete mode 100644 background/main.mjs create mode 100644 background/message_server.js delete mode 100644 background/message_server.mjs create mode 100644 background/page_actions_server.js delete mode 100644 background/page_actions_server.mjs create mode 100644 background/policy_smuggler.js delete mode 100644 background/policy_smuggler.mjs create mode 100644 background/reverse_use_info.js delete mode 100644 background/reverse_use_info.mjs create mode 100644 background/sha256.js delete mode 100644 background/sha256.mjs create mode 100644 background/storage.js delete mode 100644 background/storage.mjs create mode 100644 background/storage_server.js delete mode 100644 background/storage_server.mjs create mode 100644 background/url_item.js delete mode 100644 background/url_item.mjs create mode 100644 common/browser.js delete mode 100644 common/browser.mjs create mode 100644 common/connection_types.js delete mode 100644 common/connection_types.mjs delete mode 100644 common/is_background.mjs create mode 100644 common/lock.js delete mode 100644 common/lock.mjs create mode 100644 common/once.js delete mode 100644 common/once.mjs create mode 100644 common/storage_client.js delete mode 100644 common/storage_client.mjs create mode 100644 common/stored_types.js delete mode 100644 common/stored_types.mjs create mode 100644 content/page_actions.js delete mode 100644 content/page_actions.mjs create mode 100644 html/display-panel.js delete mode 100644 html/display-panel.mjs create mode 100644 html/options_main.js delete mode 100644 html/options_main.mjs diff --git a/TODOS.org b/TODOS.org index 72aa13c..8c8bcaf 100644 --- a/TODOS.org +++ b/TODOS.org @@ -17,7 +17,6 @@ TODO: settings and settings for pages that currently happen to live in iframes - add some nice styling to settings page -- stop using modules (not available on all browsers) -- CRUCIAL - use non-predictable value in place of "myext-allow", utilizing hashes -- CRUCIAL - rename the extension to something good - port to gecko-based browsers -- CRUCIAL @@ -37,8 +36,11 @@ TODO: - check if prerendering has to be blocked -- CRUCIAL - block prefetch - rearrange files in extension, add some mechanism to build the extension +- all solutions to modularize js code SUCK; come up with own simple DSL + to manage imports/exports DONE: +- stop using modules (not available on all browsers) -- DONE 2021-05-12 - clean up the remnants of LibreJS -- DONE 2021-05-12 - implement whitelisting -- DONE 2021-05-07 - find way to also block scripts in non-http pages (e.g. file://) -- DONE 2021-05-07 (via content scripts, may not be perfect) diff --git a/background/background.html b/background/background.html index 519c2a2..d453c47 100644 --- a/background/background.html +++ b/background/background.html @@ -2,6 +2,18 @@ - + + + + + + + + + + + + + diff --git a/background/main.js b/background/main.js new file mode 100644 index 0000000..f4b01f8 --- /dev/null +++ b/background/main.js @@ -0,0 +1,168 @@ +/** +* Myext main background script +* +* Copyright (C) 2021 Wojtek Kosior +* +* Dual-licensed under: +* - 0BSD license +* - GPLv3 or (at your option) any later version +*/ + +"use strict"; + +(() => { + const TYPE_PREFIX = window.TYPE_PREFIX; + const get_storage = window.get_storage; + const start_storage_server = window.start_storage_server; + const start_page_actions_server = window.start_page_actions_server; + const start_policy_smuggler = window.start_policy_smuggler; + const browser = window.browser; + + start_storage_server(); + start_page_actions_server(); + start_policy_smuggler(); + + async function init_myext(install_details) + { + console.log("details:", install_details); + if (install_details.reason != "install") + return; + + let storage = await get_storage(); + + await storage.clear(); + + /* + * Below we add sample settings to the extension. + * Those should be considered example values for viewing in the options + * page. They won't make my.fsf.org work. The only scripts that does + * something useful right now is the opencores one. + */ + + let components = []; + for (let script_data of [ + {url: "http://127.0.0.1:8000/myfsf_define_CRM.js", + hash:"bf0cc81c7e8d5f800877b4bc3f14639f946f5ac6d4dc120255ffac5eba5e48fe"}, + {url: "https://my.fsf.org/misc/jquery.js?v=1.4.4", + hash:"261ae472fa0cbf27c80c9200a1599a60fde581a0e652eee4bf41def8cb61f2d0"}, + {url: "https://my.fsf.org/misc/jquery-extend-3.4.0.js?v=1.4.4", + hash:"c54103ba57ee210ca55c052e70415402707548a4e6a68dd6efb3895019bee392"}, + {url: "https://my.fsf.org/misc/jquery-html-prefilter-3.5.0-backport.js?v=1.4.4", + hash:"fad84efa145fb507e5df9b582fa01b1c4e6313de7f72ebdd55726d92fa4dbf06"}, + {url: "https://my.fsf.org/misc/jquery.once.js?v=1.2", + hash:"1430f42c0d760ba8e05bb3762480502e541f654fec5739ee40625ab22dc38c4f"}, + {url: "https://my.fsf.org/misc/drupal.js?qmaukd", + hash:"2e08dccbd4d8b728a6871562995a4636b89bfe0ed3b8fb0138191c922228b116"}, + {url: "https://my.fsf.org/sites/all/modules/civicrm/bower_components/jquery/dist/jquery.min.js?qmaukd", + hash:"a6d01520d28d15dbe476de84eea90eb3ee2d058722efc062ec73cb5fad78a17b"}, + {url: "https://my.fsf.org/sites/all/modules/civicrm/bower_components/jquery-ui/jquery-ui.min.js?qmaukd", + hash:"28ce75d953678c4942df47a11707a15e3c756021cf89090e3e6aa7ad6b6971c3"}, + {url: "https://my.fsf.org/sites/all/modules/civicrm/bower_components/lodash-compat/lodash.min.js?qmaukd", + hash:"f2871cc80c52fe8c04c582c4a49797c9c8fd80391cf1452e47f7fe97835ed5cc"}, + {url: "https://my.fsf.org/sites/all/modules/civicrm/packages/jquery/plugins/jquery.mousewheel.min.js?qmaukd", + hash:"f50233e84c2ac7ada37a094d3f7d3b3f7c97716d6b7b47bf69619d93ee4ac1ce"}, + {url: "https://my.fsf.org/sites/all/modules/civicrm/bower_components/select2/select2.min.js?qmaukd", + hash:"ce61298fb9aa4ec49ccd4172d097e36a9e5db3af06a7b82796659368f15b7c1b"}, + {url: "https://my.fsf.org/sites/all/modules/civi crm/packages/jquery/plugins/jquery.form.min.js?qmaukd", + hash:"c90f0e501d2948fbc2b61bffd654fa4ab64741fd48923782419eeb14d3816fb8"}, + {url: "https://my.fsf.org/sites/all/modules/civicrm/packages/jquery/plugins/jquery.timeentry.min.js?qmaukd", + hash:"8e85df981e8ad7049d06dfb075277d038734d36a7097c7f021021b2bdccfe9bb"}, + {url: "https://my.fsf.org/sites/all/modules/civicrm/packages/jquery/plugins/jquery.blockUI.min.js?qmaukd", + hash:"806aedff52ac822f2adc5797073e1e5c5cec32eb9f15f2319cb32a347dcd232b"}, + {url: "https://my.fsf.org/sites/all/modules/civicrm/bower_components/datatables/media/js/jquery.dataTables.min.js?qmaukd", + hash:"b796504d9b1b422f0dc6ccc2d740ac78a8c9e5078cc3934836d39742b1121925"}, + {url: "https://my.fsf.org/sites/all/modules/civicrm/bower_components/jquery-validation/dist/jquery.validate.min.js?qmaukd", + hash:"f0f5373ad203101ea91bf826c5a7ef8f7cd74887f06bad2cb9277a504503b9e2"}, + {url: "https://my.fsf.org/sites/all/modules/civicrm/packages/jquery/plugins/jquery.ui.datepicker.validation.min.js?qmaukd", + hash:"c6e6f6bf7f8fff25cca338045774e267e8eaa2d48ac9100540f3d59a6d2b3c61"}, + {url: "https://my.fsf.org/sites/all/modules/civicrm/js/Common.js?qmaukd", + hash:"17aa222a3af2e8958be16accb5e77ef39f67009cb3b500718d8fffd45b399148"}, + {url: "https://my.fsf.org/sites/all/modules/civicrm/js/crm.datepicker.js?qmaukd", + hash:"9bd8d10208aa99c156325f7819da6f0dd62ba221ac4119c3ccd4834e2cf36535"}, + {url: "https://my.fsf.org/sites/all/modules/civicrm/js/crm.ajax.js?qmaukd", + hash:"6401a4e257b7499ae4a00be2c200e4504a2c9b3d6b278a830c31a7b63374f0fe"}, + {url: "https://my.fsf.org/sites/all/modules/civicrm/js/wysiwyg/crm.wysiwyg.js?qmaukd", + hash:"fa962356072a36672c3b4b25bdeb657f020995a067e20a29cd5bb84b05157762"}, + {url: "https://my.fsf.org/sites/all/modules/civicrm/js/noconflict.js?qmaukd", + hash:"58d6d9f496a235d23cf891926d71f2104e4f2afe1d14bb4e2b5233f646c35e62"}, + {url: "https://my.fsf.org/sites/all/modules/matomo/matomo.js?qmaukd", + hash:"7f39ccd085f348189cd2fb62ea4d4a658d96f6bba266265880b98605e777e2de"}, + {url: "https://my.fsf.org/sites/all/themes/fsf_venture/js/global.js?qmaukd", + hash:"aa7983f6b902f9f4415cfc8346e0c3f194cc95b78f52f2ad09ec7effa1326b9c"}, + {url: "https://my.fsf.org/sites/all/themes/fsf_venture/js/jquery.superfish.min.js?qmaukd", + hash:"5ef1f93bf3901227056bf9ed0ed93a148eec4dda30f419756b12bedd1098815e"}, + {url: "https://my.fsf.org/sites/all/themes/fsf_venture/js/jquery.sidr.min.js?qmaukd", + hash:"c4914d415826676c6af2e61f16edb72c5388f8600ba6de9049892aee49d980a0"}, + {url: "https://my.fsf.org/sites/all/themes/fsf_venture/js/jquery.flexslider.min.js?qmaukd", + hash:"cefaf715761b4494913851249b9d40dacb4a8cb61242b0efc859dc586d56e0d4"}, + {url: "http://127.0.0.1:8000/myfsf_crap.js", + hash:"d91ccf21592d0f861ea0ba946bc257fc5d88269327cad0a91387da6cb8ff633e"}, + {url: "https://my.fsf.org/sites/all/modules/civicrm/templates/CRM/Core/BillingBlock.js?r=lp7Di", + hash:"2f25d35e7a0c0060ab0a444a577f09dd3c9934ae898a7ee0eb20b6c986ab5a1c"}, + {url: "https://my.fsf.org/extensions/com.aghstrategies.giftmemberships/js/giftpricefield.js?r=lp7Di", + hash:"f86080e6bd306fe46474039aeca2808235005bce5a2a29416d08210022039a45"}, + {url: "https://my.fsf.org/extensions/com.ginkgostreet.negativenegator/js/negativenegator.js?r=lp7Di", + hash:"d0e87bac832856db70947d82a7ab4e0b7c8b1070d5f1a32335345e033ece3a14"} + ]) { + let name_regex = /\/([^/]+)\.js/; + let name = name_regex.exec(script_data.url)[1]; + await storage.set(TYPE_PREFIX.SCRIPT, name, script_data); + components.push([TYPE_PREFIX.SCRIPT, name]); + } + + await storage.set(TYPE_PREFIX.PAGE, "https://my.fsf.org/join", {components}); + + let hello_script = { + text: "console.log(\"hello, every1!\");\n" + }; + await storage.set(TYPE_PREFIX.SCRIPT, "hello", hello_script); + await storage.set(TYPE_PREFIX.BUNDLE, "hello", + [[TYPE_PREFIX.SCRIPT, "hello"]]); + await storage.set(TYPE_PREFIX.PAGE, "https://my.fsf.org/", { + components: [[TYPE_PREFIX.BUNDLE, "hello"]], + allow: true + }); + + let opencores_script = { + text: `\ +let data = JSON.parse(document.getElementById("__NEXT_DATA__").textContent); +let sections = {}; +for (let h1 of document.getElementsByClassName("cMJCrc")) { + let ul = document.createElement("ul"); + if (h1.nextElementSibling !== null) + h1.parentNode.insertBefore(ul, h1.nextElementSibling); + else + h1.parentNode.appendChild(ul); + + sections[h1.children[1].firstChild.textContent] = ul; +} + +for (let prop of data.props.pageProps.list) { + let ul = sections[prop.category]; + if (ul === undefined) { + console.log(\`unknown category "\${prop.category}" for project "\${prop.title}"\`); + continue; + } + + let li = document.createElement("li"); + let a = document.createElement("a"); + a.setAttribute("href", "/projects/" + prop.slug); + a.textContent = prop.title; + + li.appendChild(a); + ul.appendChild(li); +} +` + }; + + await storage.set(TYPE_PREFIX.SCRIPT, "opencores", opencores_script); + await storage.set(TYPE_PREFIX.PAGE, "https://opencores.org/projects", { + components: [[TYPE_PREFIX.SCRIPT, "opencores"]], + allow: false + }); + } + + browser.runtime.onInstalled.addListener(init_myext); + + console.log("hello, myext"); +})(); diff --git a/background/main.mjs b/background/main.mjs deleted file mode 100644 index ee14c74..0000000 --- a/background/main.mjs +++ /dev/null @@ -1,166 +0,0 @@ -/** -* Myext main background script -* -* Copyright (C) 2021 Wojtek Kosior -* -* Dual-licensed under: -* - 0BSD license -* - GPLv3 or (at your option) any later version -*/ - -import {TYPE_PREFIX} from '/common/stored_types.mjs'; -import get_storage from './storage.mjs'; -import start_storage_server from './storage_server.mjs'; -import start_page_actions_server from './page_actions_server.mjs'; -import start_policy_smuggler from './policy_smuggler.mjs'; -import browser from '/common/browser.mjs'; - -"use strict"; - -start_storage_server(); -start_page_actions_server(); -start_policy_smuggler(); - -async function init_myext(install_details) -{ - console.log("details:", install_details); - if (install_details.reason != "install") - return; - - let storage = await get_storage(); - - await storage.clear(); - - /* - * Below we add sample settings to the extension. - * Those should be considered example values for viewing in the options - * page. They won't make my.fsf.org work. The only scripts that does - * something useful right now is the opencores one. - */ - - let components = []; - for (let script_data of [ - {url: "http://127.0.0.1:8000/myfsf_define_CRM.js", - hash:"bf0cc81c7e8d5f800877b4bc3f14639f946f5ac6d4dc120255ffac5eba5e48fe"}, - {url: "https://my.fsf.org/misc/jquery.js?v=1.4.4", - hash:"261ae472fa0cbf27c80c9200a1599a60fde581a0e652eee4bf41def8cb61f2d0"}, - {url: "https://my.fsf.org/misc/jquery-extend-3.4.0.js?v=1.4.4", - hash:"c54103ba57ee210ca55c052e70415402707548a4e6a68dd6efb3895019bee392"}, - {url: "https://my.fsf.org/misc/jquery-html-prefilter-3.5.0-backport.js?v=1.4.4", - hash:"fad84efa145fb507e5df9b582fa01b1c4e6313de7f72ebdd55726d92fa4dbf06"}, - {url: "https://my.fsf.org/misc/jquery.once.js?v=1.2", - hash:"1430f42c0d760ba8e05bb3762480502e541f654fec5739ee40625ab22dc38c4f"}, - {url: "https://my.fsf.org/misc/drupal.js?qmaukd", - hash:"2e08dccbd4d8b728a6871562995a4636b89bfe0ed3b8fb0138191c922228b116"}, - {url: "https://my.fsf.org/sites/all/modules/civicrm/bower_components/jquery/dist/jquery.min.js?qmaukd", - hash:"a6d01520d28d15dbe476de84eea90eb3ee2d058722efc062ec73cb5fad78a17b"}, - {url: "https://my.fsf.org/sites/all/modules/civicrm/bower_components/jquery-ui/jquery-ui.min.js?qmaukd", - hash:"28ce75d953678c4942df47a11707a15e3c756021cf89090e3e6aa7ad6b6971c3"}, - {url: "https://my.fsf.org/sites/all/modules/civicrm/bower_components/lodash-compat/lodash.min.js?qmaukd", - hash:"f2871cc80c52fe8c04c582c4a49797c9c8fd80391cf1452e47f7fe97835ed5cc"}, - {url: "https://my.fsf.org/sites/all/modules/civicrm/packages/jquery/plugins/jquery.mousewheel.min.js?qmaukd", - hash:"f50233e84c2ac7ada37a094d3f7d3b3f7c97716d6b7b47bf69619d93ee4ac1ce"}, - {url: "https://my.fsf.org/sites/all/modules/civicrm/bower_components/select2/select2.min.js?qmaukd", - hash:"ce61298fb9aa4ec49ccd4172d097e36a9e5db3af06a7b82796659368f15b7c1b"}, - {url: "https://my.fsf.org/sites/all/modules/civi crm/packages/jquery/plugins/jquery.form.min.js?qmaukd", - hash:"c90f0e501d2948fbc2b61bffd654fa4ab64741fd48923782419eeb14d3816fb8"}, - {url: "https://my.fsf.org/sites/all/modules/civicrm/packages/jquery/plugins/jquery.timeentry.min.js?qmaukd", - hash:"8e85df981e8ad7049d06dfb075277d038734d36a7097c7f021021b2bdccfe9bb"}, - {url: "https://my.fsf.org/sites/all/modules/civicrm/packages/jquery/plugins/jquery.blockUI.min.js?qmaukd", - hash:"806aedff52ac822f2adc5797073e1e5c5cec32eb9f15f2319cb32a347dcd232b"}, - {url: "https://my.fsf.org/sites/all/modules/civicrm/bower_components/datatables/media/js/jquery.dataTables.min.js?qmaukd", - hash:"b796504d9b1b422f0dc6ccc2d740ac78a8c9e5078cc3934836d39742b1121925"}, - {url: "https://my.fsf.org/sites/all/modules/civicrm/bower_components/jquery-validation/dist/jquery.validate.min.js?qmaukd", - hash:"f0f5373ad203101ea91bf826c5a7ef8f7cd74887f06bad2cb9277a504503b9e2"}, - {url: "https://my.fsf.org/sites/all/modules/civicrm/packages/jquery/plugins/jquery.ui.datepicker.validation.min.js?qmaukd", - hash:"c6e6f6bf7f8fff25cca338045774e267e8eaa2d48ac9100540f3d59a6d2b3c61"}, - {url: "https://my.fsf.org/sites/all/modules/civicrm/js/Common.js?qmaukd", - hash:"17aa222a3af2e8958be16accb5e77ef39f67009cb3b500718d8fffd45b399148"}, - {url: "https://my.fsf.org/sites/all/modules/civicrm/js/crm.datepicker.js?qmaukd", - hash:"9bd8d10208aa99c156325f7819da6f0dd62ba221ac4119c3ccd4834e2cf36535"}, - {url: "https://my.fsf.org/sites/all/modules/civicrm/js/crm.ajax.js?qmaukd", - hash:"6401a4e257b7499ae4a00be2c200e4504a2c9b3d6b278a830c31a7b63374f0fe"}, - {url: "https://my.fsf.org/sites/all/modules/civicrm/js/wysiwyg/crm.wysiwyg.js?qmaukd", - hash:"fa962356072a36672c3b4b25bdeb657f020995a067e20a29cd5bb84b05157762"}, - {url: "https://my.fsf.org/sites/all/modules/civicrm/js/noconflict.js?qmaukd", - hash:"58d6d9f496a235d23cf891926d71f2104e4f2afe1d14bb4e2b5233f646c35e62"}, - {url: "https://my.fsf.org/sites/all/modules/matomo/matomo.js?qmaukd", - hash:"7f39ccd085f348189cd2fb62ea4d4a658d96f6bba266265880b98605e777e2de"}, - {url: "https://my.fsf.org/sites/all/themes/fsf_venture/js/global.js?qmaukd", - hash:"aa7983f6b902f9f4415cfc8346e0c3f194cc95b78f52f2ad09ec7effa1326b9c"}, - {url: "https://my.fsf.org/sites/all/themes/fsf_venture/js/jquery.superfish.min.js?qmaukd", - hash:"5ef1f93bf3901227056bf9ed0ed93a148eec4dda30f419756b12bedd1098815e"}, - {url: "https://my.fsf.org/sites/all/themes/fsf_venture/js/jquery.sidr.min.js?qmaukd", - hash:"c4914d415826676c6af2e61f16edb72c5388f8600ba6de9049892aee49d980a0"}, - {url: "https://my.fsf.org/sites/all/themes/fsf_venture/js/jquery.flexslider.min.js?qmaukd", - hash:"cefaf715761b4494913851249b9d40dacb4a8cb61242b0efc859dc586d56e0d4"}, - {url: "http://127.0.0.1:8000/myfsf_crap.js", - hash:"d91ccf21592d0f861ea0ba946bc257fc5d88269327cad0a91387da6cb8ff633e"}, - {url: "https://my.fsf.org/sites/all/modules/civicrm/templates/CRM/Core/BillingBlock.js?r=lp7Di", - hash:"2f25d35e7a0c0060ab0a444a577f09dd3c9934ae898a7ee0eb20b6c986ab5a1c"}, - {url: "https://my.fsf.org/extensions/com.aghstrategies.giftmemberships/js/giftpricefield.js?r=lp7Di", - hash:"f86080e6bd306fe46474039aeca2808235005bce5a2a29416d08210022039a45"}, - {url: "https://my.fsf.org/extensions/com.ginkgostreet.negativenegator/js/negativenegator.js?r=lp7Di", - hash:"d0e87bac832856db70947d82a7ab4e0b7c8b1070d5f1a32335345e033ece3a14"} - ]) { - let name_regex = /\/([^/]+)\.js/; - let name = name_regex.exec(script_data.url)[1]; - await storage.set(TYPE_PREFIX.SCRIPT, name, script_data); - components.push([TYPE_PREFIX.SCRIPT, name]); - } - - await storage.set(TYPE_PREFIX.PAGE, "https://my.fsf.org/join", {components}); - - let hello_script = { - text: "console.log(\"hello, every1!\");\n" - }; - await storage.set(TYPE_PREFIX.SCRIPT, "hello", hello_script); - await storage.set(TYPE_PREFIX.BUNDLE, "hello", - [[TYPE_PREFIX.SCRIPT, "hello"]]); - await storage.set(TYPE_PREFIX.PAGE, "https://my.fsf.org/", { - components: [[TYPE_PREFIX.BUNDLE, "hello"]], - allow: true - }); - - let opencores_script = { - text: `\ -let data = JSON.parse(document.getElementById("__NEXT_DATA__").textContent); -let sections = {}; -for (let h1 of document.getElementsByClassName("cMJCrc")) { - let ul = document.createElement("ul"); - if (h1.nextElementSibling !== null) - h1.parentNode.insertBefore(ul, h1.nextElementSibling); - else - h1.parentNode.appendChild(ul); - - sections[h1.children[1].firstChild.textContent] = ul; -} - -for (let prop of data.props.pageProps.list) { - let ul = sections[prop.category]; - if (ul === undefined) { - console.log(\`unknown category "\${prop.category}" for project "\${prop.title}"\`); - continue; - } - - let li = document.createElement("li"); - let a = document.createElement("a"); - a.setAttribute("href", "/projects/" + prop.slug); - a.textContent = prop.title; - - li.appendChild(a); - ul.appendChild(li); -} -` - }; - - await storage.set(TYPE_PREFIX.SCRIPT, "opencores", opencores_script); - await storage.set(TYPE_PREFIX.PAGE, "https://opencores.org/projects", { - components: [[TYPE_PREFIX.SCRIPT, "opencores"]], - allow: false - }); -} - -browser.runtime.onInstalled.addListener(init_myext); - -console.log("hello, myext"); diff --git a/background/message_server.js b/background/message_server.js new file mode 100644 index 0000000..612be0c --- /dev/null +++ b/background/message_server.js @@ -0,0 +1,35 @@ +/** +* Myext message server +* +* Copyright (C) 2021 Wojtek Kosior +* +* Dual-licensed under: +* - 0BSD license +* - GPLv3 or (at your option) any later version +*/ + +"use strict"; + +(() => { + const browser = window.browser; + + var listeners = {}; + + /* magic should be one of the constants from /common/connection_types.js */ + + function listen_for_connection(magic, cb) + { + listeners[magic] = cb; + } + + function raw_listen(port) { + if (listeners[port.name] === undefined) + return; + + listeners[port.name](port); + } + + browser.runtime.onConnect.addListener(raw_listen); + + window.listen_for_connection = listen_for_connection; +})(); diff --git a/background/message_server.mjs b/background/message_server.mjs deleted file mode 100644 index e3f83d0..0000000 --- a/background/message_server.mjs +++ /dev/null @@ -1,31 +0,0 @@ -/** -* Myext message server -* -* Copyright (C) 2021 Wojtek Kosior -* -* Dual-licensed under: -* - 0BSD license -* - GPLv3 or (at your option) any later version -*/ - -"use strict"; - -import browser from '/common/browser.mjs'; - -var listeners = {}; - -/* magic should be one of the constants from /common/connection_types.mjs */ - -export default function listen_for_connection(magic, cb) -{ - listeners[magic] = cb; -} - -function raw_listen(port) { - if (listeners[port.name] === undefined) - return; - - listeners[port.name](port); -} - -browser.runtime.onConnect.addListener(raw_listen); diff --git a/background/page_actions_server.js b/background/page_actions_server.js new file mode 100644 index 0000000..fbb672e --- /dev/null +++ b/background/page_actions_server.js @@ -0,0 +1,149 @@ +/** +* Myext serving of page actions to content scripts +* +* Copyright (C) 2021 Wojtek Kosior +* +* Dual-licensed under: +* - 0BSD license +* - GPLv3 or (at your option) any later version +*/ + +"use strict"; + +(() => { + const get_storage = window.get_storage; + const TYPE_PREFIX = window.TYPE_PREFIX; + const CONNECTION_TYPE = window.CONNECTION_TYPE; + const browser = window.browser; + const listen_for_connection = window.listen_for_connection; + const url_item = window.url_item; + const sha256 = window.sha256; + + var storage; + var handler; + + function send_scripts(url, port) + { + let settings = storage.get(TYPE_PREFIX.PAGE, url_item(url)); + if (settings === undefined) + return; + + let components = settings.components; + let processed_bundles = new Set(); + + send_scripts_rec(components, port, processed_bundles); + } + + // TODO: parallelize script fetching + async function send_scripts_rec(components, port, processed_bundles) + { + for (let [prefix, name] of components) { + if (prefix === TYPE_PREFIX.BUNDLE) { + if (processed_bundles.has(name)) { + console.log(`preventing recursive inclusion of bundle ${name}`); + continue; + } + + var bundle = storage.get(TYPE_PREFIX.BUNDLE, name); + + if (bundle === undefined) { + console.log(`no bundle in storage for key ${name}`); + continue; + } + + processed_bundles.add(name); + await send_scripts_rec(bundle, port, processed_bundles); + processed_bundles.delete(name); + } else { + let script_text = await get_script_text(name); + if (script_text === undefined) + continue; + + port.postMessage({inject : [script_text]}); + } + } + } + + async function get_script_text(script_name) + { + try { + let script_data = storage.get(TYPE_PREFIX.SCRIPT, script_name); + if (script_data === undefined) { + console.log(`missing data for ${script_name}`); + return; + } + let script_text = script_data.text; + if (!script_text) + script_text = await fetch_remote_script(script_data); + return script_text; + } catch (e) { + console.log(e); + } + } + + function ajax_callback() + { + if (this.readyState == 4) + this.resolve_callback(this); + } + + function initiate_ajax_request(resolve, method, url) + { + var xhttp = new XMLHttpRequest(); + xhttp.resolve_callback = resolve; + xhttp.onreadystatechange = ajax_callback; + xhttp.open(method, url, true); + xhttp.send(); + } + + function make_ajax_request(method, url) + { + return new Promise((resolve, reject) => + initiate_ajax_request(resolve, method, url)); + } + + async function fetch_remote_script(script_data) + { + try { + let xhttp = await make_ajax_request("GET", script_data.url); + if (xhttp.status === 200) { + let computed_hash = sha256(xhttp.responseText); + if (computed_hash !== script_data.hash) { + console.log(`Bad hash for ${script_data.url}\n got ${computed_hash} instead of ${script_data.hash}`); + return; + } + return xhttp.responseText; + } else { + console.log("script not fetched: " + script_data.url); + return; + } + } catch (e) { + console.log(e); + } + } + + function handle_message(port, message, handler) + { + port.onMessage.removeListener(handler[0]); + let url = message.url; + console.log({url}); + send_scripts(url, port); + } + + function new_connection(port) + { + console.log("new page actions connection!"); + let handler = []; + handler.push(m => handle_message(port, m, handler)); + port.onMessage.addListener(handler[0]); + } + + async function start() + { + storage = await get_storage(); + + listen_for_connection(CONNECTION_TYPE.PAGE_ACTIONS, new_connection); + } + + window.start_page_actions_server = start; +})(); diff --git a/background/page_actions_server.mjs b/background/page_actions_server.mjs deleted file mode 100644 index 5fb4924..0000000 --- a/background/page_actions_server.mjs +++ /dev/null @@ -1,145 +0,0 @@ -/** -* Myext serving of page actions to content scripts -* -* Copyright (C) 2021 Wojtek Kosior -* -* Dual-licensed under: -* - 0BSD license -* - GPLv3 or (at your option) any later version -*/ - -import get_storage from './storage.mjs'; -import {TYPE_PREFIX} from '/common/stored_types.mjs'; -import CONNECTION_TYPE from '/common/connection_types.mjs'; -import browser from '/common/browser.mjs'; -import listen_for_connection from './message_server.mjs'; -import url_item from './url_item.mjs'; -import sha256 from './sha256.mjs'; - -"use strict"; - -var storage; -var handler; - -function send_scripts(url, port) -{ - let settings = storage.get(TYPE_PREFIX.PAGE, url_item(url)); - if (settings === undefined) - return; - - let components = settings.components; - let processed_bundles = new Set(); - - send_scripts_rec(components, port, processed_bundles); -} - -// TODO: parallelize script fetching -async function send_scripts_rec(components, port, processed_bundles) -{ - for (let [prefix, name] of components) { - if (prefix === TYPE_PREFIX.BUNDLE) { - if (processed_bundles.has(name)) { - console.log(`preventing recursive inclusion of bundle ${name}`); - continue; - } - - var bundle = storage.get(TYPE_PREFIX.BUNDLE, name); - - if (bundle === undefined) { - console.log(`no bundle in storage for key ${name}`); - continue; - } - - processed_bundles.add(name); - await send_scripts_rec(bundle, port, processed_bundles); - processed_bundles.delete(name); - } else { - let script_text = await get_script_text(name); - if (script_text === undefined) - continue; - - port.postMessage({inject : [script_text]}); - } - } -} - -async function get_script_text(script_name) -{ - try { - let script_data = storage.get(TYPE_PREFIX.SCRIPT, script_name); - if (script_data === undefined) { - console.log(`missing data for ${script_name}`); - return; - } - let script_text = script_data.text; - if (!script_text) - script_text = await fetch_remote_script(script_data); - return script_text; - } catch (e) { - console.log(e); - } -} - -function ajax_callback() -{ - if (this.readyState == 4) - this.resolve_callback(this); -} - -function initiate_ajax_request(resolve, method, url) -{ - var xhttp = new XMLHttpRequest(); - xhttp.resolve_callback = resolve; - xhttp.onreadystatechange = ajax_callback; - xhttp.open(method, url, true); - xhttp.send(); -} - -function make_ajax_request(method, url) -{ - return new Promise((resolve, reject) => - initiate_ajax_request(resolve, method, url)); -} - -async function fetch_remote_script(script_data) -{ - try { - let xhttp = await make_ajax_request("GET", script_data.url); - if (xhttp.status === 200) { - let computed_hash = sha256(xhttp.responseText); - if (computed_hash !== script_data.hash) { - console.log(`Bad hash for ${script_data.url}\n got ${computed_hash} instead of ${script_data.hash}`); - return; - } - return xhttp.responseText; - } else { - console.log("script not fetched: " + script_data.url); - return; - } - } catch (e) { - console.log(e); - } -} - -function handle_message(port, message, handler) -{ - port.onMessage.removeListener(handler[0]); - let url = message.url; - console.log({url}); - send_scripts(url, port); -} - -function new_connection(port) -{ - console.log("new page actions connection!"); - let handler = []; - handler.push(m => handle_message(port, m, handler)); - port.onMessage.addListener(handler[0]); -} - -export default async function start() -{ - storage = await get_storage(); - - listen_for_connection(CONNECTION_TYPE.PAGE_ACTIONS, new_connection); -} diff --git a/background/policy_smuggler.js b/background/policy_smuggler.js new file mode 100644 index 0000000..6d0da38 --- /dev/null +++ b/background/policy_smuggler.js @@ -0,0 +1,64 @@ +/** +* Myext smuggling policy to content script through url +* +* Copyright (C) 2021 Wojtek Kosior +* +* Dual-licensed under: +* - 0BSD license +* - GPLv3 or (at your option) any later version +*/ + +"use strict"; + +(() => { + const TYPE_PREFIX = window.TYPE_PREFIX; + const get_storage = window.get_storage; + const browser = window.browser; + const url_item = window.url_item; + + var storage; + + function redirect(request) + { + let url_re = /^([^#]*)((#[^#]*)(#.*)?)?$/; + let match = url_re.exec(request.url); + let base_url = match[1]; + let first_target = match[3]; + let second_target = match[4]; + + if (first_target === "#myext-allow") { + console.log(["not redirecting"]); + return {cancel : false}; + } + + let url = url_item(request.url); + let settings = storage.get(TYPE_PREFIX.PAGE, url); + console.log("got", storage.get(TYPE_PREFIX.PAGE, url), "for", url); + if (settings === undefined || !settings.allow) + return {cancel : false}; + + second_target = (first_target || "") + (second_target || "") + + console.log(["redirecting", request.url, + (base_url + "#myext-allow" + second_target)]); + + return { + redirectUrl : (base_url + "#myext-allow" + second_target) + }; + } + + async function start() { + storage = await get_storage(); + + chrome.webRequest.onBeforeRequest.addListener( + redirect, + { + urls: [""], + types: ["main_frame", "sub_frame"] + }, + ["blocking"] + ); + } + + window.start_policy_smuggler = start; +})(); diff --git a/background/policy_smuggler.mjs b/background/policy_smuggler.mjs deleted file mode 100644 index 61f34c5..0000000 --- a/background/policy_smuggler.mjs +++ /dev/null @@ -1,60 +0,0 @@ -/** -* Myext smuggling policy to content script through url -* -* Copyright (C) 2021 Wojtek Kosior -* -* Dual-licensed under: -* - 0BSD license -* - GPLv3 or (at your option) any later version -*/ - -import {TYPE_PREFIX} from '/common/stored_types.mjs'; -import get_storage from './storage.mjs'; -import browser from '/common/browser.mjs'; -import url_item from './url_item.mjs'; - -"use strict"; - -var storage; - -function redirect(request) -{ - let url_re = /^([^#]*)((#[^#]*)(#.*)?)?$/; - let match = url_re.exec(request.url); - let base_url = match[1]; - let first_target = match[3]; - let second_target = match[4]; - - if (first_target === "#myext-allow") { - console.log(["not redirecting"]); - return {cancel : false}; - } - - let url = url_item(request.url); - let settings = storage.get(TYPE_PREFIX.PAGE, url); - console.log("got", storage.get(TYPE_PREFIX.PAGE, url), "for", url); - if (settings === undefined || !settings.allow) - return {cancel : false}; - - second_target = (first_target || "") + (second_target || "") - - console.log(["redirecting", request.url, - (base_url + "#myext-allow" + second_target)]); - - return { - redirectUrl : (base_url + "#myext-allow" + second_target) - }; -} - -export default async function start() { - storage = await get_storage(); - - chrome.webRequest.onBeforeRequest.addListener( - redirect, - { - urls: [""], - types: ["main_frame", "sub_frame"] - }, - ["blocking"] - ); -} diff --git a/background/reverse_use_info.js b/background/reverse_use_info.js new file mode 100644 index 0000000..3399285 --- /dev/null +++ b/background/reverse_use_info.js @@ -0,0 +1,96 @@ +/** +* Myext scripts and bundles usage index +* +* Copyright (C) 2021 Wojtek Kosior +* +* Dual-licensed under: +* - 0BSD license +* - GPLv3 or (at your option) any later version +*/ + +"use strict"; + +/* + * We want to count referenes to scripts and bundles in order to know, + * for example, whether one can be safely deleted. + */ + +(() => { + const TYPE_PREFIX = window.TYPE_PREFIX; + const get_storage = window.get_storage; + const make_once = window.make_once; + + var component_uses = new Map(); + var storage; + + function add_use_info_by_item(prefix, item, components) + { + for (let component of components) { + component = component.join(""); + + let used_by_info = component_uses.get(component); + + if (used_by_info === undefined) { + used_by_info = {}; + component_uses.set(component, used_by_info); + } + + if (used_by_info[prefix] === undefined) + used_by_info[prefix] = new Set(); + + used_by_info[prefix].add(item); + } + } + + function remove_use_info_by_item(prefix, item, components) + { + for (let component of components) { + used_by_info = component_uses.get(component.join("")); + if (used_by_info === undefined || + used_by_info[prefix] === undefined) + return; + + used_by_info[prefix].delete(item); + } + } + + function build_reverse_uses_info(entries_it, type_prefix) + { + for (let [item, components] of storage.get_all_it(type_prefix)) + add_use_info_by_item(type_prefix, item, components); + } + + function handle_change(change) + { + if (change.old_val !== undefined) + remove_use_info_by_item(change.prefix, change.item, change.old_val); + + if (change.new_val !== undefined) + add_use_info_by_item(change.prefix, change.item, change.new_val); + } + + function get_uses(arg1, arg2=undefined) + { + let [prefix, item] = [arg1, arg2]; + + if (arg2 === undefined) + [prefix, item] = arg1; + + return component_uses.get(prefix + item); + } + + async function init() + { + storage = await get_storage(); + + prefixes = [TYPE_PREFIX.PAGE, TYPE_PREFIX.BUNDLE]; + for (let prefix of prefixes) + build_reverse_uses_info(prefix); + + storage.add_change_listener(handle_change, prefixes); + + return get_uses; + } + + window.get_reverse_use_info = make_once(init); +})(); diff --git a/background/reverse_use_info.mjs b/background/reverse_use_info.mjs deleted file mode 100644 index c38c52f..0000000 --- a/background/reverse_use_info.mjs +++ /dev/null @@ -1,93 +0,0 @@ -/** -* Myext scripts and bundles usage index -* -* Copyright (C) 2021 Wojtek Kosior -* -* Dual-licensed under: -* - 0BSD license -* - GPLv3 or (at your option) any later version -*/ - -import TYPE_PREFIX from '/common/stored_types.mjs'; -import get_storage from './storage.mjs'; -import make_once from '/common/once.mjs'; - -"use strict"; - -/* - * We want to count referenes to scripts and bundles in order to know, - * for example, whether one can be safely deleted. - */ - -var component_uses = new Map(); -var storage; - -function add_use_info_by_item(prefix, item, components) -{ - for (let component of components) { - component = component.join(""); - - let used_by_info = component_uses.get(component); - - if (used_by_info === undefined) { - used_by_info = {}; - component_uses.set(component, used_by_info); - } - - if (used_by_info[prefix] === undefined) - used_by_info[prefix] = new Set(); - - used_by_info[prefix].add(item); - } -} - -function remove_use_info_by_item(prefix, item, components) -{ - for (let component of components) { - used_by_info = component_uses.get(component.join("")); - if (used_by_info === undefined || used_by_info[prefix] === undefined) - return; - - used_by_info[prefix].delete(item); - } -} - -function build_reverse_uses_info(entries_it, type_prefix) -{ - for (let [item, components] of storage.get_all_it(type_prefix)) - add_use_info_by_item(type_prefix, item, components); -} - -function handle_change(change) -{ - if (change.old_val !== undefined) - remove_use_info_by_item(change.prefix, change.item, change.old_val); - - if (change.new_val !== undefined) - add_use_info_by_item(change.prefix, change.item, change.new_val); -} - -function get_uses(arg1, arg2=undefined) -{ - let [prefix, item] = [arg1, arg2]; - - if (arg2 === undefined) - [prefix, item] = arg1; - - return component_uses.get(prefix + item); -} - -async function init() -{ - storage = await get_storage(); - - prefixes = [TYPE_PREFIX.PAGE, TYPE_PREFIX.BUNDLE]; - for (let prefix of prefixes) - build_reverse_uses_info(prefix); - - storage.add_change_listener(handle_change, prefixes); - - return get_uses; -} - -export default make_once(init); diff --git a/background/sha256.js b/background/sha256.js new file mode 100644 index 0000000..271ec87 --- /dev/null +++ b/background/sha256.js @@ -0,0 +1,523 @@ +/** + * [js-sha256]{@link https://github.com/emn178/js-sha256/} + * + * @version 0.9.0 + * @author Chen, Yi-Cyuan [emn178@gmail.com] + * @copyright Chen, Yi-Cyuan 2014-2017 + * @license MIT + */ + +"use strict"; + +(() => { + var fake_window = {}; + + console.log('hello, crypto!'); + var ERROR = 'input is invalid type'; + var WINDOW = typeof window === 'object'; + var root = /*WINDOW ? window : {}*/ fake_window; + if (root.JS_SHA256_NO_WINDOW) { + WINDOW = false; + } + var WEB_WORKER = !WINDOW && typeof self === 'object'; + var NODE_JS = !root.JS_SHA256_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node; + if (NODE_JS) { + root = global; + } else if (WEB_WORKER) { + root = self; + } + var COMMON_JS = !root.JS_SHA256_NO_COMMON_JS && typeof module === 'object' && module.exports; + var AMD = typeof define === 'function' && define.amd; + var ARRAY_BUFFER = !root.JS_SHA256_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined'; + var HEX_CHARS = '0123456789abcdef'.split(''); + var EXTRA = [-2147483648, 8388608, 32768, 128]; + var SHIFT = [24, 16, 8, 0]; + var K = [ + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 + ]; + var OUTPUT_TYPES = ['hex', 'array', 'digest', 'arrayBuffer']; + + var blocks = []; + + if (root.JS_SHA256_NO_NODE_JS || !Array.isArray) { + Array.isArray = function (obj) { + return Object.prototype.toString.call(obj) === '[object Array]'; + }; + } + + if (ARRAY_BUFFER && (root.JS_SHA256_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)) { + ArrayBuffer.isView = function (obj) { + return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer; + }; + } + + var createOutputMethod = function (outputType, is224) { + return function (message) { + return new Sha256(is224, true).update(message)[outputType](); + }; + }; + + var createMethod = function (is224) { + var method = createOutputMethod('hex', is224); + if (NODE_JS) { + method = nodeWrap(method, is224); + } + method.create = function () { + return new Sha256(is224); + }; + method.update = function (message) { + return method.create().update(message); + }; + for (var i = 0; i < OUTPUT_TYPES.length; ++i) { + var type = OUTPUT_TYPES[i]; + method[type] = createOutputMethod(type, is224); + } + return method; + }; + + var nodeWrap = function (method, is224) { + var crypto = eval("require('crypto')"); + var Buffer = eval("require('buffer').Buffer"); + var algorithm = is224 ? 'sha224' : 'sha256'; + var nodeMethod = function (message) { + if (typeof message === 'string') { + return crypto.createHash(algorithm).update(message, 'utf8').digest('hex'); + } else { + if (message === null || message === undefined) { + throw new Error(ERROR); + } else if (message.constructor === ArrayBuffer) { + message = new Uint8Array(message); + } + } + if (Array.isArray(message) || ArrayBuffer.isView(message) || + message.constructor === Buffer) { + return crypto.createHash(algorithm).update(new Buffer(message)).digest('hex'); + } else { + return method(message); + } + }; + return nodeMethod; + }; + + var createHmacOutputMethod = function (outputType, is224) { + return function (key, message) { + return new HmacSha256(key, is224, true).update(message)[outputType](); + }; + }; + + var createHmacMethod = function (is224) { + var method = createHmacOutputMethod('hex', is224); + method.create = function (key) { + return new HmacSha256(key, is224); + }; + method.update = function (key, message) { + return method.create(key).update(message); + }; + for (var i = 0; i < OUTPUT_TYPES.length; ++i) { + var type = OUTPUT_TYPES[i]; + method[type] = createHmacOutputMethod(type, is224); + } + return method; + }; + + function Sha256(is224, sharedMemory) { + if (sharedMemory) { + blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] = + blocks[4] = blocks[5] = blocks[6] = blocks[7] = + blocks[8] = blocks[9] = blocks[10] = blocks[11] = + blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; + this.blocks = blocks; + } else { + this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + } + + if (is224) { + this.h0 = 0xc1059ed8; + this.h1 = 0x367cd507; + this.h2 = 0x3070dd17; + this.h3 = 0xf70e5939; + this.h4 = 0xffc00b31; + this.h5 = 0x68581511; + this.h6 = 0x64f98fa7; + this.h7 = 0xbefa4fa4; + } else { // 256 + this.h0 = 0x6a09e667; + this.h1 = 0xbb67ae85; + this.h2 = 0x3c6ef372; + this.h3 = 0xa54ff53a; + this.h4 = 0x510e527f; + this.h5 = 0x9b05688c; + this.h6 = 0x1f83d9ab; + this.h7 = 0x5be0cd19; + } + + this.block = this.start = this.bytes = this.hBytes = 0; + this.finalized = this.hashed = false; + this.first = true; + this.is224 = is224; + } + + Sha256.prototype.update = function (message) { + if (this.finalized) { + return; + } + var notString, type = typeof message; + if (type !== 'string') { + if (type === 'object') { + if (message === null) { + throw new Error(ERROR); + } else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) { + message = new Uint8Array(message); + } else if (!Array.isArray(message)) { + if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) { + throw new Error(ERROR); + } + } + } else { + throw new Error(ERROR); + } + notString = true; + } + var code, index = 0, i, length = message.length, blocks = this.blocks; + + while (index < length) { + if (this.hashed) { + this.hashed = false; + blocks[0] = this.block; + blocks[16] = blocks[1] = blocks[2] = blocks[3] = + blocks[4] = blocks[5] = blocks[6] = blocks[7] = + blocks[8] = blocks[9] = blocks[10] = blocks[11] = + blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; + } + + if (notString) { + for (i = this.start; index < length && i < 64; ++index) { + blocks[i >> 2] |= message[index] << SHIFT[i++ & 3]; + } + } else { + for (i = this.start; index < length && i < 64; ++index) { + code = message.charCodeAt(index); + if (code < 0x80) { + blocks[i >> 2] |= code << SHIFT[i++ & 3]; + } else if (code < 0x800) { + blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; + } else if (code < 0xd800 || code >= 0xe000) { + blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; + } else { + code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff)); + blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; + } + } + } + + this.lastByteIndex = i; + this.bytes += i - this.start; + if (i >= 64) { + this.block = blocks[16]; + this.start = i - 64; + this.hash(); + this.hashed = true; + } else { + this.start = i; + } + } + if (this.bytes > 4294967295) { + this.hBytes += this.bytes / 4294967296 << 0; + this.bytes = this.bytes % 4294967296; + } + return this; + }; + + Sha256.prototype.finalize = function () { + if (this.finalized) { + return; + } + this.finalized = true; + var blocks = this.blocks, i = this.lastByteIndex; + blocks[16] = this.block; + blocks[i >> 2] |= EXTRA[i & 3]; + this.block = blocks[16]; + if (i >= 56) { + if (!this.hashed) { + this.hash(); + } + blocks[0] = this.block; + blocks[16] = blocks[1] = blocks[2] = blocks[3] = + blocks[4] = blocks[5] = blocks[6] = blocks[7] = + blocks[8] = blocks[9] = blocks[10] = blocks[11] = + blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; + } + blocks[14] = this.hBytes << 3 | this.bytes >>> 29; + blocks[15] = this.bytes << 3; + this.hash(); + }; + + Sha256.prototype.hash = function () { + var a = this.h0, b = this.h1, c = this.h2, d = this.h3, e = this.h4, f = this.h5, g = this.h6, + h = this.h7, blocks = this.blocks, j, s0, s1, maj, t1, t2, ch, ab, da, cd, bc; + + for (j = 16; j < 64; ++j) { + // rightrotate + t1 = blocks[j - 15]; + s0 = ((t1 >>> 7) | (t1 << 25)) ^ ((t1 >>> 18) | (t1 << 14)) ^ (t1 >>> 3); + t1 = blocks[j - 2]; + s1 = ((t1 >>> 17) | (t1 << 15)) ^ ((t1 >>> 19) | (t1 << 13)) ^ (t1 >>> 10); + blocks[j] = blocks[j - 16] + s0 + blocks[j - 7] + s1 << 0; + } + + bc = b & c; + for (j = 0; j < 64; j += 4) { + if (this.first) { + if (this.is224) { + ab = 300032; + t1 = blocks[0] - 1413257819; + h = t1 - 150054599 << 0; + d = t1 + 24177077 << 0; + } else { + ab = 704751109; + t1 = blocks[0] - 210244248; + h = t1 - 1521486534 << 0; + d = t1 + 143694565 << 0; + } + this.first = false; + } else { + s0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10)); + s1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7)); + ab = a & b; + maj = ab ^ (a & c) ^ bc; + ch = (e & f) ^ (~e & g); + t1 = h + s1 + ch + K[j] + blocks[j]; + t2 = s0 + maj; + h = d + t1 << 0; + d = t1 + t2 << 0; + } + s0 = ((d >>> 2) | (d << 30)) ^ ((d >>> 13) | (d << 19)) ^ ((d >>> 22) | (d << 10)); + s1 = ((h >>> 6) | (h << 26)) ^ ((h >>> 11) | (h << 21)) ^ ((h >>> 25) | (h << 7)); + da = d & a; + maj = da ^ (d & b) ^ ab; + ch = (h & e) ^ (~h & f); + t1 = g + s1 + ch + K[j + 1] + blocks[j + 1]; + t2 = s0 + maj; + g = c + t1 << 0; + c = t1 + t2 << 0; + s0 = ((c >>> 2) | (c << 30)) ^ ((c >>> 13) | (c << 19)) ^ ((c >>> 22) | (c << 10)); + s1 = ((g >>> 6) | (g << 26)) ^ ((g >>> 11) | (g << 21)) ^ ((g >>> 25) | (g << 7)); + cd = c & d; + maj = cd ^ (c & a) ^ da; + ch = (g & h) ^ (~g & e); + t1 = f + s1 + ch + K[j + 2] + blocks[j + 2]; + t2 = s0 + maj; + f = b + t1 << 0; + b = t1 + t2 << 0; + s0 = ((b >>> 2) | (b << 30)) ^ ((b >>> 13) | (b << 19)) ^ ((b >>> 22) | (b << 10)); + s1 = ((f >>> 6) | (f << 26)) ^ ((f >>> 11) | (f << 21)) ^ ((f >>> 25) | (f << 7)); + bc = b & c; + maj = bc ^ (b & d) ^ cd; + ch = (f & g) ^ (~f & h); + t1 = e + s1 + ch + K[j + 3] + blocks[j + 3]; + t2 = s0 + maj; + e = a + t1 << 0; + a = t1 + t2 << 0; + } + + this.h0 = this.h0 + a << 0; + this.h1 = this.h1 + b << 0; + this.h2 = this.h2 + c << 0; + this.h3 = this.h3 + d << 0; + this.h4 = this.h4 + e << 0; + this.h5 = this.h5 + f << 0; + this.h6 = this.h6 + g << 0; + this.h7 = this.h7 + h << 0; + }; + + Sha256.prototype.hex = function () { + this.finalize(); + + var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4, h5 = this.h5, + h6 = this.h6, h7 = this.h7; + + var hex = HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] + + HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] + + HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] + + HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] + + HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] + + HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] + + HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] + + HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] + + HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] + + HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] + + HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] + + HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] + + HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F] + + HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] + + HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] + + HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] + + HEX_CHARS[(h4 >> 28) & 0x0F] + HEX_CHARS[(h4 >> 24) & 0x0F] + + HEX_CHARS[(h4 >> 20) & 0x0F] + HEX_CHARS[(h4 >> 16) & 0x0F] + + HEX_CHARS[(h4 >> 12) & 0x0F] + HEX_CHARS[(h4 >> 8) & 0x0F] + + HEX_CHARS[(h4 >> 4) & 0x0F] + HEX_CHARS[h4 & 0x0F] + + HEX_CHARS[(h5 >> 28) & 0x0F] + HEX_CHARS[(h5 >> 24) & 0x0F] + + HEX_CHARS[(h5 >> 20) & 0x0F] + HEX_CHARS[(h5 >> 16) & 0x0F] + + HEX_CHARS[(h5 >> 12) & 0x0F] + HEX_CHARS[(h5 >> 8) & 0x0F] + + HEX_CHARS[(h5 >> 4) & 0x0F] + HEX_CHARS[h5 & 0x0F] + + HEX_CHARS[(h6 >> 28) & 0x0F] + HEX_CHARS[(h6 >> 24) & 0x0F] + + HEX_CHARS[(h6 >> 20) & 0x0F] + HEX_CHARS[(h6 >> 16) & 0x0F] + + HEX_CHARS[(h6 >> 12) & 0x0F] + HEX_CHARS[(h6 >> 8) & 0x0F] + + HEX_CHARS[(h6 >> 4) & 0x0F] + HEX_CHARS[h6 & 0x0F]; + if (!this.is224) { + hex += HEX_CHARS[(h7 >> 28) & 0x0F] + HEX_CHARS[(h7 >> 24) & 0x0F] + + HEX_CHARS[(h7 >> 20) & 0x0F] + HEX_CHARS[(h7 >> 16) & 0x0F] + + HEX_CHARS[(h7 >> 12) & 0x0F] + HEX_CHARS[(h7 >> 8) & 0x0F] + + HEX_CHARS[(h7 >> 4) & 0x0F] + HEX_CHARS[h7 & 0x0F]; + } + return hex; + }; + + Sha256.prototype.toString = Sha256.prototype.hex; + + Sha256.prototype.digest = function () { + this.finalize(); + + var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4, h5 = this.h5, + h6 = this.h6, h7 = this.h7; + + var arr = [ + (h0 >> 24) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 8) & 0xFF, h0 & 0xFF, + (h1 >> 24) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 8) & 0xFF, h1 & 0xFF, + (h2 >> 24) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 8) & 0xFF, h2 & 0xFF, + (h3 >> 24) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 8) & 0xFF, h3 & 0xFF, + (h4 >> 24) & 0xFF, (h4 >> 16) & 0xFF, (h4 >> 8) & 0xFF, h4 & 0xFF, + (h5 >> 24) & 0xFF, (h5 >> 16) & 0xFF, (h5 >> 8) & 0xFF, h5 & 0xFF, + (h6 >> 24) & 0xFF, (h6 >> 16) & 0xFF, (h6 >> 8) & 0xFF, h6 & 0xFF + ]; + if (!this.is224) { + arr.push((h7 >> 24) & 0xFF, (h7 >> 16) & 0xFF, (h7 >> 8) & 0xFF, h7 & 0xFF); + } + return arr; + }; + + Sha256.prototype.array = Sha256.prototype.digest; + + Sha256.prototype.arrayBuffer = function () { + this.finalize(); + + var buffer = new ArrayBuffer(this.is224 ? 28 : 32); + var dataView = new DataView(buffer); + dataView.setUint32(0, this.h0); + dataView.setUint32(4, this.h1); + dataView.setUint32(8, this.h2); + dataView.setUint32(12, this.h3); + dataView.setUint32(16, this.h4); + dataView.setUint32(20, this.h5); + dataView.setUint32(24, this.h6); + if (!this.is224) { + dataView.setUint32(28, this.h7); + } + return buffer; + }; + + function HmacSha256(key, is224, sharedMemory) { + var i, type = typeof key; + if (type === 'string') { + var bytes = [], length = key.length, index = 0, code; + for (i = 0; i < length; ++i) { + code = key.charCodeAt(i); + if (code < 0x80) { + bytes[index++] = code; + } else if (code < 0x800) { + bytes[index++] = (0xc0 | (code >> 6)); + bytes[index++] = (0x80 | (code & 0x3f)); + } else if (code < 0xd800 || code >= 0xe000) { + bytes[index++] = (0xe0 | (code >> 12)); + bytes[index++] = (0x80 | ((code >> 6) & 0x3f)); + bytes[index++] = (0x80 | (code & 0x3f)); + } else { + code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff)); + bytes[index++] = (0xf0 | (code >> 18)); + bytes[index++] = (0x80 | ((code >> 12) & 0x3f)); + bytes[index++] = (0x80 | ((code >> 6) & 0x3f)); + bytes[index++] = (0x80 | (code & 0x3f)); + } + } + key = bytes; + } else { + if (type === 'object') { + if (key === null) { + throw new Error(ERROR); + } else if (ARRAY_BUFFER && key.constructor === ArrayBuffer) { + key = new Uint8Array(key); + } else if (!Array.isArray(key)) { + if (!ARRAY_BUFFER || !ArrayBuffer.isView(key)) { + throw new Error(ERROR); + } + } + } else { + throw new Error(ERROR); + } + } + + if (key.length > 64) { + key = (new Sha256(is224, true)).update(key).array(); + } + + var oKeyPad = [], iKeyPad = []; + for (i = 0; i < 64; ++i) { + var b = key[i] || 0; + oKeyPad[i] = 0x5c ^ b; + iKeyPad[i] = 0x36 ^ b; + } + + Sha256.call(this, is224, sharedMemory); + + this.update(iKeyPad); + this.oKeyPad = oKeyPad; + this.inner = true; + this.sharedMemory = sharedMemory; + } + HmacSha256.prototype = new Sha256(); + + HmacSha256.prototype.finalize = function () { + Sha256.prototype.finalize.call(this); + if (this.inner) { + this.inner = false; + var innerHash = this.array(); + Sha256.call(this, this.is224, this.sharedMemory); + this.update(this.oKeyPad); + this.update(innerHash); + Sha256.prototype.finalize.call(this); + } + }; + + var exports = createMethod(); + exports.sha256 = exports; + exports.sha224 = createMethod(true); + exports.sha256.hmac = createHmacMethod(); + exports.sha224.hmac = createHmacMethod(true); + + if (COMMON_JS) { + module.exports = exports; + } else { + root.sha256 = exports.sha256; + root.sha224 = exports.sha224; + if (AMD) { + define(function () { + return exports; + }); + } + } + + window.sha256 = fake_window.sha256; +})(); diff --git a/background/sha256.mjs b/background/sha256.mjs deleted file mode 100644 index b8d9bda..0000000 --- a/background/sha256.mjs +++ /dev/null @@ -1,524 +0,0 @@ -/** - * [js-sha256]{@link https://github.com/emn178/js-sha256/} - * - * @version 0.9.0 - * @author Chen, Yi-Cyuan [emn178@gmail.com] - * @copyright Chen, Yi-Cyuan 2014-2017 - * @license MIT - */ - -var fake_window = {}; - -/*jslint bitwise: true */ -(function () { - 'use strict'; - - console.log('hello, crypto!'); - var ERROR = 'input is invalid type'; - var WINDOW = typeof window === 'object'; - var root = /*WINDOW ? window : {}*/ fake_window; - if (root.JS_SHA256_NO_WINDOW) { - WINDOW = false; - } - var WEB_WORKER = !WINDOW && typeof self === 'object'; - var NODE_JS = !root.JS_SHA256_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node; - if (NODE_JS) { - root = global; - } else if (WEB_WORKER) { - root = self; - } - var COMMON_JS = !root.JS_SHA256_NO_COMMON_JS && typeof module === 'object' && module.exports; - var AMD = typeof define === 'function' && define.amd; - var ARRAY_BUFFER = !root.JS_SHA256_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined'; - var HEX_CHARS = '0123456789abcdef'.split(''); - var EXTRA = [-2147483648, 8388608, 32768, 128]; - var SHIFT = [24, 16, 8, 0]; - var K = [ - 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, - 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, - 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, - 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, - 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, - 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, - 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, - 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 - ]; - var OUTPUT_TYPES = ['hex', 'array', 'digest', 'arrayBuffer']; - - var blocks = []; - - if (root.JS_SHA256_NO_NODE_JS || !Array.isArray) { - Array.isArray = function (obj) { - return Object.prototype.toString.call(obj) === '[object Array]'; - }; - } - - if (ARRAY_BUFFER && (root.JS_SHA256_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)) { - ArrayBuffer.isView = function (obj) { - return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer; - }; - } - - var createOutputMethod = function (outputType, is224) { - return function (message) { - return new Sha256(is224, true).update(message)[outputType](); - }; - }; - - var createMethod = function (is224) { - var method = createOutputMethod('hex', is224); - if (NODE_JS) { - method = nodeWrap(method, is224); - } - method.create = function () { - return new Sha256(is224); - }; - method.update = function (message) { - return method.create().update(message); - }; - for (var i = 0; i < OUTPUT_TYPES.length; ++i) { - var type = OUTPUT_TYPES[i]; - method[type] = createOutputMethod(type, is224); - } - return method; - }; - - var nodeWrap = function (method, is224) { - var crypto = eval("require('crypto')"); - var Buffer = eval("require('buffer').Buffer"); - var algorithm = is224 ? 'sha224' : 'sha256'; - var nodeMethod = function (message) { - if (typeof message === 'string') { - return crypto.createHash(algorithm).update(message, 'utf8').digest('hex'); - } else { - if (message === null || message === undefined) { - throw new Error(ERROR); - } else if (message.constructor === ArrayBuffer) { - message = new Uint8Array(message); - } - } - if (Array.isArray(message) || ArrayBuffer.isView(message) || - message.constructor === Buffer) { - return crypto.createHash(algorithm).update(new Buffer(message)).digest('hex'); - } else { - return method(message); - } - }; - return nodeMethod; - }; - - var createHmacOutputMethod = function (outputType, is224) { - return function (key, message) { - return new HmacSha256(key, is224, true).update(message)[outputType](); - }; - }; - - var createHmacMethod = function (is224) { - var method = createHmacOutputMethod('hex', is224); - method.create = function (key) { - return new HmacSha256(key, is224); - }; - method.update = function (key, message) { - return method.create(key).update(message); - }; - for (var i = 0; i < OUTPUT_TYPES.length; ++i) { - var type = OUTPUT_TYPES[i]; - method[type] = createHmacOutputMethod(type, is224); - } - return method; - }; - - function Sha256(is224, sharedMemory) { - if (sharedMemory) { - blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] = - blocks[4] = blocks[5] = blocks[6] = blocks[7] = - blocks[8] = blocks[9] = blocks[10] = blocks[11] = - blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; - this.blocks = blocks; - } else { - this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; - } - - if (is224) { - this.h0 = 0xc1059ed8; - this.h1 = 0x367cd507; - this.h2 = 0x3070dd17; - this.h3 = 0xf70e5939; - this.h4 = 0xffc00b31; - this.h5 = 0x68581511; - this.h6 = 0x64f98fa7; - this.h7 = 0xbefa4fa4; - } else { // 256 - this.h0 = 0x6a09e667; - this.h1 = 0xbb67ae85; - this.h2 = 0x3c6ef372; - this.h3 = 0xa54ff53a; - this.h4 = 0x510e527f; - this.h5 = 0x9b05688c; - this.h6 = 0x1f83d9ab; - this.h7 = 0x5be0cd19; - } - - this.block = this.start = this.bytes = this.hBytes = 0; - this.finalized = this.hashed = false; - this.first = true; - this.is224 = is224; - } - - Sha256.prototype.update = function (message) { - if (this.finalized) { - return; - } - var notString, type = typeof message; - if (type !== 'string') { - if (type === 'object') { - if (message === null) { - throw new Error(ERROR); - } else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) { - message = new Uint8Array(message); - } else if (!Array.isArray(message)) { - if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) { - throw new Error(ERROR); - } - } - } else { - throw new Error(ERROR); - } - notString = true; - } - var code, index = 0, i, length = message.length, blocks = this.blocks; - - while (index < length) { - if (this.hashed) { - this.hashed = false; - blocks[0] = this.block; - blocks[16] = blocks[1] = blocks[2] = blocks[3] = - blocks[4] = blocks[5] = blocks[6] = blocks[7] = - blocks[8] = blocks[9] = blocks[10] = blocks[11] = - blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; - } - - if (notString) { - for (i = this.start; index < length && i < 64; ++index) { - blocks[i >> 2] |= message[index] << SHIFT[i++ & 3]; - } - } else { - for (i = this.start; index < length && i < 64; ++index) { - code = message.charCodeAt(index); - if (code < 0x80) { - blocks[i >> 2] |= code << SHIFT[i++ & 3]; - } else if (code < 0x800) { - blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; - } else if (code < 0xd800 || code >= 0xe000) { - blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; - } else { - code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff)); - blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; - } - } - } - - this.lastByteIndex = i; - this.bytes += i - this.start; - if (i >= 64) { - this.block = blocks[16]; - this.start = i - 64; - this.hash(); - this.hashed = true; - } else { - this.start = i; - } - } - if (this.bytes > 4294967295) { - this.hBytes += this.bytes / 4294967296 << 0; - this.bytes = this.bytes % 4294967296; - } - return this; - }; - - Sha256.prototype.finalize = function () { - if (this.finalized) { - return; - } - this.finalized = true; - var blocks = this.blocks, i = this.lastByteIndex; - blocks[16] = this.block; - blocks[i >> 2] |= EXTRA[i & 3]; - this.block = blocks[16]; - if (i >= 56) { - if (!this.hashed) { - this.hash(); - } - blocks[0] = this.block; - blocks[16] = blocks[1] = blocks[2] = blocks[3] = - blocks[4] = blocks[5] = blocks[6] = blocks[7] = - blocks[8] = blocks[9] = blocks[10] = blocks[11] = - blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; - } - blocks[14] = this.hBytes << 3 | this.bytes >>> 29; - blocks[15] = this.bytes << 3; - this.hash(); - }; - - Sha256.prototype.hash = function () { - var a = this.h0, b = this.h1, c = this.h2, d = this.h3, e = this.h4, f = this.h5, g = this.h6, - h = this.h7, blocks = this.blocks, j, s0, s1, maj, t1, t2, ch, ab, da, cd, bc; - - for (j = 16; j < 64; ++j) { - // rightrotate - t1 = blocks[j - 15]; - s0 = ((t1 >>> 7) | (t1 << 25)) ^ ((t1 >>> 18) | (t1 << 14)) ^ (t1 >>> 3); - t1 = blocks[j - 2]; - s1 = ((t1 >>> 17) | (t1 << 15)) ^ ((t1 >>> 19) | (t1 << 13)) ^ (t1 >>> 10); - blocks[j] = blocks[j - 16] + s0 + blocks[j - 7] + s1 << 0; - } - - bc = b & c; - for (j = 0; j < 64; j += 4) { - if (this.first) { - if (this.is224) { - ab = 300032; - t1 = blocks[0] - 1413257819; - h = t1 - 150054599 << 0; - d = t1 + 24177077 << 0; - } else { - ab = 704751109; - t1 = blocks[0] - 210244248; - h = t1 - 1521486534 << 0; - d = t1 + 143694565 << 0; - } - this.first = false; - } else { - s0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10)); - s1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7)); - ab = a & b; - maj = ab ^ (a & c) ^ bc; - ch = (e & f) ^ (~e & g); - t1 = h + s1 + ch + K[j] + blocks[j]; - t2 = s0 + maj; - h = d + t1 << 0; - d = t1 + t2 << 0; - } - s0 = ((d >>> 2) | (d << 30)) ^ ((d >>> 13) | (d << 19)) ^ ((d >>> 22) | (d << 10)); - s1 = ((h >>> 6) | (h << 26)) ^ ((h >>> 11) | (h << 21)) ^ ((h >>> 25) | (h << 7)); - da = d & a; - maj = da ^ (d & b) ^ ab; - ch = (h & e) ^ (~h & f); - t1 = g + s1 + ch + K[j + 1] + blocks[j + 1]; - t2 = s0 + maj; - g = c + t1 << 0; - c = t1 + t2 << 0; - s0 = ((c >>> 2) | (c << 30)) ^ ((c >>> 13) | (c << 19)) ^ ((c >>> 22) | (c << 10)); - s1 = ((g >>> 6) | (g << 26)) ^ ((g >>> 11) | (g << 21)) ^ ((g >>> 25) | (g << 7)); - cd = c & d; - maj = cd ^ (c & a) ^ da; - ch = (g & h) ^ (~g & e); - t1 = f + s1 + ch + K[j + 2] + blocks[j + 2]; - t2 = s0 + maj; - f = b + t1 << 0; - b = t1 + t2 << 0; - s0 = ((b >>> 2) | (b << 30)) ^ ((b >>> 13) | (b << 19)) ^ ((b >>> 22) | (b << 10)); - s1 = ((f >>> 6) | (f << 26)) ^ ((f >>> 11) | (f << 21)) ^ ((f >>> 25) | (f << 7)); - bc = b & c; - maj = bc ^ (b & d) ^ cd; - ch = (f & g) ^ (~f & h); - t1 = e + s1 + ch + K[j + 3] + blocks[j + 3]; - t2 = s0 + maj; - e = a + t1 << 0; - a = t1 + t2 << 0; - } - - this.h0 = this.h0 + a << 0; - this.h1 = this.h1 + b << 0; - this.h2 = this.h2 + c << 0; - this.h3 = this.h3 + d << 0; - this.h4 = this.h4 + e << 0; - this.h5 = this.h5 + f << 0; - this.h6 = this.h6 + g << 0; - this.h7 = this.h7 + h << 0; - }; - - Sha256.prototype.hex = function () { - this.finalize(); - - var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4, h5 = this.h5, - h6 = this.h6, h7 = this.h7; - - var hex = HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] + - HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] + - HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] + - HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] + - HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] + - HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] + - HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] + - HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] + - HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] + - HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] + - HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] + - HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] + - HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F] + - HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] + - HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] + - HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] + - HEX_CHARS[(h4 >> 28) & 0x0F] + HEX_CHARS[(h4 >> 24) & 0x0F] + - HEX_CHARS[(h4 >> 20) & 0x0F] + HEX_CHARS[(h4 >> 16) & 0x0F] + - HEX_CHARS[(h4 >> 12) & 0x0F] + HEX_CHARS[(h4 >> 8) & 0x0F] + - HEX_CHARS[(h4 >> 4) & 0x0F] + HEX_CHARS[h4 & 0x0F] + - HEX_CHARS[(h5 >> 28) & 0x0F] + HEX_CHARS[(h5 >> 24) & 0x0F] + - HEX_CHARS[(h5 >> 20) & 0x0F] + HEX_CHARS[(h5 >> 16) & 0x0F] + - HEX_CHARS[(h5 >> 12) & 0x0F] + HEX_CHARS[(h5 >> 8) & 0x0F] + - HEX_CHARS[(h5 >> 4) & 0x0F] + HEX_CHARS[h5 & 0x0F] + - HEX_CHARS[(h6 >> 28) & 0x0F] + HEX_CHARS[(h6 >> 24) & 0x0F] + - HEX_CHARS[(h6 >> 20) & 0x0F] + HEX_CHARS[(h6 >> 16) & 0x0F] + - HEX_CHARS[(h6 >> 12) & 0x0F] + HEX_CHARS[(h6 >> 8) & 0x0F] + - HEX_CHARS[(h6 >> 4) & 0x0F] + HEX_CHARS[h6 & 0x0F]; - if (!this.is224) { - hex += HEX_CHARS[(h7 >> 28) & 0x0F] + HEX_CHARS[(h7 >> 24) & 0x0F] + - HEX_CHARS[(h7 >> 20) & 0x0F] + HEX_CHARS[(h7 >> 16) & 0x0F] + - HEX_CHARS[(h7 >> 12) & 0x0F] + HEX_CHARS[(h7 >> 8) & 0x0F] + - HEX_CHARS[(h7 >> 4) & 0x0F] + HEX_CHARS[h7 & 0x0F]; - } - return hex; - }; - - Sha256.prototype.toString = Sha256.prototype.hex; - - Sha256.prototype.digest = function () { - this.finalize(); - - var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4, h5 = this.h5, - h6 = this.h6, h7 = this.h7; - - var arr = [ - (h0 >> 24) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 8) & 0xFF, h0 & 0xFF, - (h1 >> 24) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 8) & 0xFF, h1 & 0xFF, - (h2 >> 24) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 8) & 0xFF, h2 & 0xFF, - (h3 >> 24) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 8) & 0xFF, h3 & 0xFF, - (h4 >> 24) & 0xFF, (h4 >> 16) & 0xFF, (h4 >> 8) & 0xFF, h4 & 0xFF, - (h5 >> 24) & 0xFF, (h5 >> 16) & 0xFF, (h5 >> 8) & 0xFF, h5 & 0xFF, - (h6 >> 24) & 0xFF, (h6 >> 16) & 0xFF, (h6 >> 8) & 0xFF, h6 & 0xFF - ]; - if (!this.is224) { - arr.push((h7 >> 24) & 0xFF, (h7 >> 16) & 0xFF, (h7 >> 8) & 0xFF, h7 & 0xFF); - } - return arr; - }; - - Sha256.prototype.array = Sha256.prototype.digest; - - Sha256.prototype.arrayBuffer = function () { - this.finalize(); - - var buffer = new ArrayBuffer(this.is224 ? 28 : 32); - var dataView = new DataView(buffer); - dataView.setUint32(0, this.h0); - dataView.setUint32(4, this.h1); - dataView.setUint32(8, this.h2); - dataView.setUint32(12, this.h3); - dataView.setUint32(16, this.h4); - dataView.setUint32(20, this.h5); - dataView.setUint32(24, this.h6); - if (!this.is224) { - dataView.setUint32(28, this.h7); - } - return buffer; - }; - - function HmacSha256(key, is224, sharedMemory) { - var i, type = typeof key; - if (type === 'string') { - var bytes = [], length = key.length, index = 0, code; - for (i = 0; i < length; ++i) { - code = key.charCodeAt(i); - if (code < 0x80) { - bytes[index++] = code; - } else if (code < 0x800) { - bytes[index++] = (0xc0 | (code >> 6)); - bytes[index++] = (0x80 | (code & 0x3f)); - } else if (code < 0xd800 || code >= 0xe000) { - bytes[index++] = (0xe0 | (code >> 12)); - bytes[index++] = (0x80 | ((code >> 6) & 0x3f)); - bytes[index++] = (0x80 | (code & 0x3f)); - } else { - code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff)); - bytes[index++] = (0xf0 | (code >> 18)); - bytes[index++] = (0x80 | ((code >> 12) & 0x3f)); - bytes[index++] = (0x80 | ((code >> 6) & 0x3f)); - bytes[index++] = (0x80 | (code & 0x3f)); - } - } - key = bytes; - } else { - if (type === 'object') { - if (key === null) { - throw new Error(ERROR); - } else if (ARRAY_BUFFER && key.constructor === ArrayBuffer) { - key = new Uint8Array(key); - } else if (!Array.isArray(key)) { - if (!ARRAY_BUFFER || !ArrayBuffer.isView(key)) { - throw new Error(ERROR); - } - } - } else { - throw new Error(ERROR); - } - } - - if (key.length > 64) { - key = (new Sha256(is224, true)).update(key).array(); - } - - var oKeyPad = [], iKeyPad = []; - for (i = 0; i < 64; ++i) { - var b = key[i] || 0; - oKeyPad[i] = 0x5c ^ b; - iKeyPad[i] = 0x36 ^ b; - } - - Sha256.call(this, is224, sharedMemory); - - this.update(iKeyPad); - this.oKeyPad = oKeyPad; - this.inner = true; - this.sharedMemory = sharedMemory; - } - HmacSha256.prototype = new Sha256(); - - HmacSha256.prototype.finalize = function () { - Sha256.prototype.finalize.call(this); - if (this.inner) { - this.inner = false; - var innerHash = this.array(); - Sha256.call(this, this.is224, this.sharedMemory); - this.update(this.oKeyPad); - this.update(innerHash); - Sha256.prototype.finalize.call(this); - } - }; - - var exports = createMethod(); - exports.sha256 = exports; - exports.sha224 = createMethod(true); - exports.sha256.hmac = createHmacMethod(); - exports.sha224.hmac = createHmacMethod(true); - - if (COMMON_JS) { - module.exports = exports; - } else { - root.sha256 = exports.sha256; - root.sha224 = exports.sha224; - if (AMD) { - define(function () { - return exports; - }); - } - } -})(); - -export default fake_window.sha256; diff --git a/background/storage.js b/background/storage.js new file mode 100644 index 0000000..ea390ef --- /dev/null +++ b/background/storage.js @@ -0,0 +1,397 @@ +/** +* Myext storage manager +* +* Copyright (C) 2021 Wojtek Kosior +* +* Dual-licensed under: +* - 0BSD license +* - GPLv3 or (at your option) any later version +*/ + +"use strict"; + +(() => { + const TYPE_PREFIX = window.TYPE_PREFIX; + const TYPE_NAME = window.TYPE_NAME; + const list_prefixes = window.list_prefixes; + const make_lock = window.make_lock; + const lock = window.lock; + const unlock = window.unlock; + const make_once = window.make_once; + const browser = window.browser; + const is_chrome = window.is_chrome; + + var exports = {}; + + /* We're yet to decide how to handle errors... */ + + /* Here are some basic wrappers for storage API functions */ + + async function get(key) + { + try { + /* Fix for fact that Chrome does not use promises here */ + let promise = is_chrome ? + new Promise((resolve, reject) => + chrome.storage.local.get(key, + val => resolve(val))) : + browser.storage.local.get(key); + + return (await promise)[key]; + } catch (e) { + console.log(e); + } + } + + async function set(key, value) + { + try { + return browser.storage.local.set({[key]: value}); + } catch (e) { + console.log(e); + } + } + + async function setn(keys_and_values) + { + let obj = Object(); + while (keys_and_values.length > 1) { + let value = keys_and_values.pop(); + let key = keys_and_values.pop(); + obj[key] = value; + } + + try { + return browser.storage.local.set(obj); + } catch (e) { + console.log(e); + } + } + + async function set_var(name, value) + { + return set(TYPE_PREFIX.VAR + name, value); + } + + async function get_var(name) + { + return get(TYPE_PREFIX.VAR + name); + } + + /* + * A special case of persisted variable is one that contains list + * of items. + */ + + async function get_list_var(name) + { + let list = await get_var(name); + + return list === undefined ? [] : list; + } + + /* We maintain in-memory copies of some stored lists. */ + + async function list(prefix) + { + let name = TYPE_NAME[prefix] + "s"; /* Make plural. */ + let map = new Map(); + + for (let item of await get_list_var(name)) + map.set(item, await get(prefix + item)); + + return {map, prefix, name, listeners : new Set(), lock : make_lock()}; + } + + var pages; + var bundles; + var scripts; + + var list_by_prefix = {}; + + async function init() + { + for (let prefix of list_prefixes) + list_by_prefix[prefix] = await list(prefix); + + return exports; + } + + /* + * Facilitate listening to changes + */ + + exports.add_change_listener = function (cb, prefixes=list_prefixes) + { + if (typeof(prefixes) === "string") + prefixes = [prefixes]; + + for (let prefix of prefixes) + list_by_prefix[prefix].listeners.add(cb); + } + + exports.remove_change_listener = function (cb, prefixes=list_prefixes) + { + if (typeof(prefixes) === "string") + prefixes = [prefixes]; + + for (let prefix of prefixes) + list_by_prefix[prefix].listeners.delete(cb); + } + + function broadcast_change(change, list) + { + for (let listener_callback of list.listeners) + listener_callback(change); + } + + /* Prepare some hepler functions to get elements of a list */ + + function list_items_it(list, with_values=false) + { + return with_values ? list.map.entries() : list.map.keys(); + } + + function list_entries_it(list) + { + return list_items_it(list, true); + } + + function list_items(list, with_values=false) + { + let array = []; + + for (let item of list_items_it(list, with_values)) + array.push(item); + + return array; + } + + function list_entries(list) + { + return list_items(list, true); + } + + /* + * Below we make additional effort to update map of given kind of items + * every time an item is added/removed to keep everything coherent. + */ + async function set_item(item, value, list) + { + await lock(list.lock); + let result = await _set_item(...arguments); + unlock(list.lock) + return result; + } + async function _set_item(item, value, list) + { + let key = list.prefix + item; + let old_val = list.map.get(item); + if (old_val === undefined) { + let items = list_items(list); + items.push(item); + await setn([key, value, "_" + list.name, items]); + } else { + await set(key, value); + } + + list.map.set(item, value) + + let change = { + prefix : list.prefix, + item, + old_val, + new_val : value + }; + + broadcast_change(change, list); + + return old_val; + } + + // TODO: The actual idea to set value to undefined is good - this way we can + // also set a new list of items in the same API call. But such key + // is still stored in the storage. We need to somehow remove it later. + // For that, we're going to have to store 1 more list of each kind. + async function remove_item(item, list) + { + await lock(list.lock); + let result = await _remove_item(...arguments); + unlock(list.lock) + return result; + } + async function _remove_item(item, list) + { + let old_val = list.map.get(item); + if (old_val === undefined) + return; + + let key = list.prefix + item; + let items = list_items(list); + let index = items.indexOf(item); + items.splice(index, 1); + + await setn([key, undefined, "_" + list.name, items]); + + list.map.delete(item); + + let change = { + prefix : list.prefix, + item, + old_val, + new_val : undefined + }; + + broadcast_change(change, list); + + return old_val; + } + + // TODO: same as above applies here + async function replace_item(old_item, new_item, list, new_val=undefined) + { + await lock(list.lock); + let result = await _replace_item(...arguments); + unlock(list.lock) + return result; + } + async function _replace_item(old_item, new_item, list, new_val=undefined) + { + let old_val = list.map.get(old_item); + if (new_val === undefined) { + if (old_val === undefined) + return; + new_val = old_val + } else if (new_val === old_val && new_item === old_item) { + return old_val; + } + + if (old_item === new_item || old_val === undefined) { + await _set_item(new_item, new_val, list); + return old_val; + } + + let new_key = list.prefix + new_item; + let old_key = list.prefix + old_item; + let items = list_items(list); + let index = items.indexOf(old_item); + items[index] = new_item; + await setn([old_key, undefined, new_key, new_val, + "_" + list.name, items]); + + list.map.delete(old_item); + + let change = { + prefix : list.prefix, + item : old_item, + old_val, + new_val : undefined + }; + + broadcast_change(change, list); + + list.map.set(new_item, new_val); + + change.item = new_item; + change.old_val = undefined; + change.new_val = new_val; + + broadcast_change(change, list); + + return old_val; + } + + /* + * For scripts, item name is chosen by user, data should be + * an object containing: + * - script's url and hash or + * - script's text or + * - all three + */ + + /* + * For bundles, item name is chosen by user, data is an array of 2-element + * arrays with type prefix and script/bundle names. + */ + + /* + * For pages data argument is an object with properties `allow' + * and `components'. Item name is url. + */ + + exports.set = async function (prefix, item, data) + { + return set_item(item, data, list_by_prefix[prefix]); + } + + exports.get = function (prefix, item) + { + return list_by_prefix[prefix].map.get(item); + } + + exports.remove = async function (prefix, item) + { + return remove_item(item, list_by_prefix[prefix]); + } + + exports.replace = async function (prefix, old_item, new_item, + new_data=undefined) + { + return replace_item(old_item, new_item, list_by_prefix[prefix], + new_data); + } + + exports.get_all_names = function (prefix) + { + return list_items(list_by_prefix[prefix]); + } + + exports.get_all_names_it = function (prefix) + { + return list_items_it(list_by_prefix[prefix]); + } + + exports.get_all = function (prefix) + { + return list_entries(list_by_prefix[prefix]); + } + + exports.get_all_it = function (prefix) + { + return list_entries_it(list_by_prefix[prefix]); + } + + /* Finally, a quick way to wipe all the data. */ + // TODO: maybe delete items in such order that none of them ever references + // an already-deleted one? + exports.clear = async function () + { + let lists = list_prefixes.map((p) => list_by_prefix[p]); + + for (let list of lists) + await lock(list.lock); + + for (let list of lists) { + + let change = { + prefix : list.prefix, + new_val : undefined + }; + + for (let [item, val] of list_entries_it(list)) { + change.item = item; + change.old_val = val; + broadcast_change(change, list); + } + + list.map = new Map(); + } + + await browser.storage.local.clear(); + + for (let list of lists) + unlock(list.lock); + } + + window.get_storage = make_once(init); +})(); diff --git a/background/storage.mjs b/background/storage.mjs deleted file mode 100644 index 00b1ace..0000000 --- a/background/storage.mjs +++ /dev/null @@ -1,380 +0,0 @@ -/** -* Myext storage manager -* -* Copyright (C) 2021 Wojtek Kosior -* -* Dual-licensed under: -* - 0BSD license -* - GPLv3 or (at your option) any later version -*/ - -import {TYPE_PREFIX, TYPE_NAME, list_prefixes} from '/common/stored_types.mjs'; -import {make_lock, lock, unlock} from '/common/lock.mjs'; -import make_once from '/common/once.mjs'; -import browser from '/common/browser.mjs'; - -"use strict"; - -var exports = {}; - -/* We're yet to decide how to handle errors... */ - -/* Here are some basic wrappers for storage API functions */ - -async function get(key) -{ - try { - /* Fix for fact that Chrome does not use promises here */ - let promise = window.browser === undefined ? - new Promise((resolve, reject) => - chrome.storage.local.get(key, val => resolve(val))) : - browser.storage.local.get(key); - - return (await promise)[key]; - } catch (e) { - console.log(e); - } -} - -async function set(key, value) -{ - try { - return browser.storage.local.set({[key]: value}); - } catch (e) { - console.log(e); - } -} - -async function setn(keys_and_values) -{ - let obj = Object(); - while (keys_and_values.length > 1) { - let value = keys_and_values.pop(); - let key = keys_and_values.pop(); - obj[key] = value; - } - - try { - return browser.storage.local.set(obj); - } catch (e) { - console.log(e); - } -} - -async function set_var(name, value) -{ - return set(TYPE_PREFIX.VAR + name, value); -} - -async function get_var(name) -{ - return get(TYPE_PREFIX.VAR + name); -} - -/* A special case of a persisted variable is one that contains list of items. */ - -async function get_list_var(name) -{ - let list = await get_var(name); - - return list === undefined ? [] : list; -} - -/* We maintain in-memory copies of some stored lists. */ - -async function list(prefix) -{ - let name = TYPE_NAME[prefix] + "s"; /* Make plural. */ - let map = new Map(); - - for (let item of await get_list_var(name)) - map.set(item, await get(prefix + item)); - - return {map, prefix, name, listeners : new Set(), lock : make_lock()}; -} - -var pages; -var bundles; -var scripts; - -var list_by_prefix = {}; - -async function init() -{ - for (let prefix of list_prefixes) - list_by_prefix[prefix] = await list(prefix); - - return exports; -} - -/* - * Facilitate listening to changes - */ - -exports.add_change_listener = function (cb, prefixes=list_prefixes) -{ - if (typeof(prefixes) === "string") - prefixes = [prefixes]; - - for (let prefix of prefixes) - list_by_prefix[prefix].listeners.add(cb); -} - -exports.remove_change_listener = function (cb, prefixes=list_prefixes) -{ - if (typeof(prefixes) === "string") - prefixes = [prefixes]; - - for (let prefix of prefixes) - list_by_prefix[prefix].listeners.delete(cb); -} - -function broadcast_change(change, list) -{ - for (let listener_callback of list.listeners) - listener_callback(change); -} - -/* Prepare some hepler functions to get elements of a list */ - -function list_items_it(list, with_values=false) -{ - return with_values ? list.map.entries() : list.map.keys(); -} - -function list_entries_it(list) -{ - return list_items_it(list, true); -} - -function list_items(list, with_values=false) -{ - let array = []; - - for (let item of list_items_it(list, with_values)) - array.push(item); - - return array; -} - -function list_entries(list) -{ - return list_items(list, true); -} - -/* - * Below we make additional effort to update map of given kind of items - * every time an item is added/removed to keep everything coherent. - */ -async function set_item(item, value, list) -{ - await lock(list.lock); - let result = await _set_item(...arguments); - unlock(list.lock) - return result; -} -async function _set_item(item, value, list) -{ - let key = list.prefix + item; - let old_val = list.map.get(item); - if (old_val === undefined) { - let items = list_items(list); - items.push(item); - await setn([key, value, "_" + list.name, items]); - } else { - await set(key, value); - } - - list.map.set(item, value) - - let change = { - prefix : list.prefix, - item, - old_val, - new_val : value - }; - - broadcast_change(change, list); - - return old_val; -} - -// TODO: The actual idea to set value to undefined is good - this way we can -// also set a new list of items in the same API call. But such key -// is still stored in the storage. We need to somehow remove it later. -// For that, we're going to have to store 1 more list of each kind. -async function remove_item(item, list) -{ - await lock(list.lock); - let result = await _remove_item(...arguments); - unlock(list.lock) - return result; -} -async function _remove_item(item, list) -{ - let old_val = list.map.get(item); - if (old_val === undefined) - return; - - let key = list.prefix + item; - let items = list_items(list); - let index = items.indexOf(item); - items.splice(index, 1); - - await setn([key, undefined, "_" + list.name, items]); - - list.map.delete(item); - - let change = { - prefix : list.prefix, - item, - old_val, - new_val : undefined - }; - - broadcast_change(change, list); - - return old_val; -} - -// TODO: same as above applies here -async function replace_item(old_item, new_item, list, new_val=undefined) -{ - await lock(list.lock); - let result = await _replace_item(...arguments); - unlock(list.lock) - return result; -} -async function _replace_item(old_item, new_item, list, new_val=undefined) -{ - let old_val = list.map.get(old_item); - if (new_val === undefined) { - if (old_val === undefined) - return; - new_val = old_val - } else if (new_val === old_val && new_item === old_item) { - return old_val; - } - - if (old_item === new_item || old_val === undefined) { - await _set_item(new_item, new_val, list); - return old_val; - } - - let new_key = list.prefix + new_item; - let old_key = list.prefix + old_item; - let items = list_items(list); - let index = items.indexOf(old_item); - items[index] = new_item; - await setn([old_key, undefined, new_key, new_val, "_" + list.name, items]); - - list.map.delete(old_item); - - let change = { - prefix : list.prefix, - item : old_item, - old_val, - new_val : undefined - }; - - broadcast_change(change, list); - - list.map.set(new_item, new_val); - - change.item = new_item; - change.old_val = undefined; - change.new_val = new_val; - - broadcast_change(change, list); - - return old_val; -} - -/* - * For scripts, item name is chosen by user, data should be an object containing - * - script's url and hash or - * - script's text or - * - all three - */ - -/* - * For bundles, item name is chosen by user, data is an array of 2-element - * arrays with type prefix and script/bundle names. - */ - -/* For pages data argument is same as for bundles. Item name is url. */ - -exports.set = async function (prefix, item, data) -{ - return set_item(item, data, list_by_prefix[prefix]); -} - -exports.get = function (prefix, item) -{ - return list_by_prefix[prefix].map.get(item); -} - -exports.remove = async function (prefix, item) -{ - return remove_item(item, list_by_prefix[prefix]); -} - -exports.replace = async function (prefix, old_item, new_item, - new_data=undefined) -{ - return replace_item(old_item, new_item, list_by_prefix[prefix], new_data); -} - -exports.get_all_names = function (prefix) -{ - return list_items(list_by_prefix[prefix]); -} - -exports.get_all_names_it = function (prefix) -{ - return list_items_it(list_by_prefix[prefix]); -} - -exports.get_all = function (prefix) -{ - return list_entries(list_by_prefix[prefix]); -} - -exports.get_all_it = function (prefix) -{ - return list_entries_it(list_by_prefix[prefix]); -} - -/* Finally, a quick way to wipe all the data. */ -// TODO: maybe delete items in such order that none of them ever references -// an already-deleted one? -exports.clear = async function () -{ - let lists = list_prefixes.map((p) => list_by_prefix[p]); - - for (let list of lists) - await lock(list.lock); - - for (let list of lists) { - - let change = { - prefix : list.prefix, - new_val : undefined - }; - - for (let [item, val] of list_entries_it(list)) { - change.item = item; - change.old_val = val; - broadcast_change(change, list); - } - - list.map = new Map(); - } - - await browser.storage.local.clear(); - - for (let list of lists) - unlock(list.lock); -} - -export default make_once(init); diff --git a/background/storage_server.js b/background/storage_server.js new file mode 100644 index 0000000..05f616b --- /dev/null +++ b/background/storage_server.js @@ -0,0 +1,65 @@ +/** +* Myext storage through connection (server side) +* +* Copyright (C) 2021 Wojtek Kosior +* +* Dual-licensed under: +* - 0BSD license +* - GPLv3 or (at your option) any later version +*/ + +"use strict"; + +(() => { + const listen_for_connection = window.listen_for_connection; + const get_storage = window.get_storage; + const TYPE_PREFIX = window.TYPE_PREFIX; + const CONNECTION_TYPE = window.CONNECTION_TYPE; + + var storage; + + async function handle_remote_call(port, message) + { + let [call_id, func, args] = message; + + try { + let result = await Promise.resolve(storage[func](...args)); + port.postMessage({call_id, result}); + } catch (error) { + error = error + ''; + port.postMessage({call_id, error}); + } + } + + function remove_storage_listener(cb) { + storage.remove_change_listener(cb); + } + + function new_connection(port) + { + console.log("new remote storage connection!"); + + port.postMessage({ + [TYPE_PREFIX.SCRIPT] : storage.get_all(TYPE_PREFIX.SCRIPT), + [TYPE_PREFIX.BUNDLE] : storage.get_all(TYPE_PREFIX.BUNDLE), + [TYPE_PREFIX.PAGE] : storage.get_all(TYPE_PREFIX.PAGE) + }); + + let handle_change = change => port.postMessage(change); + + storage.add_change_listener(handle_change); + + port.onMessage.addListener(m => handle_remote_call(port, m)); + port.onDisconnect.addListener(() => + remove_storage_listener(handle_change)); + } + + async function start() + { + storage = await get_storage(); + + listen_for_connection(CONNECTION_TYPE.REMOTE_STORAGE, new_connection); + } + + window.start_storage_server = start; +})(); diff --git a/background/storage_server.mjs b/background/storage_server.mjs deleted file mode 100644 index 3a141ff..0000000 --- a/background/storage_server.mjs +++ /dev/null @@ -1,58 +0,0 @@ -/** -* Myext storage through connection (server side) -* -* Copyright (C) 2021 Wojtek Kosior -* -* Dual-licensed under: -* - 0BSD license -* - GPLv3 or (at your option) any later version -*/ - -import listen_for_connection from './message_server.mjs'; -import get_storage from './storage.mjs'; -import {TYPE_PREFIX} from '/common/stored_types.mjs'; -import CONNECTION_TYPE from '/common/connection_types.mjs'; - -var storage; - -async function handle_remote_call(port, message) -{ - let [call_id, func, args] = message; - - try { - let result = await Promise.resolve(storage[func](...args)); - port.postMessage({call_id, result}); - } catch (error) { - error = error + ''; - port.postMessage({call_id, error}); - } -} - -function remove_storage_listener(cb) { - storage.remove_change_listener(cb); -} - -function new_connection(port) -{ - console.log("new remote storage connection!"); - - port.postMessage({ - [TYPE_PREFIX.SCRIPT] : storage.get_all(TYPE_PREFIX.SCRIPT), - [TYPE_PREFIX.BUNDLE] : storage.get_all(TYPE_PREFIX.BUNDLE), - [TYPE_PREFIX.PAGE] : storage.get_all(TYPE_PREFIX.PAGE) - }); - - let handle_change = change => port.postMessage(change); - - storage.add_change_listener(handle_change); - - port.onMessage.addListener(m => handle_remote_call(port, m)); - port.onDisconnect.addListener(() => remove_storage_listener(handle_change)); -} - -export default async function start() -{ - storage = await get_storage(); - - listen_for_connection(CONNECTION_TYPE.REMOTE_STORAGE, new_connection); -} diff --git a/background/url_item.js b/background/url_item.js new file mode 100644 index 0000000..7850871 --- /dev/null +++ b/background/url_item.js @@ -0,0 +1,22 @@ +/** +* Myext stripping url from query and target +* +* Copyright (C) 2021 Wojtek Kosior +* +* Dual-licensed under: +* - 0BSD license +* - GPLv3 or (at your option) any later version +*/ + +"use strict"; + +(() => { + function url_item(url) + { + let url_re = /^([^?#]*).*$/; + let match = url_re.exec(url); + return match[1]; + } + + window.url_item = url_item; +})(); diff --git a/background/url_item.mjs b/background/url_item.mjs deleted file mode 100644 index 2ee1d6d..0000000 --- a/background/url_item.mjs +++ /dev/null @@ -1,18 +0,0 @@ -/** -* Myext stripping url from query and target -* -* Copyright (C) 2021 Wojtek Kosior -* -* Dual-licensed under: -* - 0BSD license -* - GPLv3 or (at your option) any later version -*/ - -"use strict"; - -export default function url_item(url) -{ - let url_re = /^([^?#]*).*$/; - let match = url_re.exec(url); - return match[1]; -} diff --git a/common/browser.js b/common/browser.js new file mode 100644 index 0000000..1d9edda --- /dev/null +++ b/common/browser.js @@ -0,0 +1,27 @@ +/** +* Myext WebExtension API access normalization +* +* Copyright (C) 2021 Wojtek Kosior +* +* Dual-licensed under: +* - 0BSD license +* - GPLv3 or (at your option) any later version +*/ + +"use strict"; + +/* + * This module normalizes access to WebExtension apis between + * chrome-based and firefox-based browsers. + */ + +(() => { + if (window.browser === undefined) { + window.browser = window.chrome; + window.is_chrome = true; + window.is_mozilla = false; + } else { + window.is_chrome = false; + window.is_mozilla = true; + } +})(); diff --git a/common/browser.mjs b/common/browser.mjs deleted file mode 100644 index 0d1b233..0000000 --- a/common/browser.mjs +++ /dev/null @@ -1,18 +0,0 @@ -/** -* Myext WebExtension API access normalization -* -* Copyright (C) 2021 Wojtek Kosior -* -* Dual-licensed under: -* - 0BSD license -* - GPLv3 or (at your option) any later version -*/ - -"use strict"; - -/* - * This module normalizes access to WebExtension apis between - * chrome-based and firefox-based browsers. - */ - -export default (window.browser === undefined) ? chrome : browser; diff --git a/common/connection_types.js b/common/connection_types.js new file mode 100644 index 0000000..a01a777 --- /dev/null +++ b/common/connection_types.js @@ -0,0 +1,25 @@ +/** +* Myext background scripts message connection types "enum" +* +* Copyright (C) 2021 Wojtek Kosior +* +* Dual-licensed under: +* - 0BSD license +* - GPLv3 or (at your option) any later version +*/ + +"use strict"; + +/* + * Those need to be strings so they can be used as 'name' parameter + * to browser.runtime.connect() + */ + +(() => { + const CONNECTION_TYPE = { + REMOTE_STORAGE : "0", + PAGE_ACTIONS : "1" + }; + + window.CONNECTION_TYPE = CONNECTION_TYPE; +})(); diff --git a/common/connection_types.mjs b/common/connection_types.mjs deleted file mode 100644 index 12d6de3..0000000 --- a/common/connection_types.mjs +++ /dev/null @@ -1,21 +0,0 @@ -/** -* Myext background scripts message connection types "enum" -* -* Copyright (C) 2021 Wojtek Kosior -* -* Dual-licensed under: -* - 0BSD license -* - GPLv3 or (at your option) any later version -*/ - -/* - * Those need to be strings so they can be used as 'name' parameter - * to browser.runtime.connect() - */ - -const CONNECTION_TYPE = { - REMOTE_STORAGE : "0", - PAGE_ACTIONS : "1" -}; - -export default CONNECTION_TYPE; diff --git a/common/is_background.mjs b/common/is_background.mjs deleted file mode 100644 index ef728a7..0000000 --- a/common/is_background.mjs +++ /dev/null @@ -1,17 +0,0 @@ -/** -* Myext programmatic check of where the script is being run -* -* Copyright (C) 2021 Wojtek Kosior -* -* Dual-licensed under: -* - 0BSD license -* - GPLv3 or (at your option) any later version -*/ - -/* This needs to be changed if we ever modify the html file path. */ - -export default function is_background() -{ - return window.location.protocol === "moz-extension:" && - window.location.pathname === "/background/background.html"; -} diff --git a/common/lock.js b/common/lock.js new file mode 100644 index 0000000..107287f --- /dev/null +++ b/common/lock.js @@ -0,0 +1,61 @@ +/** +* Myext lock (aka binary semaphore aka mutex) +* +* Copyright (C) 2021 Wojtek Kosior +* +* Dual-licensed under: +* - 0BSD license +* - GPLv3 or (at your option) any later version +*/ + +/* + * Javascript runs single-threaded, with an event loop. Because of that, + * explicit synchronization is often not needed. An exception is when we use + * an API function that must wait. Ajax is an example. Callback passed to ajax + * call doesn't get called immediately, but after some time. In the meantime + * some other piece of code might get to execute and modify some variables. + * Access to WebExtension local storage is another situation where this problem + * can occur. + * + * This is a solution. A lock object, that can be used to delay execution of + * some code until other code finishes its critical work. Locking is wrapped + * in a promise. + */ + +"use strict"; + +(() => { + function make_lock() { + return {free: true, queue: []}; + } + + function _lock(lock, cb) { + if (lock.free) { + lock.free = false; + setTimeout(cb); + } else { + lock.queue.push(cb); + } + } + + function lock(lock) { + return new Promise((resolve, reject) => _lock(lock, resolve)); + } + + function unlock(lock) { + if (lock.free) + throw new Exception("Attempting to release a free lock"); + + if (lock.queue.length === 0) { + lock.free = true; + } else { + let cb = lock.queue[0]; + lock.queue.splice(0, 1); + setTimeout(cb); + } + } + + window.make_lock = make_lock; + window.lock = lock; + window.unlock = unlock; +})(); diff --git a/common/lock.mjs b/common/lock.mjs deleted file mode 100644 index 596dd9c..0000000 --- a/common/lock.mjs +++ /dev/null @@ -1,55 +0,0 @@ -/** -* Myext lock (aka binary semaphore aka mutex) -* -* Copyright (C) 2021 Wojtek Kosior -* -* Dual-licensed under: -* - 0BSD license -* - GPLv3 or (at your option) any later version -*/ - -/* - * Javascript runs single-threaded, with an event loop. Because of that, - * explicit synchronization is often not needed. An exception is when we use - * an API function that must wait. Ajax is an example. Callback passed to ajax - * call doesn't get called immediately, but after some time. In the meantime - * some other piece of code might get to execute and modify some variables. - * Access to WebExtension local storage is another situation where this problem - * can occur. - * - * This is a solution. A lock object, that can be used to delay execution of - * some code until other code finishes its critical work. Locking is wrapped - * in a promise. - */ - -"use strict"; - -export function make_lock() { - return {free: true, queue: []}; -} - -function _lock(lock, cb) { - if (lock.free) { - lock.free = false; - setTimeout(cb); - } else { - lock.queue.push(cb); - } -} - -export function lock(lock) { - return new Promise((resolve, reject) => _lock(lock, resolve)); -} - -export function unlock(lock) { - if (lock.free) - throw new Exception("Attempting to release a free lock"); - - if (lock.queue.length === 0) { - lock.free = true; - } else { - let cb = lock.queue[0]; - lock.queue.splice(0, 1); - setTimeout(cb); - } -} diff --git a/common/once.js b/common/once.js new file mode 100644 index 0000000..1842a47 --- /dev/null +++ b/common/once.js @@ -0,0 +1,44 @@ +/** +* Myext feature initialization promise +* +* Copyright (C) 2021 Wojtek Kosior +* +* Dual-licensed under: +* - 0BSD license +* - GPLv3 or (at your option) any later version +*/ + +"use strict"; + +/* + * This module provides an easy way to wrap an async function into a promise + * so that it only gets executed once. + */ + +(() => { + async function assign_result(state, result_producer) + { + state.result = await result_producer(); + state.ready = true; + for (let cb of state.waiting) + setTimeout(cb, 0, state.result); + state.waiting = undefined; + } + + async function get_result(state) + { + if (state.ready) + return state.result; + + return new Promise((resolve, reject) => state.waiting.push(resolve)); + } + + function make_once(result_producer) + { + let state = {waiting : [], ready : false, result : undefined}; + assign_result(state, result_producer); + return () => get_result(state); + } + + window.make_once = make_once; +})(); diff --git a/common/once.mjs b/common/once.mjs deleted file mode 100644 index 0f76366..0000000 --- a/common/once.mjs +++ /dev/null @@ -1,42 +0,0 @@ -/** -* Myext feature initialization promise -* -* Copyright (C) 2021 Wojtek Kosior -* -* Dual-licensed under: -* - 0BSD license -* - GPLv3 or (at your option) any later version -*/ - -"use strict"; - -/* - * This module provides an easy way to wrap an async function into a promise - * so that it only gets executed once. - */ - -async function assign_result(state, result_producer) -{ - state.result = await result_producer(); - state.ready = true; - for (let cb of state.waiting) - setTimeout(cb, 0, state.result); - state.waiting = undefined; -} - -async function get_result(state) -{ - if (state.ready) - return state.result; - - return new Promise((resolve, reject) => state.waiting.push(resolve)); -} - -export function make_once(result_producer) -{ - let state = {waiting : [], ready : false, result : undefined}; - assign_result(state, result_producer); - return () => get_result(state); -} - -export default make_once; diff --git a/common/storage_client.js b/common/storage_client.js new file mode 100644 index 0000000..39ece44 --- /dev/null +++ b/common/storage_client.js @@ -0,0 +1,189 @@ +/** +* Myext storage through connection (client side) +* +* Copyright (C) 2021 Wojtek Kosior +* +* Dual-licensed under: +* - 0BSD license +* - GPLv3 or (at your option) any later version +*/ + +"use strict"; + +(() => { + const CONNECTION_TYPE = window.CONNECTION_TYPE; + const TYPE_PREFIX = window.TYPE_PREFIX; + const list_prefixes = window.list_prefixes; + const make_once = window.make_once; + const browser = window.browser; + + var call_id = 0; + var port; + var calls_waiting = new Map(); + + function set_call_callback(resolve, reject, func, args) + { + port.postMessage([call_id, func, args]); + calls_waiting.set(call_id++, [resolve, reject]); + } + + async function remote_call(func, args) + { + return new Promise((resolve, reject) => + set_call_callback(resolve, reject, func, args)); + } + + function handle_message(message) + { + let callbacks = calls_waiting.get(message.call_id); + if (callbacks === undefined) { + handle_change(message); + return; + } + + let [resolve, reject] = callbacks; + calls_waiting.delete(message.call_id); + if (message.error !== undefined) + setTimeout(reject, 0, message.error); + else + setTimeout(resolve, 0, message.result); + } + + function list(name, prefix) + { + return {prefix, name, listeners : new Set()}; + } + + var scripts = list("scripts", TYPE_PREFIX.SCRIPT); + var bundles = list("bundles", TYPE_PREFIX.BUNDLE); + var pages = list("pages", TYPE_PREFIX.PAGE); + + const list_by_prefix = { + [TYPE_PREFIX.SCRIPT] : scripts, + [TYPE_PREFIX.BUNDLE] : bundles, + [TYPE_PREFIX.PAGE] : pages + }; + + var resolve_init; + + function handle_first_message(message) + { + for (let prefix of Object.keys(message)) + list_by_prefix[prefix].map = new Map(message[prefix]); + + port.onMessage.removeListener(handle_first_message); + port.onMessage.addListener(handle_message); + + resolve_init(); + } + + function handle_change(change) + { + let list = list_by_prefix[change.prefix]; + + if (change.new_val === undefined) + list.map.delete(change.item); + else + list.map.set(change.item, change.new_val); + + for (let listener_callback of list.listeners) + listener_callback(change); + } + + var exports = {}; + + function start_connection(resolve) + { + resolve_init = resolve; + port = browser.runtime.connect({name : CONNECTION_TYPE.REMOTE_STORAGE}); + port.onMessage.addListener(handle_first_message); + } + + async function init() { + await new Promise((resolve, reject) => start_connection(resolve)); + return exports; + } + + for (let call_name of ["set", "remove", "replace", "clear"]) + exports [call_name] = (...args) => remote_call(call_name, args); + + // TODO: Much of the code below is copy-pasted from /background/storage.mjs. + // This should later be refactored into a separate module + // to avoid duplication. + + /* + * Facilitate listening to changes + */ + + exports.add_change_listener = function (cb, prefixes=list_prefixes) + { + if (typeof(prefixes) === "string") + prefixes = [prefixes]; + + for (let prefix of prefixes) + list_by_prefix[prefix].listeners.add(cb); + } + + exports.remove_change_listener = function (cb, prefixes=list_prefixes) + { + if (typeof(prefixes) === "string") + prefixes = [prefixes]; + + for (let prefix of prefixes) + list_by_prefix[prefix].listeners.delete(cb); + } + + /* Prepare some hepler functions to get elements of a list */ + + function list_items_it(list, with_values=false) + { + return with_values ? list.map.entries() : list.map.keys(); + } + + function list_entries_it(list) + { + return list_items_it(list, true); + } + + function list_items(list, with_values=false) + { + let array = []; + + for (let item of list_items_it(list, with_values)) + array.push(item); + + return array; + } + + function list_entries(list) + { + return list_items(list, true); + } + + exports.get = function (prefix, item) + { + return list_by_prefix[prefix].map.get(item); + } + + exports.get_all_names = function (prefix) + { + return list_items(list_by_prefix[prefix]); + } + + exports.get_all_names_it = function (prefix) + { + return list_items_it(list_by_prefix[prefix]); + } + + exports.get_all = function (prefix) + { + return list_entries(list_by_prefix[prefix]); + } + + exports.get_all_it = function (prefix) + { + return list_entries_it(list_by_prefix[prefix]); + } + + window.get_storage = make_once(init); +})(); diff --git a/common/storage_client.mjs b/common/storage_client.mjs deleted file mode 100644 index 8260ad7..0000000 --- a/common/storage_client.mjs +++ /dev/null @@ -1,186 +0,0 @@ -/** -* Myext storage through connection (client side) -* -* Copyright (C) 2021 Wojtek Kosior -* -* Dual-licensed under: -* - 0BSD license -* - GPLv3 or (at your option) any later version -*/ - -"use strict"; - -import CONNECTION_TYPE from './connection_types.mjs'; -import {TYPE_PREFIX, list_prefixes} from '/common/stored_types.mjs'; -import make_once from './once.mjs'; -import browser from '/common/browser.mjs'; - -var call_id = 0; -var port; -var calls_waiting = new Map(); - -function set_call_callback(resolve, reject, func, args) -{ - port.postMessage([call_id, func, args]); - calls_waiting.set(call_id++, [resolve, reject]); -} - -async function remote_call(func, args) -{ - return new Promise((resolve, reject) => - set_call_callback(resolve, reject, func, args)); -} - -function handle_message(message) -{ - let callbacks = calls_waiting.get(message.call_id); - if (callbacks === undefined) { - handle_change(message); - return; - } - - let [resolve, reject] = callbacks; - calls_waiting.delete(message.call_id); - if (message.error !== undefined) - setTimeout(reject, 0, message.error); - else - setTimeout(resolve, 0, message.result); -} - -function list(name, prefix) -{ - return {prefix, name, listeners : new Set()}; -} - -var scripts = list("scripts", TYPE_PREFIX.SCRIPT); -var bundles = list("bundles", TYPE_PREFIX.BUNDLE); -var pages = list("pages", TYPE_PREFIX.PAGE); - -const list_by_prefix = { - [TYPE_PREFIX.SCRIPT] : scripts, - [TYPE_PREFIX.BUNDLE] : bundles, - [TYPE_PREFIX.PAGE] : pages -}; - -var resolve_init; - -function handle_first_message(message) -{ - for (let prefix of Object.keys(message)) - list_by_prefix[prefix].map = new Map(message[prefix]); - - port.onMessage.removeListener(handle_first_message); - port.onMessage.addListener(handle_message); - - resolve_init(); -} - -function handle_change(change) -{ - let list = list_by_prefix[change.prefix]; - - if (change.new_val === undefined) - list.map.delete(change.item); - else - list.map.set(change.item, change.new_val); - - for (let listener_callback of list.listeners) - listener_callback(change); -} - -var exports = {}; - -function start_connection(resolve) -{ - resolve_init = resolve; - port = browser.runtime.connect({name : CONNECTION_TYPE.REMOTE_STORAGE}); - port.onMessage.addListener(handle_first_message); -} - -async function init() { - await new Promise((resolve, reject) => start_connection(resolve)); - return exports; -} - -for (let call_name of ["set", "remove", "replace", "clear"]) - exports [call_name] = (...args) => remote_call(call_name, args); - -// TODO: Much of the code below is copy-pasted from /background/storage.mjs. -// This should later be refactored into a separate module -// to avoid duplication. - -/* - * Facilitate listening to changes - */ - -exports.add_change_listener = function (cb, prefixes=list_prefixes) -{ - if (typeof(prefixes) === "string") - prefixes = [prefixes]; - - for (let prefix of prefixes) - list_by_prefix[prefix].listeners.add(cb); -} - -exports.remove_change_listener = function (cb, prefixes=list_prefixes) -{ - if (typeof(prefixes) === "string") - prefixes = [prefixes]; - - for (let prefix of prefixes) - list_by_prefix[prefix].listeners.delete(cb); -} - -/* Prepare some hepler functions to get elements of a list */ - -function list_items_it(list, with_values=false) -{ - return with_values ? list.map.entries() : list.map.keys(); -} - -function list_entries_it(list) -{ - return list_items_it(list, true); -} - -function list_items(list, with_values=false) -{ - let array = []; - - for (let item of list_items_it(list, with_values)) - array.push(item); - - return array; -} - -function list_entries(list) -{ - return list_items(list, true); -} - -exports.get = function (prefix, item) -{ - return list_by_prefix[prefix].map.get(item); -} - -exports.get_all_names = function (prefix) -{ - return list_items(list_by_prefix[prefix]); -} - -exports.get_all_names_it = function (prefix) -{ - return list_items_it(list_by_prefix[prefix]); -} - -exports.get_all = function (prefix) -{ - return list_entries(list_by_prefix[prefix]); -} - -exports.get_all_it = function (prefix) -{ - return list_entries_it(list_by_prefix[prefix]); -} - -export default make_once(init); diff --git a/common/stored_types.js b/common/stored_types.js new file mode 100644 index 0000000..de0ec71 --- /dev/null +++ b/common/stored_types.js @@ -0,0 +1,44 @@ +/** + * Myext stored item types "enum" + * + * Copyright (C) 2021 Wojtek Kosior + * + * Dual-licensed under: + * - 0BSD license + * - GPLv3 or (at your option) any later version + */ + +/* + * Key for item that is stored in quantity (script, page) is constructed by + * prepending its name with first letter of its list name. However, we also + * need to store some items that don't belong to any list. Let's call them + * persisted variables. In such case item's key is its "name" prepended with + * an underscore. + */ + +"use strict"; + +(() => { + const TYPE_PREFIX = { + PAGE : "p", + BUNDLE : "b", + SCRIPT : "s", + VAR : "_" + }; + + const TYPE_NAME = { + [TYPE_PREFIX.PAGE] : "page", + [TYPE_PREFIX.BUNDLE] : "bundle", + [TYPE_PREFIX.SCRIPT] : "script" + } + + const list_prefixes = [ + TYPE_PREFIX.PAGE, + TYPE_PREFIX.BUNDLE, + TYPE_PREFIX.SCRIPT + ]; + + window.TYPE_PREFIX = TYPE_PREFIX; + window.TYPE_NAME = TYPE_NAME; + window.list_prefixes = list_prefixes; +})(); diff --git a/common/stored_types.mjs b/common/stored_types.mjs deleted file mode 100644 index 8545d44..0000000 --- a/common/stored_types.mjs +++ /dev/null @@ -1,38 +0,0 @@ -/** -* Myext stored item types "enum" -* -* Copyright (C) 2021 Wojtek Kosior -* -* Dual-licensed under: -* - 0BSD license -* - GPLv3 or (at your option) any later version -*/ - -/* - * Key for item that is stored in quantity (script, page) is constructed by - * prepending its name with first letter of its list name. However, we also - * need to store some items that don't belong to any list. Let's call them - * persisted variables. In such case item's key is its "name" prepended with - * an underscore. - */ - -const TYPE_PREFIX = { - PAGE : "p", - BUNDLE : "b", - SCRIPT : "s", - VAR : "_" -}; - -const TYPE_NAME = { - [TYPE_PREFIX.PAGE] : "page", - [TYPE_PREFIX.BUNDLE] : "bundle", - [TYPE_PREFIX.SCRIPT] : "script" -} - -const list_prefixes = [ - TYPE_PREFIX.PAGE, - TYPE_PREFIX.BUNDLE, - TYPE_PREFIX.SCRIPT -]; - -export {TYPE_PREFIX, TYPE_NAME, list_prefixes}; diff --git a/content/main.js b/content/main.js index 12a94c9..282c7b5 100644 --- a/content/main.js +++ b/content/main.js @@ -1,95 +1,97 @@ /** -* Myext main content script run in all frames -* -* Copyright (C) 2021 Wojtek Kosior -* -* Dual-licensed under: -* - 0BSD license -* - GPLv3 or (at your option) any later version -*/ + * Myext main content script run in all frames + * + * Copyright (C) 2021 Wojtek Kosior + * + * Dual-licensed under: + * - 0BSD license + * - GPLv3 or (at your option) any later version + */ -var url_re = /^([^#]*)((#[^#]*)(#.*)?)?$/; -var match = url_re.exec(document.URL); -var base_url = match[1]; -var first_target = match[3]; -var second_target = match[4]; +"use strict"; -var block = true; -if (first_target !== undefined && - first_target === "#myext-allow") { - block = false; - console.log(["allowing", document.URL]); - if (second_target !== undefined) - window.location.href = base_url + second_target; - else - history.replaceState(null, "", base_url); -} else { - console.log(["not allowing", document.URL]); -} +(() => { + const handle_page_actions = window.handle_page_actions; -function handle_mutation(mutations, observer) -{ - if (document.readyState === 'complete') { - console.log("complete"); - observer.disconnect(); - return; + var url_re = /^([^#]*)((#[^#]*)(#.*)?)?$/; + var match = url_re.exec(document.URL); + var base_url = match[1]; + var first_target = match[3]; + var second_target = match[4]; + + var block = true; + if (first_target !== undefined && + first_target === "#myext-allow") { + block = false; + console.log(["allowing", document.URL]); + if (second_target !== undefined) + window.location.href = base_url + second_target; + else + history.replaceState(null, "", base_url); + } else { + console.log(["not allowing", document.URL]); } - for (let mutation of mutations) { - for (let node of mutation.addedNodes) { - if (node.tagName === "SCRIPT") - block_script(node); - else - sanitize_attributes(node); + + function handle_mutation(mutations, observer) + { + if (document.readyState === 'complete') { + console.log("complete"); + observer.disconnect(); + return; + } + for (let mutation of mutations) { + for (let node of mutation.addedNodes) { + if (node.tagName === "SCRIPT") + block_script(node); + else + sanitize_attributes(node); + } } } -} -function block_script(node) -{ - console.log(node); + function block_script(node) + { + console.log(node); - /* - * Disabling scripts this way allows them to still be relatively accessed - * in case they contain some useful data. - */ - if (node.hasAttribute("type")) - node.setAttribute("blocked-type", node.getAttribute("type")); - node.setAttribute("type", "application/json"); -} + /* + * Disabling scripts this way allows them to still be relatively + * easily accessed in case they contain some useful data. + */ + if (node.hasAttribute("type")) + node.setAttribute("blocked-type", node.getAttribute("type")); + node.setAttribute("type", "application/json"); + } -function sanitize_attributes(node) -{ - if (node.attributes === undefined) - return; + function sanitize_attributes(node) + { + if (node.attributes === undefined) + return; - /* We have to do it in 2 loops, removing attribute modifies our iterator */ - let attr_names = []; - for (let attr of node.attributes) { - let attr_name = attr.localName; - if (attr_name.startsWith("on")) - attr_names.push(attr_name); - } + /* + * We have to do it in 2 loops, removing attribute modifies + * our iterator + */ + let attr_names = []; + for (let attr of node.attributes) { + let attr_name = attr.localName; + if (attr_name.startsWith("on")) + attr_names.push(attr_name); + } - for (let attr_name of attr_names) { - node.removeAttribute(attr_name); - console.log("sanitized", attr_name); + for (let attr_name of attr_names) { + node.removeAttribute(attr_name); + console.log("sanitized", attr_name); + } } -} -async function run_module() -{ - let src = chrome.runtime.getURL("content/page_actions.mjs"); - let module = await import(src); - module.default(); -} - -if (block) { - var observer = new MutationObserver(handle_mutation); - observer.observe(document.documentElement, { - attributes: true, - childList: true, - subtree: true - }); -} + if (block) { + var observer = new MutationObserver(handle_mutation); + observer.observe(document.documentElement, { + attributes: true, + childList: true, + subtree: true + }); + } -run_module(); + handle_page_actions(); +})(); diff --git a/content/page_actions.js b/content/page_actions.js new file mode 100644 index 0000000..047bf24 --- /dev/null +++ b/content/page_actions.js @@ -0,0 +1,63 @@ +/** +* Myext handling of page actions in content scripts +* +* Copyright (C) 2021 Wojtek Kosior +* +* Dual-licensed under: +* - 0BSD license +* - GPLv3 or (at your option) any later version +*/ + +"use strict"; + +(() => { + const CONNECTION_TYPE = window.CONNECTION_TYPE; + const browser = window.browser; + + var port; + var loaded = false; + var scripts_awaiting = []; + + function handle_message(message) + { + console.log(["message", message]); + + if (message.inject === undefined) + return; + + for (let script_text of message.inject) { + if (loaded) + add_script(script_text); + else + scripts_awaiting.push(script_text); + } + } + + function document_loaded(event) + { + console.log("loaded"); + + loaded = true; + + for (let script_text of scripts_awaiting) + add_script(script_text); + + scripts_awaiting = undefined; + } + + function add_script(script_text) + { + let script = document.createElement("script"); + script.textContent = script_text; + document.body.appendChild(script); + } + + function handle_page_actions() { + document.addEventListener("DOMContentLoaded", document_loaded); + port = browser.runtime.connect({name : CONNECTION_TYPE.PAGE_ACTIONS}); + port.onMessage.addListener(handle_message); + port.postMessage({url: document.URL}); + } + + window.handle_page_actions = handle_page_actions; +})(); diff --git a/content/page_actions.mjs b/content/page_actions.mjs deleted file mode 100644 index 3ce5b73..0000000 --- a/content/page_actions.mjs +++ /dev/null @@ -1,58 +0,0 @@ -/** -* Myext handling of page actions in content scripts -* -* Copyright (C) 2021 Wojtek Kosior -* -* Dual-licensed under: -* - 0BSD license -* - GPLv3 or (at your option) any later version -*/ - -import CONNECTION_TYPE from '/common/connection_types.mjs'; -import make_once from '/common/once.mjs'; -import browser from '/common/browser.mjs'; - -var port; -var loaded = false; -var scripts_awaiting = []; - -function handle_message(message) -{ - console.log(["message", message]); - - if (message.inject === undefined) - return; - - for (let script_text of message.inject) { - if (loaded) - add_script(script_text); - else - scripts_awaiting.push(script_text); - } -} - -function document_loaded(event) -{ - console.log("loaded"); - - loaded = true; - - for (let script_text of scripts_awaiting) - add_script(script_text); - - scripts_awaiting = undefined; -} - -function add_script(script_text) -{ - let script = document.createElement("script"); - script.textContent = script_text; - document.body.appendChild(script); -} - -export default function main() { - document.addEventListener("DOMContentLoaded", document_loaded); - port = browser.runtime.connect({name : CONNECTION_TYPE.PAGE_ACTIONS}); - port.onMessage.addListener(handle_message); - port.postMessage({url: document.URL}); -} diff --git a/html/display-panel.html b/html/display-panel.html index 9c8de5e..4adeb53 100644 --- a/html/display-panel.html +++ b/html/display-panel.html @@ -6,6 +6,7 @@ - + + diff --git a/html/display-panel.js b/html/display-panel.js new file mode 100644 index 0000000..91b0df7 --- /dev/null +++ b/html/display-panel.js @@ -0,0 +1,18 @@ +/** +* Myext display panel logic +* +* Copyright (C) 2021 Wojtek Kosior +* +* Dual-licensed under: +* - 0BSD license +* - GPLv3 or (at your option) any later version +*/ + +"use strict"; + +(() => { + const browser = window.browser; + + document.getElementById("settings_but") + .addEventListener("click", (e) => browser.runtime.openOptionsPage()); +})(); diff --git a/html/display-panel.mjs b/html/display-panel.mjs deleted file mode 100644 index 38f2a42..0000000 --- a/html/display-panel.mjs +++ /dev/null @@ -1,16 +0,0 @@ -/** -* Myext display panel logic -* -* Copyright (C) 2021 Wojtek Kosior -* -* Dual-licensed under: -* - 0BSD license -* - GPLv3 or (at your option) any later version -*/ - -"use strict"; - -import browser from '/common/browser.mjs'; - -document.getElementById("settings_but") - .addEventListener("click", (e) => browser.runtime.openOptionsPage()); diff --git a/html/options.html b/html/options.html index 921b423..0bce6fc 100644 --- a/html/options.html +++ b/html/options.html @@ -170,6 +170,11 @@ - + + + + + + diff --git a/html/options_main.js b/html/options_main.js new file mode 100644 index 0000000..979fb44 --- /dev/null +++ b/html/options_main.js @@ -0,0 +1,399 @@ +/** + * Myext HTML options page main script + * + * Copyright (C) 2021 Wojtek Kosior + * + * Dual-licensed under: + * - 0BSD license + * - GPLv3 or (at your option) any later version + */ + +"use strict"; + +(() => { + const get_storage = window.get_storage; + const TYPE_PREFIX = window.TYPE_PREFIX; + const TYPE_NAME = window.TYPE_NAME; + const list_prefixes = window.list_prefixes; + + var storage; + function by_id(id) + { + return document.getElementById(id); + } + + const item_li_template = by_id("item_li_template"); + const component_li_template = by_id("component_li_template"); + const selectable_component_li_template = + by_id("selectable_component_li_template"); + /* Make sure they are later cloned without id. */ + item_li_template.removeAttribute("id"); + component_li_template.removeAttribute("id"); + selectable_component_li_template.removeAttribute("id"); + + function item_li_id(prefix, item) + { + return `li_${prefix}_${item}`; + } + + function add_li(prefix, item, at_the_end=false) + { + let ul = ul_by_prefix[prefix]; + let li = item_li_template.cloneNode(true); + li.id = item_li_id(prefix, item); + + let span = li.firstElementChild; + span.textContent = item; + + let edit_button = span.nextElementSibling; + edit_button.addEventListener("click", () => edit_item(prefix, item)); + + let remove_button = edit_button.nextElementSibling; + remove_button.addEventListener("click", + () => storage.remove(prefix, item)); + + if (!at_the_end) { + for (let element of ul.ul.children) { + if (element.id < li.id || element.id.startsWith("work_")) + continue; + + ul.ul.insertBefore(li, element); + return; + } + } + + ul.ul.appendChild(li); + } + + const selectable_components_ul = by_id("selectable_components_ul"); + + function selectable_li_id(prefix, item) + { + return `sli_${prefix}_${item}`; + } + + function add_selectable(prefix, name) + { + if (prefix === TYPE_PREFIX.PAGE) + return; + + let li = selectable_component_li_template.cloneNode(true); + li.id = selectable_li_id(prefix, name); + li.setAttribute("data-prefix", prefix); + li.setAttribute("data-name", name); + + let chbx = li.firstElementChild; + let span = chbx.nextElementSibling; + + span.textContent = `${name} (${TYPE_NAME[prefix]})`; + + selectable_components_ul.appendChild(li); + } + + /* + * Used to construct and update components list of edited + * bundle as well as edited page. + */ + function add_components(ul, components) + { + let components_ul = ul.work_name_input.nextElementSibling; + + for (let component of components) { + let [prefix, name] = component; + let li = component_li_template.cloneNode(true); + li.setAttribute("data-prefix", prefix); + li.setAttribute("data-name", name); + let span = li.firstElementChild; + span.textContent = `${name} (${TYPE_NAME[prefix]})`; + let remove_but = span.nextElementSibling; + remove_but.addEventListener("click", () => + components_ul.removeChild(li)); + components_ul.appendChild(li); + } + + components_ul.appendChild(ul.work_empty_component_li); + } + + /* Used to reset edited bundle as well as edited page. */ + function generic_reset_work_li(ul, item, components) + { + if (item === undefined) { + item = ""; + components = []; + }; + + ul.work_name_input.value = item; + let old_components_ul = ul.work_name_input.nextElementSibling; + let components_ul = old_components_ul.cloneNode(false); + + ul.work_li.insertBefore(components_ul, old_components_ul); + ul.work_li.removeChild(old_components_ul); + + add_components(ul, components); + } + + function reset_work_page_li(ul, item, settings) + { + ul.work_page_allow_chbx.checked = !!settings?.allow; + generic_reset_work_li(ul, item, settings?.components); + } + + /* Used to get edited bundle as well as edited page data for saving. */ + function generic_work_li_data(ul) + { + let components_ul = ul.work_name_input.nextElementSibling; + let component_li = components_ul.firstElementChild; + + let components = []; + + /* Last list element is empty li with id set. */ + while (component_li.id === '') { + components.push([component_li.getAttribute("data-prefix"), + component_li.getAttribute("data-name")]); + component_li = component_li.nextElementSibling; + } + + return [ul.work_name_input.value, components]; + } + + function work_page_li_data(ul) + { + let [url, components] = generic_work_li_data(ul); + let settings = {components, allow : !!ul.work_page_allow_chbx.checked}; + + return [url, settings]; + } + + const script_url_input = by_id("script_url_field"); + const script_sha256_input = by_id("script_sha256_field"); + const script_contents_field = by_id("script_contents_field"); + + function maybe_string(maybe_defined) + { + return maybe_defined === undefined ? "" : maybe_defined + ""; + } + + function reset_work_script_li(ul, name, data) + { + ul.work_name_input.value = maybe_string(name); + script_url_input.value = maybe_string(data?.url); + script_sha256_input.value = maybe_string(data?.hash); + script_contents_field.value = maybe_string(data?.text); + } + + function work_script_li_data(ul) + { + return [ul.work_name_input.value, { + url : script_url_input.value, + hash : script_sha256_input.value, + text : script_contents_field.value + }]; + } + + function cancel_work(prefix) + { + let ul = ul_by_prefix[prefix]; + + if (ul.state === UL_STATE.IDLE) + return; + + if (ul.state === UL_STATE.EDITING_ENTRY) { + add_li(prefix, ul.edited_item); + } + + ul.work_li.classList.add("hide"); + ul.state = UL_STATE.IDLE; + } + + function save_work(prefix) + { + let ul = ul_by_prefix[prefix]; + + if (ul.state === UL_STATE.IDLE) + return; + + let [item, data] = ul.get_work_li_data(ul); + + if (prefix == TYPE_PREFIX.PAGE) + + /* Here we fire promises and return without waiting. */ + + if (ul.state === UL_STATE.EDITING_ENTRY) + storage.replace(prefix, ul.edited_item, item, data); + + if (ul.state === UL_STATE.ADDING_ENTRY) + storage.set(prefix, item, data); + + cancel_work(prefix); + } + + function edit_item(prefix, item) + { + cancel_work(prefix); + + let ul = ul_by_prefix[prefix]; + let li = by_id(item_li_id(prefix, item)); + ul.reset_work_li(ul, item, storage.get(prefix, item)); + ul.ul.insertBefore(ul.work_li, li); + ul.ul.removeChild(li); + ul.work_li.classList.remove("hide"); + + ul.state = UL_STATE.EDITING_ENTRY; + ul.edited_item = item; + } + + function add_new_item(prefix) + { + cancel_work(prefix); + + let ul = ul_by_prefix[prefix]; + ul.reset_work_li(ul); + ul.work_li.classList.remove("hide"); + ul.ul.appendChild(ul.work_li); + + ul.state = UL_STATE.ADDING_ENTRY; + } + + const select_components_window = by_id("select_components_window"); + var select_prefix; + + function select_components(prefix) + { + select_prefix = prefix; + select_components_window.classList.remove("hide"); + + for (let li of selectable_components_ul.children) { + let chbx = li.firstElementChild; + chbx.checked = false; + } + } + + function commit_components() + { + let selected = []; + + for (let li of selectable_components_ul.children) { + let chbx = li.firstElementChild; + if (!chbx.checked) + continue; + + selected.push([li.getAttribute("data-prefix"), + li.getAttribute("data-name")]); + } + + add_components(ul_by_prefix[select_prefix], selected); + cancel_components(); + } + + function cancel_components() + { + select_components_window.classList.add("hide"); + } + + const UL_STATE = { + EDITING_ENTRY : 0, + ADDING_ENTRY : 1, + IDLE : 2 + }; + + const ul_by_prefix = { + [TYPE_PREFIX.PAGE] : { + ul : by_id("pages_ul"), + work_li : by_id("work_page_li"), + work_name_input : by_id("page_url_field"), + work_empty_component_li : by_id("empty_page_component_li"), + work_page_allow_chbx : by_id("page_allow_chbx"), + reset_work_li : reset_work_page_li, + get_work_li_data : work_page_li_data, + state : UL_STATE.IDLE, + edited_item : undefined, + }, + [TYPE_PREFIX.BUNDLE] : { + ul : by_id("bundles_ul"), + work_li : by_id("work_bundle_li"), + work_name_input : by_id("bundle_name_field"), + work_empty_component_li : by_id("empty_bundle_component_li"), + reset_work_li : generic_reset_work_li, + get_work_li_data : generic_work_li_data, + state : UL_STATE.IDLE, + edited_item : undefined, + }, + [TYPE_PREFIX.SCRIPT] : { + ul : by_id("scripts_ul"), + work_li : by_id("work_script_li"), + work_name_input : by_id("script_name_field"), + reset_work_li : reset_work_script_li, + get_work_li_data : work_script_li_data, + state : UL_STATE.IDLE, + edited_item : undefined, + } + } + + async function main() + { + storage = await get_storage(); + + for (let prefix of list_prefixes) { + for (let item of storage.get_all_names(prefix).sort()) { + add_li(prefix, item, true); + add_selectable(prefix, item); + } + } + + storage.add_change_listener(handle_change); + + let commit_components_but = by_id("commit_components_but"); + let cancel_components_but = by_id("cancel_components_but"); + commit_components_but.addEventListener("click", commit_components); + cancel_components_but.addEventListener("click", cancel_components); + + for (let prefix of list_prefixes) { + let add_but = by_id(`add_${TYPE_NAME[prefix]}_but`); + let discard_but = by_id(`${TYPE_NAME[prefix]}_discard_but`); + let save_but = by_id(`${TYPE_NAME[prefix]}_save_but`); + let select_components_but = + by_id(`${TYPE_NAME[prefix]}_select_components_but`); + + add_but.addEventListener("click", () => add_new_item(prefix)); + discard_but.addEventListener("click", () => cancel_work(prefix)); + save_but.addEventListener("click", () => save_work(prefix)); + if (select_components_but === null) + continue; + + select_components_but.addEventListener( + "click", + () => select_components(prefix) + ); + } + } + + function handle_change(change) + { + if (change.old_val === undefined) { + add_li(change.prefix, change.item); + add_selectable(change.prefix, change.item); + return; + } + + if (change.new_val !== undefined) + return; + + let ul = ul_by_prefix[change.prefix]; + if (ul.state === UL_STATE.EDITING_ENTRY && + ul.edited_item === change.item) { + ul.state = UL_STATE.ADDING_ENTRY; + return; + } + + let li = by_id(item_li_id(change.prefix, change.item)); + ul.ul.removeChild(li); + + if (change.prefix === TYPE_PREFIX.PAGE) + return; + + let sli = by_id(selectable_li_id(change.prefix, change.item)); + selectable_components_ul.removeChild(sli); + } + + main(); +})(); diff --git a/html/options_main.mjs b/html/options_main.mjs deleted file mode 100644 index f288971..0000000 --- a/html/options_main.mjs +++ /dev/null @@ -1,398 +0,0 @@ -/** -* Myext HTML options page main script -* -* Copyright (C) 2021 Wojtek Kosior -* -* Dual-licensed under: -* - 0BSD license -* - GPLv3 or (at your option) any later version -*/ - -"use strict"; - -import get_storage from '/common/storage_client.mjs'; -import {TYPE_PREFIX, TYPE_NAME, list_prefixes} from '/common/stored_types.mjs'; - -var storage; - -const item_li_template = document.getElementById("item_li_template"); -const component_li_template = document.getElementById("component_li_template"); -const selectable_component_li_template = - document.getElementById("selectable_component_li_template"); -/* Make sure they are later cloned without id. */ -item_li_template.removeAttribute("id"); -component_li_template.removeAttribute("id"); -selectable_component_li_template.removeAttribute("id"); - -function item_li_id(prefix, item) -{ - return `li_${prefix}_${item}`; -} - -function add_li(prefix, item, at_the_end=false) -{ - let ul = ul_by_prefix[prefix]; - let li = item_li_template.cloneNode(true); - li.id = item_li_id(prefix, item); - - let span = li.firstElementChild; - span.textContent = item; - - let edit_button = span.nextElementSibling; - edit_button.addEventListener("click", () => edit_item(prefix, item)); - - let remove_button = edit_button.nextElementSibling; - remove_button.addEventListener("click", () => storage.remove(prefix, item)); - - if (!at_the_end) { - for (let element of ul.ul.children) { - if (element.id < li.id || element.id.startsWith("work_")) - continue; - - ul.ul.insertBefore(li, element); - return; - } - } - - ul.ul.appendChild(li); -} - -const selectable_components_ul = - document.getElementById("selectable_components_ul"); - -function selectable_li_id(prefix, item) -{ - return `sli_${prefix}_${item}`; -} - -function add_selectable(prefix, name) -{ - if (prefix === TYPE_PREFIX.PAGE) - return; - - let li = selectable_component_li_template.cloneNode(true); - li.id = selectable_li_id(prefix, name); - li.setAttribute("data-prefix", prefix); - li.setAttribute("data-name", name); - - let chbx = li.firstElementChild; - let span = chbx.nextElementSibling; - - span.textContent = `${name} (${TYPE_NAME[prefix]})`; - - selectable_components_ul.appendChild(li); -} - -/* - * Used to construct and update components list of edited - * bundle as well as edited page. - */ -function add_components(ul, components) -{ - let components_ul = ul.work_name_input.nextElementSibling; - - for (let component of components) { - let [prefix, name] = component; - let li = component_li_template.cloneNode(true); - li.setAttribute("data-prefix", prefix); - li.setAttribute("data-name", name); - let span = li.firstElementChild; - span.textContent = `${name} (${TYPE_NAME[prefix]})`; - let remove_but = span.nextElementSibling; - remove_but.addEventListener("click", () => - components_ul.removeChild(li)); - components_ul.appendChild(li); - } - - components_ul.appendChild(ul.work_empty_component_li); -} - -/* Used to reset edited bundle as well as edited page. */ -function generic_reset_work_li(ul, item, components) -{ - if (item === undefined) { - item = ""; - components = []; - }; - - ul.work_name_input.value = item; - let old_components_ul = ul.work_name_input.nextElementSibling; - let components_ul = old_components_ul.cloneNode(false); - - ul.work_li.insertBefore(components_ul, old_components_ul); - ul.work_li.removeChild(old_components_ul); - - add_components(ul, components); -} - -function reset_work_page_li(ul, item, settings) -{ - ul.work_page_allow_chbx.checked = !!settings?.allow; - generic_reset_work_li(ul, item, settings?.components); -} - -/* Used to get edited bundle as well as edited page data for saving. */ -function generic_work_li_data(ul) -{ - let components_ul = ul.work_name_input.nextElementSibling; - let component_li = components_ul.firstElementChild; - - let components = []; - - /* Last list element is empty li with id set. */ - while (component_li.id === '') { - components.push([component_li.getAttribute("data-prefix"), - component_li.getAttribute("data-name")]); - component_li = component_li.nextElementSibling; - } - - return [ul.work_name_input.value, components]; -} - -function work_page_li_data(ul) -{ - let [url, components] = generic_work_li_data(ul); - let settings = {components, allow : !!ul.work_page_allow_chbx.checked}; - - return [url, settings]; -} - -const script_url_input = document.getElementById("script_url_field"); -const script_sha256_input = document.getElementById("script_sha256_field"); -const script_contents_field = document.getElementById("script_contents_field"); - -function maybe_string(maybe_defined) -{ - return maybe_defined === undefined ? "" : maybe_defined + ""; -} - -function reset_work_script_li(ul, name, data) -{ - ul.work_name_input.value = maybe_string(name); - script_url_input.value = maybe_string(data?.url); - script_sha256_input.value = maybe_string(data?.hash); - script_contents_field.value = maybe_string(data?.text); -} - -function work_script_li_data(ul) -{ - return [ul.work_name_input.value, { - url : script_url_input.value, - hash : script_sha256_input.value, - text : script_contents_field.value - }]; -} - -function cancel_work(prefix) -{ - let ul = ul_by_prefix[prefix]; - - if (ul.state === UL_STATE.IDLE) - return; - - if (ul.state === UL_STATE.EDITING_ENTRY) { - add_li(prefix, ul.edited_item); - } - - ul.work_li.classList.add("hide"); - ul.state = UL_STATE.IDLE; -} - -function save_work(prefix) -{ - let ul = ul_by_prefix[prefix]; - - if (ul.state === UL_STATE.IDLE) - return; - - let [item, data] = ul.get_work_li_data(ul); - - if (prefix == TYPE_PREFIX.PAGE) - - /* Here we fire promises and return without waiting. */ - - if (ul.state === UL_STATE.EDITING_ENTRY) - storage.replace(prefix, ul.edited_item, item, data); - - if (ul.state === UL_STATE.ADDING_ENTRY) - storage.set(prefix, item, data); - - cancel_work(prefix); -} - -function edit_item(prefix, item) -{ - cancel_work(prefix); - - let ul = ul_by_prefix[prefix]; - let li = document.getElementById(item_li_id(prefix, item)); - ul.reset_work_li(ul, item, storage.get(prefix, item)); - ul.ul.insertBefore(ul.work_li, li); - ul.ul.removeChild(li); - ul.work_li.classList.remove("hide"); - - ul.state = UL_STATE.EDITING_ENTRY; - ul.edited_item = item; -} - -function add_new_item(prefix) -{ - cancel_work(prefix); - - let ul = ul_by_prefix[prefix]; - ul.reset_work_li(ul); - ul.work_li.classList.remove("hide"); - ul.ul.appendChild(ul.work_li); - - ul.state = UL_STATE.ADDING_ENTRY; -} - -const select_components_window = - document.getElementById("select_components_window"); -var select_prefix; - -function select_components(prefix) -{ - select_prefix = prefix; - select_components_window.classList.remove("hide"); - - for (let li of selectable_components_ul.children) { - let chbx = li.firstElementChild; - chbx.checked = false; - } -} - -function commit_components() -{ - let selected = []; - - for (let li of selectable_components_ul.children) { - let chbx = li.firstElementChild; - if (!chbx.checked) - continue; - - selected.push([li.getAttribute("data-prefix"), - li.getAttribute("data-name")]); - } - - add_components(ul_by_prefix[select_prefix], selected); - cancel_components(); -} - -function cancel_components() -{ - select_components_window.classList.add("hide"); -} - -const UL_STATE = { - EDITING_ENTRY : 0, - ADDING_ENTRY : 1, - IDLE : 2 -}; - -const ul_by_prefix = { - [TYPE_PREFIX.PAGE] : { - ul : document.getElementById("pages_ul"), - work_li : document.getElementById("work_page_li"), - work_name_input : document.getElementById("page_url_field"), - work_empty_component_li : - document.getElementById("empty_page_component_li"), - work_page_allow_chbx : document.getElementById("page_allow_chbx"), - reset_work_li : reset_work_page_li, - get_work_li_data : work_page_li_data, - state : UL_STATE.IDLE, - edited_item : undefined, - }, - [TYPE_PREFIX.BUNDLE] : { - ul : document.getElementById("bundles_ul"), - work_li : document.getElementById("work_bundle_li"), - work_name_input : document.getElementById("bundle_name_field"), - work_empty_component_li : - document.getElementById("empty_bundle_component_li"), - reset_work_li : generic_reset_work_li, - get_work_li_data : generic_work_li_data, - state : UL_STATE.IDLE, - edited_item : undefined, - }, - [TYPE_PREFIX.SCRIPT] : { - ul : document.getElementById("scripts_ul"), - work_li : document.getElementById("work_script_li"), - work_name_input : document.getElementById("script_name_field"), - reset_work_li : reset_work_script_li, - get_work_li_data : work_script_li_data, - state : UL_STATE.IDLE, - edited_item : undefined, - } -} - -async function main() -{ - storage = await get_storage(); - - for (let prefix of list_prefixes) { - for (let item of storage.get_all_names(prefix).sort()) { - add_li(prefix, item, true); - add_selectable(prefix, item); - } - } - - storage.add_change_listener(handle_change); - - let commit_components_but = - document.getElementById("commit_components_but"); - let cancel_components_but = - document.getElementById("cancel_components_but"); - commit_components_but.addEventListener("click", commit_components); - cancel_components_but.addEventListener("click", cancel_components); - - for (let prefix of list_prefixes) { - let add_but = document.getElementById(`add_${TYPE_NAME[prefix]}_but`); - let discard_but = - document.getElementById(`${TYPE_NAME[prefix]}_discard_but`); - let save_but = document.getElementById(`${TYPE_NAME[prefix]}_save_but`); - let select_components_but = document.getElementById( - `${TYPE_NAME[prefix]}_select_components_but` - ); - - add_but.addEventListener("click", () => add_new_item(prefix)); - discard_but.addEventListener("click", () => cancel_work(prefix)); - save_but.addEventListener("click", () => save_work(prefix)); - if (select_components_but === null) - continue; - - select_components_but.addEventListener( - "click", - () => select_components(prefix) - ); - } -} - -function handle_change(change) -{ - if (change.old_val === undefined) { - add_li(change.prefix, change.item); - add_selectable(change.prefix, change.item); - return; - } - - if (change.new_val !== undefined) - return; - - let ul = ul_by_prefix[change.prefix]; - if (ul.state === UL_STATE.EDITING_ENTRY && ul.edited_item === change.item) { - ul.state = UL_STATE.ADDING_ENTRY; - return; - } - - let li = document.getElementById(item_li_id(change.prefix, change.item)); - ul.ul.removeChild(li); - - if (change.prefix === TYPE_PREFIX.PAGE) - return; - - let sli = document.getElementById(selectable_li_id(change.prefix, - change.item)); - selectable_components_ul.removeChild(sli); -} - -main(); diff --git a/manifest.json b/manifest.json index d9ff113..85dde71 100644 --- a/manifest.json +++ b/manifest.json @@ -38,10 +38,6 @@ "open_in_tab": true }, "web_accessible_resources": [ - "/common/connection_types.mjs", - "/common/once.mjs", - "/common/browser.mjs", - "/content/page_actions.mjs" ], "background": { "page": "background/background.html" @@ -53,6 +49,9 @@ "match_about_blank": true, "all_frames": true, "js": [ + "common/browser.js", + "common/connection_types.js", + "content/page_actions.js", "content/main.js" ] } -- cgit v1.2.3