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

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