Copy HTML to Anki

Copy entire HTML of a webpage and send it to Anki, converting relative URLs to absolute URLs. Trigger with Ctrl+Shift+Y or via Tampermonkey menu.

当前为 2024-07-10 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Copy HTML to Anki
// @namespace    http://tampermonkey.net/
// @version      1.7
// @description  Copy entire HTML of a webpage and send it to Anki, converting relative URLs to absolute URLs. Trigger with Ctrl+Shift+Y or via Tampermonkey menu.
// @author       nabe
// @match        *://*/*
// @grant        GM_xmlhttpRequest
// @grant        GM_registerMenuCommand
// @connect      localhost
// @run-at       document-end
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    function copyHtmlToAnki() {
        console.log("copyHtmlToAnki function triggered");

        // Function to convert relative URLs to absolute URLs
        function makeAbsolute(url) {
            return new URL(url, document.baseURI).href;
        }

        // Clone the document to manipulate it
        let docClone = document.documentElement.cloneNode(true);

        // Convert all relative URLs to absolute URLs
        let elements = docClone.querySelectorAll('[src], [href]');
        elements.forEach(function(element) {
            if (element.hasAttribute('src')) {
                element.setAttribute('src', makeAbsolute(element.getAttribute('src')));
            }
            if (element.hasAttribute('href')) {
                element.setAttribute('href', makeAbsolute(element.getAttribute('href')));
            }
        });

        let htmlContent = docClone.outerHTML;
        console.log("HTML content copied with absolute URLs:", htmlContent);

        GM_xmlhttpRequest({
            method: "POST",
            url: "http://localhost:8765",
            data: JSON.stringify({
                "action": "addNote",
                "version": 6,
                "params": {
                    "note": {
                        "deckName": "Default",
                        "modelName": "htmlcard",
                        "fields": {
                            "HTML": htmlContent
                        },
                        "tags": ["newimport"]
                    }
                }
            }),
            headers: {
                "Content-Type": "application/json"
            },
            onload: function(response) {
                console.log("Response from AnkiConnect:", response);
                if (response.status === 200) {
                    console.log("HTML content sent to Anki!");
                } else {
                    console.error("Failed to send content to Anki. Response:", response);
                }
            },
            onerror: function(error) {
                console.error("Error sending content to Anki:", error);
            }
        });
    }

    // Add event listener for the keyboard shortcut (Ctrl+Shift+Y)
    document.addEventListener('keydown', function(event) {
        console.log("Keydown event detected:", event);
        if (event.ctrlKey && event.shiftKey && event.code === 'KeyY') {
            console.log("Ctrl+Shift+Y detected");
            copyHtmlToAnki();
        }
    });

    // Register the menu command to Tampermonkey menu
    GM_registerMenuCommand("Copy HTML to Anki", function() {
        console.log("Menu command selected");
        copyHtmlToAnki();
    });

    console.log("Copy HTML to Anki script loaded");
})();