feat: prevent middle-click link open (option) (#690)

* feat: add NoMiddleOpen plugin

* feat: extend LimitMiddleClickPaste
This commit is contained in:
korzi 2026-01-27 03:20:53 +05:00 • committed by GitHub
parent 0c5c6a8662
commit 1e0f7b38f9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 41 additions and 2 deletions

View file

@ -34,12 +34,43 @@ const settings = definePluginSettings({
default: 100,
onChange(newValue) { if (newValue < 1) { settings.store.threshold = 1; } },
},
preventLinkOpen: {
type: OptionType.BOOLEAN,
description: "Prevent middle-click on links from opening new tabs while preserving autoscroll.",
default: false,
onChange(newValue) {
if (newValue) {
document.addEventListener("auxclick", handleAuxClick, true);
} else {
document.removeEventListener("auxclick", handleAuxClick, true);
}
},
},
});
function shouldBlockLink(e: MouseEvent): boolean {
if (e.button !== MIDDLE_CLICK) return false;
const target = e.target as HTMLElement | null;
if (!target?.closest) return false;
const a = target.closest("a[href]") as HTMLAnchorElement | null;
if (!a) return false;
const href = a.getAttribute("href");
return !!href && href !== "#";
}
function handleAuxClick(e: MouseEvent) {
if (!shouldBlockLink(e)) return;
e.preventDefault();
e.stopPropagation();
}
export default definePlugin({
name: "LimitMiddleClickPaste",
description: "Prevent middle click pasting either always or just when a text area is not focused.",
authors: [EquicordDevs.Etorix],
description: "Prevent middle click pasting either always or just when a text area is not focused. Optionally prevent middle-click on links from opening new tabs.",
authors: [EquicordDevs.Etorix, EquicordDevs.korzi],
settings,
isPastingDisabled(isInput: boolean) {
@ -59,10 +90,14 @@ export default definePlugin({
start() {
document.addEventListener("mouseup", this.onMouseUp);
if (settings.store.preventLinkOpen) {
document.addEventListener("auxclick", handleAuxClick, true);
}
},
stop() {
document.removeEventListener("mouseup", this.onMouseUp);
document.removeEventListener("auxclick", handleAuxClick, true);
},
patches: [

View file

@ -1249,6 +1249,10 @@ export const EquicordDevs = Object.freeze({
name: "BioTomateDE",
id: 553499669226061844n,
},
korzi: {
name: "korzi",
id: 740966310875365416n,
},
} satisfies Record<string, Dev>);
// iife so #__PURE__ works correctly