From 949e8fe0da0b770c8d7f3687c7ffefef3b419dc9 Mon Sep 17 00:00:00 2001 From: Maxim Baz Date: Sun, 21 Apr 2019 04:41:14 +0200 Subject: [PATCH] Extract helpers.js (#130) * Extract helpers.js * Rebuild background.dist.js and popup.dist.js when helpers change * Cleanup require definitions --- Makefile | 1 - src/Makefile | 6 +++--- src/background.js | 37 ++++------------------------------ src/helpers.js | 39 ++++++++++++++++++++++++++++++++++++ src/options/interface.js | 2 +- src/options/options.js | 2 +- src/popup/interface.js | 8 ++++---- src/popup/popup.js | 37 ++++------------------------------ src/popup/searchinterface.js | 2 +- 9 files changed, 57 insertions(+), 77 deletions(-) create mode 100644 src/helpers.js diff --git a/Makefile b/Makefile index 153fef6..c3c8bd2 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,6 @@ extension: $(MAKE) -C src EXTENSION_FILES := \ - src/*.css \ src/*.png \ src/*.svg \ src/fonts/* \ diff --git a/src/Makefile b/src/Makefile index 881dab8..ede36b9 100644 --- a/src/Makefile +++ b/src/Makefile @@ -2,7 +2,7 @@ BROWSERIFY := node_modules/.bin/browserify PRETTIER := node_modules/.bin/prettier LESSC := node_modules/.bin/lessc -CLEAN_FILES := js +CLEAN_FILES := css js PRETTIER_FILES := $(wildcard *.json *.js popup/*.js options/*.js *.less popup/*.less options/*.less *.html popup/*.html options/*.html) .PHONY: all @@ -24,11 +24,11 @@ css/options.dist.css: $(LESSC) options/options.less [ -d css ] || mkdir -p css $(LESSC) options/options.less css/options.dist.css -js/background.dist.js: $(BROWSERIFY) background.js +js/background.dist.js: $(BROWSERIFY) background.js helpers.js [ -d js ] || mkdir -p js $(BROWSERIFY) -o js/background.dist.js background.js -js/popup.dist.js: $(BROWSERIFY) popup/*.js +js/popup.dist.js: $(BROWSERIFY) popup/*.js helpers.js [ -d js ] || mkdir -p js $(BROWSERIFY) -o js/popup.dist.js popup/popup.js diff --git a/src/background.js b/src/background.js index 40ab26c..5113f22 100644 --- a/src/background.js +++ b/src/background.js @@ -2,9 +2,9 @@ "use strict"; require("chrome-extension-async"); -var TldJS = require("tldjs"); -var sha1 = require("sha1"); -var idb = require("idb"); +const sha1 = require("sha1"); +const idb = require("idb"); +const helpers = require("./helpers"); // native application id var appID = "com.github.browserpass.native"; @@ -61,35 +61,6 @@ chrome.runtime.onInstalled.addListener(onExtensionInstalled); //----------------------------------- Function definitions ----------------------------------// -/** - * Get the deepest available domain component of a path - * - * @since 3.0.0 - * - * @param string path Path to parse - * @param string currentHost Current hostname for the active tab - * @return string|null Extracted domain - */ -function pathToDomain(path, currentHost) { - var parts = path.split(/\//).reverse(); - for (var key in parts) { - if (parts[key].indexOf("@") >= 0) { - continue; - } - var t = TldJS.parse(parts[key]); - if ( - t.isValid && - ((t.tldExists && t.domain !== null) || - t.hostname === currentHost || - currentHost.endsWith(`.${t.hostname}`)) - ) { - return t.hostname; - } - } - - return null; -} - /** * Set badge text with the number of matching password entries * @@ -119,7 +90,7 @@ async function updateMatchingPasswordsCount(tabId) { for (var storeId in response.data.files) { for (var key in response.data.files[storeId]) { const login = response.data.files[storeId][key].replace(/\.gpg$/i, ""); - const domain = pathToDomain(storeId + "/" + login, currentDomain); + const domain = helpers.pathToDomain(storeId + "/" + login, currentDomain); const inCurrentDomain = currentDomain === domain || currentDomain.endsWith("." + domain); const recent = settings.recent[sha1(currentDomain + sha1(storeId + sha1(login)))]; diff --git a/src/helpers.js b/src/helpers.js new file mode 100644 index 0000000..78ebba9 --- /dev/null +++ b/src/helpers.js @@ -0,0 +1,39 @@ +//------------------------------------- Initialisation --------------------------------------// +"use strict"; + +const TldJS = require("tldjs"); + +module.exports = { + pathToDomain +}; + +//----------------------------------- Function definitions ----------------------------------// + +/** + * Get the deepest available domain component of a path + * + * @since 3.0.0 + * + * @param string path Path to parse + * @param string currentHost Current hostname for the active tab + * @return string|null Extracted domain + */ +function pathToDomain(path, currentHost) { + var parts = path.split(/\//).reverse(); + for (var key in parts) { + if (parts[key].indexOf("@") >= 0) { + continue; + } + var t = TldJS.parse(parts[key]); + if ( + t.isValid && + ((t.tldExists && t.domain !== null) || + t.hostname === currentHost || + currentHost.endsWith(`.${t.hostname}`)) + ) { + return t.hostname; + } + } + + return null; +} diff --git a/src/options/interface.js b/src/options/interface.js index 8921bda..9a70233 100644 --- a/src/options/interface.js +++ b/src/options/interface.js @@ -1,6 +1,6 @@ module.exports = Interface; -var m = require("mithril"); +const m = require("mithril"); /** * Options main interface diff --git a/src/options/options.js b/src/options/options.js index 8e1b6df..0a9c622 100644 --- a/src/options/options.js +++ b/src/options/options.js @@ -2,7 +2,7 @@ "use strict"; require("chrome-extension-async"); -var Interface = require("./interface"); +const Interface = require("./interface"); run(); diff --git a/src/popup/interface.js b/src/popup/interface.js index c9c377b..937d1a1 100644 --- a/src/popup/interface.js +++ b/src/popup/interface.js @@ -1,9 +1,9 @@ module.exports = Interface; -var m = require("mithril"); -var FuzzySort = require("fuzzysort"); -var Moment = require("moment"); -var SearchInterface = require("./searchinterface"); +const m = require("mithril"); +const FuzzySort = require("fuzzysort"); +const Moment = require("moment"); +const SearchInterface = require("./searchinterface"); const LATEST_NATIVE_APP_VERSION = 3000003; diff --git a/src/popup/popup.js b/src/popup/popup.js index eda6af7..c7de0bd 100644 --- a/src/popup/popup.js +++ b/src/popup/popup.js @@ -2,9 +2,9 @@ "use strict"; require("chrome-extension-async"); -var TldJS = require("tldjs"); -var sha1 = require("sha1"); -var Interface = require("./interface"); +const sha1 = require("sha1"); +const Interface = require("./interface"); +const helpers = require("../helpers"); run(); @@ -29,35 +29,6 @@ function handleError(error, type = "error") { document.body.appendChild(errorNode); } -/** - * Get the deepest available domain component of a path - * - * @since 3.0.0 - * - * @param string path Path to parse - * @param string currentHost Current hostname for the active tab - * @return string|null Extracted domain - */ -function pathToDomain(path, currentHost) { - var parts = path.split(/\//).reverse(); - for (var key in parts) { - if (parts[key].indexOf("@") >= 0) { - continue; - } - var t = TldJS.parse(parts[key]); - if ( - t.isValid && - ((t.tldExists && t.domain !== null) || - t.hostname === currentHost || - currentHost.endsWith(`.${t.hostname}`)) - ) { - return t.hostname; - } - } - - return null; -} - /** * Run the main popup logic * @@ -98,7 +69,7 @@ async function run() { login: response.files[storeId][key].replace(/\.gpg$/i, ""), allowFill: true }; - login.domain = pathToDomain(storeId + "/" + login.login, settings.host); + login.domain = helpers.pathToDomain(storeId + "/" + login.login, settings.host); login.inCurrentDomain = settings.host == login.domain || settings.host.endsWith("." + login.domain); login.recent = diff --git a/src/popup/searchinterface.js b/src/popup/searchinterface.js index 41a2b48..28ff5bb 100644 --- a/src/popup/searchinterface.js +++ b/src/popup/searchinterface.js @@ -1,6 +1,6 @@ module.exports = SearchInterface; -var m = require("mithril"); +const m = require("mithril"); /** * Search interface