☰

BDSMLR Hover Preview

Show full-size image/GIF when hovering post thumbnails on bdsmlr.com. S saves, A or Shift+right-click marks, D downloads marked, C clears marks, hold Shift to disable preview

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

Um dieses Skript zu installieren, müssen Sie eine Erweiterung wie Tampermonkey oder Violentmonkey installieren.

Um dieses Skript zu installieren, müssen Sie eine Erweiterung wie Tampermonkey oder Violentmonkey installieren.

Um dieses Skript zu installieren, müssen Sie eine Erweiterung wie Tampermonkey oder Userscripts installieren.

Um dieses Skript zu installieren, müssen Sie eine Erweiterung wie Tampermonkey installieren.

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

Um dieses Design zu installieren, müssen Sie eine Erweiterung wie Stylus installieren.

Um dieses Design zu installieren, müssen Sie eine Erweiterung wie Stylus installieren.

Um dieses Design zu installieren, müssen Sie eine Erweiterung wie Stylus installieren.

Um diesen Stil zu installieren, müssen Sie eine Erweiterung für den Benutzerstil Verwaltung installieren.

Um diesen Stil zu installieren, müssen Sie eine Erweiterung für den Benutzerstil Verwaltung installieren.

Um diesen Stil zu installieren, müssen Sie eine Erweiterung für den Benutzerstil Verwaltung installieren.

(Ich habe bereits einen Benutzerstil Verwaltung, ich möchte ihn installieren!)

// ==UserScript==
// @name         BDSMLR Hover Preview
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  Show full-size image/GIF when hovering post thumbnails on bdsmlr.com. S saves, A or Shift+right-click marks, D downloads marked, C clears marks, hold Shift to disable preview
// @author       You
// @match        https://bdsmlr.com/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        GM_download
// @connect      *.bdsmlr.com
// ==/UserScript==

