diff --git a/src/helpers/base.js b/src/helpers/base.js index 6b888adc..6284c162 100644 --- a/src/helpers/base.js +++ b/src/helpers/base.js @@ -148,6 +148,7 @@ function parseAuthUrl(url) { return null; } + /** * Prepare list of logins based on provided files * diff --git a/src/helpers/ui.js b/src/helpers/ui.js index 2c936bd7..ce1aee7b 100644 --- a/src/helpers/ui.js +++ b/src/helpers/ui.js @@ -5,6 +5,7 @@ const m = require("mithril"); const dialog = require("../popup/modalDialog"); const notify = require("../popup/notifications"); const helpers = require("../helpers/base"); +const BrowserpassURL = require("@browserpass/url"); const containsNumbersRegEx = RegExp(/[0-9]/); const containsSymbolsRegEx = RegExp(/[\p{P}\p{S}]/, "u"); @@ -13,6 +14,7 @@ module.exports = { handleError, highlight, withLogin, + getCurrentUrl }; //----------------------------------- Function definitions ----------------------------------// @@ -152,3 +154,19 @@ async function withLogin(action, params = {}) { handleError(e); } } + +/** + * Returns current url + * @param object settings Settings object to use + * @returns object Instance of BrowserpassURL + */ +function getCurrentUrl(settings) { + let url; + const authUrl = helpers.parseAuthUrl(window?.location?.href ?? null); + if (settings.authRequested && authUrl) { + url = new BrowserpassURL(authUrl); + } else { + url = new BrowserpassURL(settings.origin); + } + return url +} diff --git a/src/popup/addEditInterface.js b/src/popup/addEditInterface.js index 1a988929..876a5248 100644 --- a/src/popup/addEditInterface.js +++ b/src/popup/addEditInterface.js @@ -284,6 +284,11 @@ function AddEditInterface(settingsModel) { } else { // view instance should be a Login loginObj = new Login(settings); + + const url = helpersUI.getCurrentUrl(settings); + + // prefill the host of the current tab + loginObj.login = url.isValid ? url.hostname : ""; } // set the storePath and get tree dirs diff --git a/src/popup/searchinterface.js b/src/popup/searchinterface.js index 4b9c7723..02a3337a 100644 --- a/src/popup/searchinterface.js +++ b/src/popup/searchinterface.js @@ -3,6 +3,7 @@ module.exports = SearchInterface; const BrowserpassURL = require("@browserpass/url"); const dialog = require("./modalDialog"); const helpers = require("../helpers/base"); +const helpersUI = require("../helpers/ui"); const m = require("mithril"); /** @@ -33,15 +34,8 @@ function SearchInterface(popup) { function view(ctl, params) { var self = this; - let url = ""; - const authUrl = helpers.parseAuthUrl(window?.location?.href ?? null); - if (this.popup.settings.authRequested && authUrl) { - url = new BrowserpassURL(authUrl); - } else { - url = new BrowserpassURL(this.popup.settings.origin); - } - - const host = url.hostname; + const url = helpersUI.getCurrentUrl(this.popup.settings); + const host = url.isValid ? url.hostname : ""; return m( "form.part.search",