Skip to content

Commit

Permalink
Add module to extract the domain name from a given hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
sblask committed Jul 17, 2017
1 parent 82b419a commit b17aca7
Show file tree
Hide file tree
Showing 6 changed files with 8,466 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/node_modules/
/*.zip
/node_modules/
/public_suffix_list.dat
46 changes: 46 additions & 0 deletions bin/generate-pslrules
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

set -o errexit -o nounset -o pipefail -o xtrace

SCRIPT_DIRECTORY=$( cd "$( dirname "${BASH_SOURCE:-$0}" )" && pwd )
ROOT_DIRECTORY=$(realpath $SCRIPT_DIRECTORY/../)

PSL_FILE="public_suffix_list.dat"
PSL_RULES="pslrules.js"
PSL_FILE_PATH=$ROOT_DIRECTORY/$PSL_FILE
PSL_RULES_PATH=$ROOT_DIRECTORY/$PSL_RULES

if [ ! -f $PSL_FILE_PATH ]; then
wget --directory-prefix $ROOT_DIRECTORY https://publicsuffix.org/list/$PSL_FILE
fi

CONTENT=$(cat << END
const pslrules = (function(root) { // eslint-disable-line no-unused-vars
const EXCEPTION_ENTRIES = new Set([
$(cat $PSL_FILE_PATH | grep '^!' | sed -e 's/^!//' | awk '{print $1}' | sed -e 's/\(.*\)/ "\1",/')
]);
const WILDCARD_ENTRIES = new Set([
$(cat $PSL_FILE_PATH | grep '^*.' | sed -e 's/^\*\.//' | awk '{print $1}' | sed -e 's/\(.*\)/ "\1",/')
]);
const NORMAL_ENTRIES = new Set([
$(cat $PSL_FILE_PATH | grep -v '^//' | grep -v '^$' | grep -v '^!' | grep -v '^*.' | awk '{print $1}' | sed -e 's/\(.*\)/ "\1",/')
]);
root.EXCEPTION_ENTRIES = EXCEPTION_ENTRIES;
root.WILDCARD_ENTRIES = WILDCARD_ENTRIES;
root.NORMAL_ENTRIES = NORMAL_ENTRIES;
return {
EXCEPTION_ENTRIES: EXCEPTION_ENTRIES,
WILDCARD_ENTRIES: WILDCARD_ENTRIES,
NORMAL_ENTRIES: NORMAL_ENTRIES,
};
})(this);
END
)

echo "$CONTENT" > $PSL_RULES_PATH
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"eslint": "4.1.x",
"jscs": "2.6.x",
"tape": "4.6.x",
"tape-benchmark": "0.0.0",
"web-ext": "1.9.x"
},
"license": "MIT",
Expand Down Expand Up @@ -31,8 +32,8 @@
"start": "GTK_THEME=Greybird web-ext run --verbose --firefox firefox-trunk --url $(jq --raw-output '.homepage_url' manifest.json)",
"start:german": "GTK_THEME=Greybird web-ext run --verbose --firefox firefox --pref=general.useragent.locale=de-DE --pref=intl.locale.matchOS=false --url $(jq --raw-output '.homepage_url' manifest.json)",
"start:stable": "GTK_THEME=Greybird web-ext run --verbose --firefox firefox --url $(jq --raw-output '.homepage_url' manifest.json)",
"translation-check": "./bin/translation-check",
"test": "web-ext lint && tape $(find . -not -path '*node_modules*' -name 'test-*.js') && npm run translation-check"
"test": "web-ext lint && tape $(find . -not -path '*node_modules*' -name 'test-*.js') && npm run translation-check",
"translation-check": "./bin/translation-check"
},
"title": "Skip Redirect",
"version": "2.1.7"
Expand Down
41 changes: 41 additions & 0 deletions psl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* global pslrules */
const psl = (function(root) { // eslint-disable-line no-unused-vars

if (typeof pslrules === "undefined") {
pslrules = require("./pslrules"); // eslint-disable-line no-global-assign,no-unused-vars
}

function getDomain(hostname, previousHead=undefined) {
if (!hostname) {
return undefined;
}

if (pslrules.EXCEPTION_ENTRIES.has(hostname)) {
return hostname;
}

let dotIndex = hostname.indexOf(".");
if (dotIndex == -1) {
return undefined;
}

let head = hostname.slice(0, dotIndex);
let rest = hostname.slice(dotIndex + 1);

if (pslrules.WILDCARD_ENTRIES.has(rest) && previousHead) {
return [previousHead, head, rest].join(".");
}
if (pslrules.NORMAL_ENTRIES.has(rest)) {
return [head, rest].join(".");
}

return getDomain(rest, head);
}

root.getDomain = getDomain;

return {
getDomain: getDomain,
};

})(this);

0 comments on commit b17aca7

Please sign in to comment.