LKML 內文複製器 (HTML 版)

一鍵複製 lkml.org 信件正文(保留 <br> 等 HTML 標籤)。

当前为 2025-06-06 提交的版本,查看 最新版本

// ==UserScript==
// @name         LKML 內文複製器 (HTML 版)
// @namespace    https://abc0922001.github.io/lkml-userscripts
// @version      1.1
// @description  一鍵複製 lkml.org 信件正文(保留 <br> 等 HTML 標籤)。
// @author       abc0922001
// @match        https://lkml.org/lkml/*
// @grant        none
// @license      MIT
// ==/UserScript==

(() => {
  'use strict';

  /* 等待元素出現 */
  function waitForElement(selector, timeout = 5000) {
    return new Promise((resolve, reject) => {
      const hit = document.querySelector(selector);
      if (hit) return resolve(hit);

      const ob = new MutationObserver(() => {
        const el = document.querySelector(selector);
        if (el) { ob.disconnect(); resolve(el); }
      });
      ob.observe(document.body, { childList: true, subtree: true });

      setTimeout(() => { ob.disconnect(); reject(); }, timeout);
    });
  }

  /* 提示 */
  const alertMsg = m => window.alert(m);

  /* 複製 HTML;若失敗改複製純文字 */
  async function copyToClipboard(html) {
    try {
      await navigator.clipboard.write([
        new ClipboardItem({ 'text/html': new Blob([html], { type: 'text/html' }) })
      ]);
      alertMsg('✅ 已複製 (HTML)!');
    } catch {
      navigator.clipboard.writeText(html)
        .then(() => alertMsg('✅ 已以純文字複製!'))
        .catch(e => alertMsg('❌ 複製失敗:' + e.message));
    }
  }

  /* 建按鈕 */
  function createCopyButton() {
    const btn = document.createElement('button');
    btn.textContent = '📋 複製內文(HTML)';
    Object.assign(btn.style, {
      position: 'fixed', top: '10px', right: '10px', zIndex: 1000,
      padding: '6px 10px', fontSize: '14px',
      background: '#2b7de9', color: '#fff', border: 'none',
      borderRadius: '4px', cursor: 'pointer'
    });

    btn.onclick = () => {
      waitForElement('pre[itemprop="articleBody"]')
        .then(el => copyToClipboard(el.innerHTML))   // ← 保留 <br> 等標籤
        .catch(() => alertMsg('⚠️ 找不到內文區塊。'));
    };

    document.body.appendChild(btn);
  }

  /* 🚀 啟動 */
  createCopyButton();
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址