javbus find copy

主帖、楼层、楼层评论中发现番号即生成复制按钮

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

You will need to install an extension such as Tampermonkey to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         javbus find copy
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  主帖、楼层、楼层评论中发现番号即生成复制按钮
// @match        https://www.javbus.com/forum/*
// @license      zxuuhh
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  const CODE_REGEX = /\b([A-Z]{2,6}-?\d{3,5})\b/g;

  function createCopyBtn(code) {
    const btn = document.createElement('span');
    btn.textContent = ' [复制]';
    btn.style.cssText = `
      color:#337ab7;
      cursor:pointer;
      font-size:17px;
      user-select:none;
    `;
    btn.addEventListener('click', () => {
      navigator.clipboard.writeText(code);
      btn.textContent = ' [已复制]';
      setTimeout(() => (btn.textContent = ' [复制]'), 1000);
    });
    return btn;
  }

  function processTextNode(textNode) {
    const text = textNode.nodeValue;
    if (!CODE_REGEX.test(text)) return;

    CODE_REGEX.lastIndex = 0;

    const frag = document.createDocumentFragment();
    let lastIndex = 0;
    let match;

    while ((match = CODE_REGEX.exec(text))) {
      const code = match[1];

      frag.appendChild(
        document.createTextNode(text.slice(lastIndex, match.index))
      );

      frag.appendChild(document.createTextNode(code));
      frag.appendChild(createCopyBtn(code));

      lastIndex = match.index + code.length;
    }

    frag.appendChild(
      document.createTextNode(text.slice(lastIndex))
    );

    textNode.parentNode.replaceChild(frag, textNode);
  }

  function scanContainer(container) {
    if (container.dataset.scanned) return;
    container.dataset.scanned = '1';

    const walker = document.createTreeWalker(
      container,
      NodeFilter.SHOW_TEXT,
      null,
      false
    );

    const nodes = [];
    let node;
    while ((node = walker.nextNode())) {
      nodes.push(node);
    }

    nodes.forEach(processTextNode);
  }

  function scanAll() {
    document
      .querySelectorAll('td.t_f, div.psti')
      .forEach(scanContainer);
  }

  setInterval(scanAll, 500);
})();