(() => {
  console.log('[BDSMLR hover] running');

  // ---- state ----
  let curSrc = null;   // full-size URL of the media under the cursor
  let curImg = null;   // the thumbnail element (<img> or <video>) under the cursor
  let hiddenEl = null; // element whose cursor we changed
  let lastX = 0, lastY = 0;
  const marked = new Map(); // src -> thumbnail element

  // ---- elements ----
  // two preview elements: <img> for pictures, <video> for GIFs (they are muted looping MP4s)
  const BOX_CSS = 'position:fixed;z-index:2147483647;max-height:95vh;display:none;pointer-events:none';
  const imgBox = document.createElement('img');
  const vidBox = document.createElement('video');
  vidBox.muted = true;
  vidBox.loop = true;
  vidBox.autoplay = true;
  vidBox.playsInline = true;
  [imgBox, vidBox].forEach(b => { b.style.cssText = BOX_CSS; document.documentElement.appendChild(b); });
  let box = imgBox; // the active preview element

  const badge = document.createElement('div');
  badge.style.cssText = 'position:fixed;right:12px;bottom:12px;z-index:2147483646;background:#ff00b3;color:#fff;font:bold 22px sans-serif;padding:10px 16px;border-radius:16px;display:none;pointer-events:none';
  document.documentElement.appendChild(badge);

  // 30% opacity arrow cursor (change both .3 values to adjust)
  const CURSOR = `url("data:image/svg+xml,${encodeURIComponent('<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20"><path d="M2 2l6 16 2-7 7-2z" fill="black" fill-opacity="0" stroke="white" stroke-opacity=".7"/></svg>')}") 2 2, auto`;

  // ---- helpers ----
  const showCursor = () => { hiddenEl?.style.removeProperty('cursor'); hiddenEl = null; };
  const hideAll = () => {
    imgBox.style.display = 'none';
    vidBox.style.display = 'none';
    vidBox.pause();
    showCursor();
    curSrc = null;
    curImg = null;
  };
  const previewing = () => curSrc && box.style.display !== 'none';
  const markStyle = () => { box.style.outline = marked.has(curSrc) ? '3px solid #ff00b3' : ''; box.style.outlineOffset = '-3px'; };
  const fileName = src => new URL(src).pathname.split('/').pop() || 'image.jpg';
  const save = url => GM_download({ url, name: fileName(url), onerror: err => console.log('[BDSMLR hover] save failed', url, err) });

  // find an ocdn <img> or <video> under the cursor, looking inside shadow roots too
  const mediaAt = (root, x, y, seen = new Set()) => {
    if (seen.has(root)) return;
    seen.add(root);
    for (const el of root.elementsFromPoint(x, y)) {
      if ((el.tagName === 'IMG' || el.tagName === 'VIDEO') && /ocdn\d+\.bdsmlr\.com/.test(el.currentSrc || el.src)) return el;
      if (el.shadowRoot) {
        const r = mediaAt(el.shadowRoot, x, y, seen);
        if (r) return r;
      }
    }
  };

  const place = () => {
    box.style.maxWidth = innerWidth * 0.6 + 'px';
    box.style.display = 'block';
    markStyle();
    const w = box.offsetWidth, h = box.offsetHeight;
    box.style.left = Math.max(5, Math.min(lastX - w / 2, innerWidth - w - 5)) + 'px';
    box.style.top = Math.max(5, Math.min(lastY - h / 2, innerHeight - h - 5)) + 'px';
  };

  // ---- marking ----
  const updateBadge = () => {
    badge.textContent = marked.size + ' marked';
    badge.style.display = marked.size ? 'block' : 'none';
  };
  const setOutline = (el, on) => {
    el.style.outline = on ? '3px solid #ff00b3' : '';
    el.style.outlineOffset = on ? '-3px' : '';
  };
  const toggleMark = (src, el) => {
    if (!src || !el) return;
    if (marked.has(src)) { setOutline(marked.get(src), false); marked.delete(src); }
    else { setOutline(el, true); marked.set(src, el); }
    updateBadge();
    markStyle();
  };
  const clearMarks = () => {
    marked.forEach(el => setOutline(el, false));
    marked.clear();
    updateBadge();
    markStyle();
  };
  const downloadMarked = () => {
    [...marked.keys()].forEach((url, i) => setTimeout(() => save(url), i * 300)); // gap so the browser doesn't block the batch
    clearMarks();
  };

  // ---- events ----
  imgBox.addEventListener('load', () => { if (curSrc && box === imgBox) place(); });
  vidBox.addEventListener('loadedmetadata', () => { if (curSrc && box === vidBox) place(); });

  document.addEventListener('mousemove', e => {
    const t = e.composedPath()[0];
    if (t?.tagName === 'A' && t.title) t.removeAttribute('title'); // kill "Open post" tooltip
    if (e.shiftKey) { hideAll(); return; }

    const el = mediaAt(document, e.clientX, e.clientY);
    if (!el) { hideAll(); return; }

    // swap cursor on the element under the mouse
    if (hiddenEl !== t) {
      showCursor();
      t?.style?.setProperty('cursor', CURSOR, 'important');
      hiddenEl = t;
    }

    // pick the right preview element (image vs video)
    const next = el.tagName === 'VIDEO' ? vidBox : imgBox;
    if (next !== box) {
      box.style.display = 'none';
      vidBox.pause();
      box = next;
    }

    const src = el.currentSrc || el.src;
    curSrc = src;
    curImg = el;
    lastX = e.clientX; lastY = e.clientY;
    if (box.src !== src) box.src = src;
    if (box === vidBox && vidBox.paused) vidBox.play().catch(() => {});
    place();
  }, true);

  // hide when the mouse leaves the window, the tab loses focus, or the page scrolls
  document.documentElement.addEventListener('mouseleave', hideAll);
  document.addEventListener('mouseout', e => { if (!e.relatedTarget) hideAll(); }, true);
  window.addEventListener('blur', hideAll);
  window.addEventListener('scroll', hideAll, true);

  // Shift + right-click marks/unmarks the thumbnail under the cursor
  document.addEventListener('contextmenu', e => {
    if (!e.shiftKey) return;
    const el = mediaAt(document, e.clientX, e.clientY);
    if (!el) return;
    toggleMark(el.currentSrc || el.src, el);
    e.preventDefault();
    e.stopPropagation();
  }, true);

  // keys: S save one, A mark, D download marked, C clear marks
  document.addEventListener('keydown', e => {
    if (e.key === 'Shift') { hideAll(); return; }
    if (e.ctrlKey || e.metaKey || e.altKey) return;
    const tag = e.composedPath()[0]?.tagName;
    if (tag === 'INPUT' || tag === 'TEXTAREA') return;
    const k = e.key.toLowerCase();

    if (k === 's' && previewing()) { save(curSrc); e.preventDefault(); }
    else if (k === 'a' && previewing()) { toggleMark(curSrc, curImg); e.preventDefault(); }
    else if (k === 'd' && marked.size) { downloadMarked(); e.preventDefault(); }
    else if (k === 'c' && marked.size) { clearMarks(); e.preventDefault(); }
  }, true);
})();