Skip to content

Instantly share code, notes, and snippets.

@eshack94
Created November 24, 2024 12:30
Show Gist options
  • Save eshack94/23ed5cc2d5d22881476100624a62fb20 to your computer and use it in GitHub Desktop.
Save eshack94/23ed5cc2d5d22881476100624a62fb20 to your computer and use it in GitHub Desktop.
Simple Userscript that ensures Command+Click (or Ctrl+Click) opens links in a new tab on all websites. Can be used with ViolentMonkey or other similar extensions.
// ==UserScript==
// @name Force Command+Click to Open in New Tab (Mac, Linux, and Windows)
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Ensure Command+Click (or Ctrl+Click) opens links in a new tab on all websites. Overrides client-side JS that disables this behavior.
// @author Elijah Shackelford <eshack94>
// @match *://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
document.addEventListener('click', function(event) {
if (event.metaKey || event.ctrlKey) {
const target = event.target;
const anchor = target.closest('a');
if (anchor && anchor.href) {
event.stopImmediatePropagation();
window.open(anchor.href, '_blank');
}
}
}, true);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment