Copy Page as Markdown

Convert and copy the page content as Markdown to clipboard, excluding scripts and styles.

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name         Copy Page as Markdown
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Convert and copy the page content as Markdown to clipboard, excluding scripts and styles.
// @author       You
// @match        *://*/*
// @grant        GM_setClipboard
// @grant        GM_addStyle
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/turndown.js
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Remove <script> and <style> elements from the document
    function removeScriptsAndStyles(doc) {
        const elementsToRemove = doc.querySelectorAll('script, style');
        elementsToRemove.forEach(element => element.parentNode.removeChild(element));
        return doc;
    }

    // Create a deep clone of the document body to work with, to preserve the original page content
    let clonedBody = document.body.cloneNode(true);
    clonedBody = removeScriptsAndStyles(clonedBody);

    // Initialize Turndown service with the cloned body that excludes scripts and styles
    var turndownService = new TurndownService();
    var markdown = turndownService.turndown(clonedBody.innerHTML);

    // Function to copy text to clipboard (might need permissions depending on the userscript manager)
    function copyTextToClipboard(text) {
        GM_setClipboard(text, 'text'); // Copy Markdown to clipboard
    }

    // Add a button to trigger the conversion and copying
    var button = document.createElement('button');
    button.textContent = 'Copy as Markdown';
    button.style.position = 'fixed';
    button.style.top = '20px';
    button.style.right = '20px';
    button.style.zIndex = 10000;
    document.body.appendChild(button);

    button.addEventListener('click', function() {
        copyTextToClipboard(markdown);
    });
})();