Files
browserpass-extension/src/helpers.js
Maxim Baz 949e8fe0da Extract helpers.js (#130)
* Extract helpers.js
* Rebuild background.dist.js and popup.dist.js when helpers change
* Cleanup require definitions
2019-04-21 14:41:14 +12:00

40 lines
1018 B
JavaScript

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