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:
39
src/helpers.js
Normal file
39
src/helpers.js
Normal 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;
|
||||
}
|
Reference in New Issue
Block a user