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.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==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);

})();