Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Browser Console Script to Update Copy Link accelerator key to A (which also can be used for other context menu hotkeys)
/* Script for the Browser Console
NOTE: BEFORE RUNNING THIS SCRIPT, CHECK THIS SETTING:
Type or paste about:config into the address bar and press Enter
Click the button promising to be careful
In the search box paste devtools.chrome.enabled
If the preference is false, double-click it to toggle to true
Paste this entire script into the command line at the bottom of the Browser Console (Windows: Ctrl+Shift+j)
Then press Enter to run the script.
*/
(function(){
// Define an array of key change objects (so far, only one)
var myKeyChanges = [
{
id: 'context-copylink',
newkey: 'a',
newlabel: 'Copy Link Location'
},
{
id: 'context-copyemail',
newkey: 'A',
newlabel: 'Copy Email Address'
}
];
// Current window
for (var i=0; i<myKeyChanges.length; i++){
var menuitem = gBrowser.ownerDocument.getElementById(myKeyChanges[i].id);
if (menuitem){
if (myKeyChanges[i].newkey.length == 1){
menuitem.setAttribute('accesskey', myKeyChanges[i].newkey);
}
if (myKeyChanges[i].newlabel.length > 0){
menuitem.setAttribute('label', myKeyChanges[i].newlabel);
}
}
}
// All future windows -- based largely on
// https://www.reddit.com/r/firefox/comments/kilmm2/restore_ctrlshiftb_library_by_setting_configjs/
var acceleratorUpdater = {
observe: function (aSubject) {
aSubject.addEventListener('DOMContentLoaded', this, {once: true});
},
handleEvent: function (aEvent) {
let document = aEvent.originalTarget;
let window = document.defaultView;
let location = window.location;
if (/^(chrome:(?!\/\/(global\/content\/commonDialog|browser\/content\/webext-panels)\.x?html)|about:(?!blank))/i.test(location.href)) {
if (window._gBrowser) {
for (var i=0; i<myKeyChanges.length; i++){
var menuitem = window.document.getElementById(myKeyChanges[i].id);
if (menuitem){
if (myKeyChanges[i].newkey.length == 1){
menuitem.setAttribute('accesskey', myKeyChanges[i].newkey);
}
if (myKeyChanges[i].newlabel.length > 0){
menuitem.setAttribute('label', myKeyChanges[i].newlabel);
}
}
}
}
}
}
}
Services.obs.addObserver(acceleratorUpdater, "chrome-document-global-created", false);
})();
@jscher2000
Copy link
Author

jscher2000 commented Apr 25, 2021

If you prefer W for Open Link in New Window you could add that:

	var myKeyChanges = [
		{
			id: 'context-copylink', 
			newkey: 'a',
			newlabel: 'Copy Link Location'
		},
		{
			id: 'context-copyemail', 
			newkey: 'A',
			newlabel: 'Copy Email Address'
		},
		{
			id: 'main-context-menu-open-link-new-window',
			newkey: 'W',
			newlabel: 'Open Link in New Window'
		}
	];

@briancblog
Copy link

briancblog commented Apr 27, 2021

Thanks! It works on Firefox 88!

@briancblog
Copy link

briancblog commented May 6, 2021

I need to run this script every time I restart my firefox. How to make it permanent or run automatically when restarting firefox, please?

@jscher2000
Copy link
Author

jscher2000 commented May 6, 2021

Hi Brian, if you start from the steps in the following thread --

https://www.reddit.com/r/firefox/comments/kilmm2/restore_ctrlshiftb_library_by_setting_configjs/

-- the code to put inside the ///// section is:

		var myKeyChanges = [
			{
				id: 'context-copylink', 
				newkey: 'a',
				newlabel: 'Copy Link Location'
			},
			{
				id: 'context-copyemail', 
				newkey: 'A',
				newlabel: 'Copy Email Address'
			},
			{
				id: 'main-context-menu-open-link-new-window',
				newkey: 'W',
				newlabel: 'Open Link in New Window'
			}
		];
		for (var i=0; i<myKeyChanges.length; i++){
			var menuitem = window.document.getElementById(myKeyChanges[i].id);
			if (menuitem){
				if (myKeyChanges[i].newkey.length == 1){
					menuitem.setAttribute('accesskey', myKeyChanges[i].newkey);
				}
				if (myKeyChanges[i].newlabel.length > 0){
					menuitem.setAttribute('label', myKeyChanges[i].newlabel);
				}
			}
		}

@jscher2000
Copy link
Author

jscher2000 commented May 31, 2021

I've added a downloadable zip file to this section:

https://www.userchrome.org/what-is-userchrome-js.html#combinedloader

@b3n10
Copy link

b3n10 commented Jul 25, 2021

I guess this only works in Windows? any Linux user here can help me out? I'm getting this error when running from the console:

 Uncaught ReferenceError: gBrowser is not defined
    <anonymous> debugger eval code:17
    <anonymous> debugger eval code:55

I also followed the steps and copied the scripts to config.js (as well as created the config-pref.js) which I placed in my user profile directory:

/home/<user>/.mozilla/firefox/<profile-name>/chrome/

but I still don't get the Copy Link Address on my right-click menu even after restarting Firefox.

@jscher2000
Copy link
Author

jscher2000 commented Jul 25, 2021

I'm getting this error when running from the console:

 Uncaught ReferenceError: gBrowser is not defined
    <anonymous> debugger eval code:17
    <anonymous> debugger eval code:55

Make sure you are using the Browser Console and not the in-tab Web Console. https://developer.mozilla.org/docs/Tools/Browser_Console

I also followed the steps and copied the scripts to config.js (as well as created the config-pref.js) which I placed in my user profile directory:

/home/<user>/.mozilla/firefox/<profile-name>/chrome/

but I still don't get the Copy Link Address on my right-click menu even after restarting Firefox.

You are using these files? https://www.userchrome.org/what-is-userchrome-js.html#combinedloader

The [program folder] referenced in the script is the directory where Firefox's program files are installed -- not profile specific. One way to discover that directory would be on the Troubleshooting Information page (about:support), in the first table, where the Application Binary is installed.

@b3n10
Copy link

b3n10 commented Jul 25, 2021

Make sure you are using the Browser Console and not the in-tab Web Console

I've been using Firefox since I started learning how to code (back in college?) but I just learned today that the Browser Console (CTRL+SHIFT+J) is not the same as in-tab Web Console (CTRL+SHIFT+I).

The [program folder] referenced in the script is the directory where Firefox's program files are installed -- not profile specific.

Thanks for clarifying. So what I found out, in Linux this path is located in /usr/lib/firefox (added files in /usr/lib/firefox/defaults/pref)

EDIT:
was getting parse error and i realized the config.js was on the wrong folder. Fixed by following steps above:

  • config-prefs.js is located in /usr/lib/firefox/defaults/pref/
  • config.js is in /usr/lib/firefox/

Followed your steps and it's working correctly now for me

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment