Plain Text Clipboard

Tự động chuyển text copy về dạng thuần text, loại bỏ font, màu sắc, định dạng HTML.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Plain Text Clipboard
// @namespace    https://github.com/plain-text-clipboard
// @version      2.0.0
// @description  Tự động chuyển text copy về dạng thuần text, loại bỏ font, màu sắc, định dạng HTML.
// @author       Claude
// @match        *://*/*
// @run-at       document-start
// ==/UserScript==

(function () {
  'use strict';

  function htmlToPlainText(html) {
    const div = document.createElement('div');
    div.innerHTML = html;

    div.querySelectorAll('br').forEach(br => br.replaceWith('\n'));
    div.querySelectorAll('p, div, li, tr, h1, h2, h3, h4, h5, h6, blockquote').forEach(el => {
      el.prepend('\n');
    });

    return (div.innerText || div.textContent || '')
      .replace(/\r\n/g, '\n')
      .replace(/\r/g, '\n')
      .replace(/[ \t]+/g, ' ')
      .replace(/\n{3,}/g, '\n\n')
      .trim();
  }

  document.addEventListener('copy', (e) => {
    const selection = window.getSelection();
    if (!selection || selection.rangeCount === 0 || selection.isCollapsed) return;

    let htmlContent = '';
    try {
      const range = selection.getRangeAt(0);
      const fragment = range.cloneContents();
      const tempDiv = document.createElement('div');
      tempDiv.appendChild(fragment);
      htmlContent = tempDiv.innerHTML;
    } catch {
      return;
    }

    if (!/<[a-z][\s\S]*>/i.test(htmlContent)) return;

    const plainText = htmlToPlainText(htmlContent);
    if (!plainText) return;

    try {
      e.preventDefault();
      e.clipboardData.setData('text/plain', plainText);
      e.clipboardData.setData('text/html', '');
    } catch {}
  }, true);

})();