Skip to content

Instantly share code, notes, and snippets.

@gabrielmoreira
Last active February 6, 2026 15:31
Show Gist options
  • Select an option

  • Save gabrielmoreira/1db136539ca71ae132d7948496343049 to your computer and use it in GitHub Desktop.

Select an option

Save gabrielmoreira/1db136539ca71ae132d7948496343049 to your computer and use it in GitHub Desktop.
Userscript to prevent unintended ad link openings on x.com when using mouse back/forward buttons.

X.com block mouse back/forward ad clicks

This userscript fixes a very "unintended" behavior on x.com where pressing the Back or Forward buttons on a mouse over sponsored posts can open advertiser links in a new tab without an actual, intentional click.

The problem

Some sponsored posts on x.com (for example, ads showing From didit.me) attach click and mouseup handlers that do not properly check which mouse button triggered the event.

As a result, pressing the mouse Back button (button 3) while the pointer is over an ad image or video is treated as a valid click, causing x.com to call window.open() and open the advertiser link in a new tab.

Whether this behavior is a genuine oversight or a creative interpretation of what counts as a click, the end result is the same: accidental ad clicks and inflated engagement.

What this script does

  • Intercepts mouse and pointer events in the capture phase
  • Blocks events generated by mouse Back and Forward buttons (buttons 3 and 4)
  • Prevents those events from reaching x.com ad and media click handlers
  • Stops surprise window.open() calls on sponsored posts

What this script does NOT do

  • It does not modify page content
  • It does not remove or hide ads
  • It does not add tracking or analytics
  • It does not break normal browser Back/Forward navigation

Why a userscript

The issue is narrowly scoped but highly annoying. A small userscript is the most precise and least invasive way to opt out of this particular definition of an "engaged click" while keeping the rest of the site working normally.

Installation

  1. Install a userscript manager such as Tampermonkey.
  2. Open the raw .user.js file from this gist.
  3. Tampermonkey will detect the script and prompt you to install it.
  4. Confirm the installation and reload https://x.com.

The script runs automatically on page load and requires no configuration.

Tested behavior

  • Chrome / Chromium-based browsers
  • Tampermonkey
  • Physical mouse with Back/Forward buttons

If you use a mouse with navigation buttons and browse x.com regularly, this script helps ensure that clicks are clicks, and navigation is just navigation.

// ==UserScript==
// @name X.com block mouse back/forward ad clicks
// @namespace https://gist.github.com/gabrielmoreira
// @version 1.0.9
// @description Prevents x.com sponsored posts from opening a new tab when using mouse Back/Forward buttons, while preserving normal browser navigation.
// @author gabrielmoreira
// @match https://x.com/*
// @run-at document-start
// @grant none
// @noframes
// ==/UserScript==
(function () {
'use strict';
const LOG_PREFIX = '[X.com block mouse back/forward ad clicks]';
const BLOCK_MS = 700;
const DEBUG = Boolean(window.__X_MOUSE_NAV_GUARD_DEBUG__);
function debug(...args) {
if (DEBUG) console.debug(LOG_PREFIX, ...args);
}
function info(...args) {
console.info(LOG_PREFIX, ...args);
}
info('Initialized');
let blockUntil = 0;
let lastButton = null;
function armBlock(e, src) {
if (!e) return;
if (e.button === 3 || e.button === 4) {
blockUntil = Date.now() + BLOCK_MS;
lastButton = e.button;
debug('Mouse navigation button detected', {
sourceEvent: src,
button: e.button,
blockWindowMs: BLOCK_MS
});
}
}
['pointerdown','pointerup','mousedown','mouseup','auxclick'].forEach(type => {
window.addEventListener(type, e => armBlock(e, type), true);
document.addEventListener(type, e => armBlock(e, type), true);
});
function shouldBlockNow() {
return Date.now() < blockUntil;
}
const originalOpen = window.open;
window.open = function (...args) {
const url = args && args[0];
if (shouldBlockNow()) {
info('Blocked window.open during mouse nav button window', {
button: lastButton,
url: url || null
});
return null;
}
return originalOpen.apply(this, args);
};
document.addEventListener('click', function (e) {
if (!shouldBlockNow()) return;
const t = e.target;
if (!t || !t.closest) return;
const a = t.closest('a[target="_blank"][href]');
if (!a) return;
e.preventDefault();
e.stopPropagation();
if (typeof e.stopImmediatePropagation === 'function') {
e.stopImmediatePropagation();
}
info('Blocked target=_blank navigation during mouse nav button window', {
button: lastButton,
url: a.href || a.getAttribute('href')
});
}, true);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment