Right-Click Unblocker

v1.0.0 · updated

Restores the native right-click menu, text selection and image dragging on sites that block them.

Install Download .user.js Requires Violentmonkey or Tampermonkey.
Runs on *://*/*

Some sites disable the context menu, text selection or image dragging with JavaScript and CSS. This script runs at document-start on every page and undoes that, without breaking normal clicks:

  • Context menu: capture-phase listeners stop the page’s own contextmenu, selectstart and dragstart handlers. The browser’s default action still runs, so the native menu opens.
  • Right-button handlers: only mousedown/mouseup with button === 2 are intercepted. Left and middle clicks still reach the page.
  • Inline handlers: oncontextmenu, onselectstart and ondragstart attributes (and draggable="false" on images) are stripped, including from elements added later, which a MutationObserver watches for.
  • Text selection & dragging: a stylesheet forces user-select: text and re-enables dragging of images, videos and canvases. Elements locked with inline user-select: none get an inline override, because a stylesheet can’t beat that.

No @grants and no network access. The script only touches the page it runs on.

Source

// ==UserScript==
// @name         Right-Click Unblocker
// @namespace    violentmonkey-scripts
// @version      1.0.0
// @description  Restores the native right-click menu, text selection and image dragging on sites that block them.
// @match        *://*/*
// @run-at       document-start
// @grant        none
// ==/UserScript==

(() => {
  'use strict';

  if (window.__rcuInstalled) return;
  window.__rcuInstalled = true;

  // Capture-phase listeners registered at document-start run before page
  // handlers. Stopping propagation leaves the browser default action intact,
  // so the native context menu still opens.
  const passThrough = (event) => {
    event.stopImmediatePropagation();
  };

  for (const type of ['contextmenu', 'selectstart', 'dragstart']) {
    window.addEventListener(type, passThrough, { capture: true, passive: false });
  }

  // Only block the page's own right-button handlers; left/middle clicks and
  // normal mouse tracking must keep working.
  const passRightButton = (event) => {
    if (event.button === 2) event.stopImmediatePropagation();
  };
  window.addEventListener('mousedown', passRightButton, true);
  window.addEventListener('mouseup', passRightButton, true);

  const style = document.createElement('style');
  style.id = 'right-click-unblocker-style';
  style.textContent = `
    :root, :root * {
      -webkit-user-select: text !important;
      user-select: text !important;
      -webkit-touch-callout: default !important;
    }
    :root img, :root video, :root canvas {
      -webkit-user-drag: auto !important;
    }
  `;

  const addStyle = () => {
    const parent = document.head || document.documentElement;
    if (!parent) return false;
    parent.appendChild(style);
    return true;
  };

  if (!addStyle()) {
    new MutationObserver((_, observer) => {
      if (addStyle()) observer.disconnect();
    }).observe(document, { childList: true, subtree: true });
  }

  // Inline handlers bypass listener interception (assignment to
  // element.oncontextmenu does not create a content attribute, but HTML
  // attributes and setAttribute do), so strip known blocker attributes.
  const HANDLER_ATTRS = ['oncontextmenu', 'onselectstart', 'ondragstart'];
  const BLOCKER_SELECTOR = HANDLER_ATTRS.map((attr) => `[${attr}]`).join(',');

  // Per-element inline styles beat even ID-scoped !important rules, which the
  // :root * stylesheet cannot outrank.
  const unlockElement = (element) => {
    if (element.nodeType !== Node.ELEMENT_NODE) return;
    if (getComputedStyle(element).userSelect !== 'none') return;
    element.style.setProperty('user-select', 'text', 'important');
    element.style.setProperty('-webkit-user-select', 'text', 'important');
  };

  const unlockTree = (root) => {
    if (root.nodeType === Node.ELEMENT_NODE) unlockElement(root);
    root.querySelectorAll?.('*').forEach(unlockElement);
  };

  const strip = (element) => {
    for (const attr of HANDLER_ATTRS) {
      if (element.hasAttribute(attr)) element.removeAttribute(attr);
    }
    if (element.tagName === 'IMG' && element.getAttribute('draggable') === 'false') {
      element.removeAttribute('draggable');
    }
  };

  const clean = (root) => {
    const type = root.nodeType;
    if (type === Node.ELEMENT_NODE) {
      strip(root);
      root.querySelectorAll(BLOCKER_SELECTOR).forEach(strip);
    } else if (type === Node.DOCUMENT_NODE || type === Node.DOCUMENT_FRAGMENT_NODE) {
      root.querySelectorAll?.(BLOCKER_SELECTOR).forEach(strip);
    }
    unlockTree(root);
  };

  clean(document);

  new MutationObserver((records) => {
    for (const record of records) {
      if (record.type === 'attributes') {
        if (record.attributeName === 'draggable') {
          if (record.target.tagName === 'IMG' && record.target.getAttribute('draggable') === 'false') {
            record.target.removeAttribute('draggable');
          }
        } else {
          record.target.removeAttribute(record.attributeName);
        }
        continue;
      }
      record.addedNodes.forEach(clean);
    }
  }).observe(document, {
    childList: true,
    subtree: true,
    attributes: true,
    attributeFilter: [...HANDLER_ATTRS, 'draggable'],
  });

  // Some themes lock selection with body { user-select: none }. The style
  // element above covers normal cases; inline styles on <html>/<body> survive
  // a CSP that rejects injected <style> elements.
  const unlockBody = () => {
    if (!document.body) return false;
    for (const element of [document.documentElement, document.body]) {
      element.style.setProperty('user-select', 'text', 'important');
      element.style.setProperty('-webkit-user-select', 'text', 'important');
    }
    return true;
  };

  if (!unlockBody()) {
    new MutationObserver((_, observer) => {
      if (unlockBody()) observer.disconnect();
    }).observe(document, { childList: true, subtree: true });
  }

  // Stylesheets finish applying at different times; rescan once the DOM and
  // then once all external resources are in.
  const rescan = () => unlockTree(document);
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', rescan, { once: true });
  } else {
    rescan();
  }
  window.addEventListener('load', rescan, { once: true });
})();