Right now Firefox shows the full URL in the title bar for visited sites. There is a browser.urlbar.trimURLs preference in about:config which hides the http:// prefix but not https://. Other browsers like Safari and Chrome hide the https:// prefix as well as www.. Is it possible to replicate this behavior in Firefox?
Asked
Active
Viewed 166 times
1
user12638282
- 61
- 4
1 Answers
2
You can add support for this in Firefox by including the following in a firefox.cfg (or equivalent) AutoConfig file:
// -*- mode: javascript; -*- vim: set ft=javascript:
// See https://support.mozilla.org/en-US/kb/customizing-firefox-using-autoconfig
"use strict";
(() => {
if (Services.appinfo.inSafeMode) {
return;
}
const addressPattern = new RegExp(
"^(chrome:(?!//(global/content/commonDialog)\\.xhtml)|about:(?!blank))"
);
const hostnameStripPattern = new RegExp("^www\\.");
Services.obs.addObserver(subject => {
subject.addEventListener(
"DOMContentLoaded",
event => {
const document = event.originalTarget;
const window = document.defaultView;
if (!addressPattern.test(window.location.href)) {
return;
}
// Hide URL scheme once editing in URL bar is finished.
const urlbarInput = document.getElementById("urlbar-input");
const updateURLHandler = () => {
if (!window.gURLBar.focused) {
const untrimmedValue = urlbarInput.value;
let url;
try {
url = new window.URL(untrimmedValue);
} catch (error) {
// Catch TypeError: URL constructor errors for invalid/partial URLs.
if (error instanceof window.TypeError) {
return;
}
throw error;
}
// Preserve existing behavior for built-in prefixes like "view-source:".
if (url.protocol !== "https:" && url.protocol !== "http:") {
return;
}
const suffix = url.pathname + url.search + url.hash;
const displayValue =
url.hostname.replace(hostnameStripPattern, "") +
(suffix === "/" ? "" : suffix);
if (displayValue !== urlbarInput.value) {
window.gURLBar.value = displayValue;
window.gURLBar._untrimmedValue = untrimmedValue;
}
}
};
urlbarInput?.addEventListener("SetURI", updateURLHandler);
urlbarInput?.addEventListener("blur", updateURLHandler);
},
{ once: true }
);
}, "chrome-document-global-created");
})();
See this answer for additional context and installation instructions.
This will recreate Chrome's behavior, where the https://[www.] prefix is removed but the suffix is retained unless it's only a /. To use Safari's behavior (which removes the suffix entirely and only shows the domain), change displayValue to url.hostname.replace(hostnameStripPattern, "").
You may also want to turn on the security.insecure_connection_text.enabled preference in about:config or user.js to show a "Not Secure" indicator in the URL bar for HTTP sites.
user_pref("security.insecure_connection_text.enabled", true);
There's an open issue for this on Bugzilla here.
user12638282
- 61
- 4
-
On Firefox 115 it doesn't work... Error in console: "ReferenceError: ChromeUtils is not defined [firefox.cfg:5:24]". Probably somehow related to this bug? https://bugzilla.mozilla.org/show_bug.cgi?id=1766114 – emvaized Jul 15 '23 at 17:15
-
1Turns out it works only if added "pref("general.config.sandbox_enabled", false)" in the prefs file – emvaized Jul 15 '23 at 17:19
-
Thanks, added a note in the original post. – user12638282 Jul 19 '23 at 21:06
-
This script has a small problem — when url bar is focused, "https://" part is back, making it not possible, for example, to use double click to highlight any word in url. Other than that, it works great! – emvaized Aug 06 '23 at 06:24