Skip to content

Instantly share code, notes, and snippets.

@jaredhirsch
Created January 26, 2019 00:36
Show Gist options
  • Save jaredhirsch/60f85a1dded5589ca05ee7db9aedf9ea to your computer and use it in GitHub Desktop.
Save jaredhirsch/60f85a1dded5589ca05ee7db9aedf9ea to your computer and use it in GitHub Desktop.
Converting hostname to login title: the PSL approach
commit d67700eadc05178d275d526bd696f2dacaf83221
Author: Jared Hirsch <[email protected]>
Date: Tue Jan 22 16:21:23 2019 -0800
Add Logins API utility method to get domain name, minus TLD, from a URL
diff --git a/src/experiments/logins/api.js b/src/experiments/logins/api.js
index 15dbf53..71412a2 100644
--- a/src/experiments/logins/api.js
+++ b/src/experiments/logins/api.js
@@ -128,6 +128,23 @@ this.logins = class extends ExtensionAPI {
throw new ExtensionError(ex);
}
},
+ getDomain(url) {
+ let uri, domain;
+ try {
+ uri = Services.io.newURI(url);
+ } catch (ex) {
+ throw new ExtensionError(`getDomain failed: could not create nsIURI from ${url}: ${ex.message}`);
+ }
+ try {
+ const baseDomain = Services.eTLD.getBaseDomain(uri);
+ const suffix = Services.eTLD.getPublicSuffix(uri);
+ // Subtract the suffix ("com") plus one char for the leading dot.
+ domain = baseDomain.substring(0, baseDomain.length - (suffix.length + 1));
+ } catch (ex) {
+ throw new ExtensionError(`getDomain failed: could not extract domain from ${url}: ${ex.message}`);
+ }
+ return domain;
+ },
onAdded: new EventManager(context, "logins.onAdded", fire => {
const callback = (value) => {
fire.async(value);
diff --git a/src/experiments/logins/schema.json b/src/experiments/logins/schema.json
index 9be90f0..0469468 100644
--- a/src/experiments/logins/schema.json
+++ b/src/experiments/logins/schema.json
@@ -260,6 +260,17 @@
"type": "string",
"description": "The ID of the Login to be removed."
}]
+ },
+ {
+ "name": "getDomain",
+ "type": "function",
+ "description": "Given a URL as a string, strips out the TLD and returns just the base domain. Throws if the input URL is invalid.",
+ "async": true,
+ "parameters": [{
+ "name": "url",
+ "type": "string",
+ "description": "The URL to be parsed."
+ }]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment