Extract helpers.js (#130)

* Extract helpers.js
* Rebuild background.dist.js and popup.dist.js when helpers change
* Cleanup require definitions
This commit is contained in:
Maxim Baz 2019-04-21 04:41:14 +02:00 committed by Erayd
parent 2ed6d47c07
commit 949e8fe0da
9 changed files with 57 additions and 77 deletions

View File

@ -14,7 +14,6 @@ extension:
$(MAKE) -C src
EXTENSION_FILES := \
src/*.css \
src/*.png \
src/*.svg \
src/fonts/* \

View File

@ -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

View File

@ -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)))];

39
src/helpers.js Normal file
View File

@ -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;
}

View File

@ -1,6 +1,6 @@
module.exports = Interface;
var m = require("mithril");
const m = require("mithril");
/**
* Options main interface

View File

@ -2,7 +2,7 @@
"use strict";
require("chrome-extension-async");
var Interface = require("./interface");
const Interface = require("./interface");
run();

View File

@ -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;

View File

@ -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 =

View File

@ -1,6 +1,6 @@
module.exports = SearchInterface;
var m = require("mithril");
const m = require("mithril");
/**
* Search interface