feat: add keyboard shortcut for copying current tab URL

This commit is contained in:
Alex Kontos
2025-11-03 18:10:12 +00:00
parent 8313f8f7f9
commit a135615752

View File

@@ -20,6 +20,7 @@ export const TabFeatures = {
PREF_ACTIVETAB: "browser.tabs.copyurl.activetab", PREF_ACTIVETAB: "browser.tabs.copyurl.activetab",
PREF_REQUIRECONFIRM: "browser.restart_menu.requireconfirm", PREF_REQUIRECONFIRM: "browser.restart_menu.requireconfirm",
PREF_PURGECACHE: "browser.restart_menu.purgecache", PREF_PURGECACHE: "browser.restart_menu.purgecache",
PREF_COPYURL_SHORTCUT: "browser.tabs.copyurl.shortcut",
init(aWindow) { init(aWindow) {
// Wait for XUL elements to be available before initializing listeners. // Wait for XUL elements to be available before initializing listeners.
@@ -35,6 +36,7 @@ export const TabFeatures = {
this.initListeners(aWindow); this.initListeners(aWindow);
this.initNewTabConfig(); // Does not require aWindow this.initNewTabConfig(); // Does not require aWindow
this.initNewTabFocus(aWindow); this.initNewTabFocus(aWindow);
this.initShortcutCopyUrl(aWindow);
}, },
destroy() { destroy() {
@@ -175,6 +177,75 @@ export const TabFeatures = {
}); });
}, },
initShortcutCopyUrl(aWindow) {
const doc = aWindow.document;
const handler = (e) => {
try {
// Allow disabling via pref; default to enabled if unset
if (!Services.prefs.getBoolPref(this.PREF_COPYURL_SHORTCUT, true)) {
return;
}
const isMac = AppConstants.platform === "macosx";
const accelPressed = isMac ? e.metaKey : e.ctrlKey;
if (!accelPressed || !e.shiftKey || e.key?.toLowerCase() !== "u") {
return;
}
const url = aWindow.gBrowser?.currentURI?.spec;
if (!url) {
return;
}
// Prevent default so we don't trigger other shortcuts
e.preventDefault();
e.stopPropagation();
this.copyTabUrl(url, aWindow);
this._showCopyNotification(url, aWindow);
} catch (err) {
console.error("TabFeatures: shortcut copy failed", err);
}
};
// Capture phase to beat page-level handlers
doc.addEventListener("keydown", handler, true);
aWindow.addEventListener(
"unload",
() => {
try {
doc.removeEventListener("keydown", handler, true);
} catch (_) {}
},
{ once: true }
);
},
_showCopyNotification(url, aWindow) {
try {
const alerts = Cc["@mozilla.org/alerts-service;1"].getService(
Ci.nsIAlertsService
);
const title = "🔗 URL Copied";
const text = url.length > 50 ? url.substring(0, 47) + "..." : url;
alerts.showAlertNotification(
null,
title,
text,
false,
"",
null,
"tabfeatures-copyurl"
);
} catch (e) {
console.log("URL copied to clipboard:", url);
}
},
destroyNewTabConfig() { destroyNewTabConfig() {
if (this.prefListener) { if (this.prefListener) {
Services.prefs.removeObserver( Services.prefs.removeObserver(