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 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Copy HTML to Anki
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.8
  5. // @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.
  6. // @author nabe
  7. // @match *://*/*
  8. // @grant GM_xmlhttpRequest
  9. // @grant GM_registerMenuCommand
  10. // @connect localhost
  11. // @run-at document-end
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. function copyHtmlToAnki() {
  19. console.log("copyHtmlToAnki function triggered");
  20.  
  21. // Function to convert relative URLs to absolute URLs
  22. function makeAbsolute(url) {
  23. console.log("Converting URL to absolute:", url);
  24. return new URL(url, document.baseURI).href;
  25. }
  26.  
  27. // Clone the document to manipulate it
  28. let docClone = document.documentElement.cloneNode(true);
  29. console.log("Document cloned");
  30.  
  31. // Convert all relative URLs to absolute URLs
  32. let elements = docClone.querySelectorAll('[src], [href]');
  33. elements.forEach(function(element) {
  34. if (element.hasAttribute('src')) {
  35. element.setAttribute('src', makeAbsolute(element.getAttribute('src')));
  36. }
  37. if (element.hasAttribute('href')) {
  38. element.setAttribute('href', makeAbsolute(element.getAttribute('href')));
  39. }
  40. });
  41.  
  42. let htmlContent = docClone.outerHTML;
  43. console.log("HTML content copied with absolute URLs:", htmlContent);
  44.  
  45. GM_xmlhttpRequest({
  46. method: "POST",
  47. url: "http://localhost:8765",
  48. data: JSON.stringify({
  49. "action": "addNote",
  50. "version": 6,
  51. "params": {
  52. "note": {
  53. "deckName": "Default",
  54. "modelName": "htmlcard",
  55. "fields": {
  56. "HTML": htmlContent
  57. },
  58. "tags": ["newimport"]
  59. }
  60. }
  61. }),
  62. headers: {
  63. "Content-Type": "application/json"
  64. },
  65. onload: function(response) {
  66. console.log("Response from AnkiConnect:", response);
  67. if (response.status === 200) {
  68. console.log("HTML content sent to Anki!");
  69. } else {
  70. console.error("Failed to send content to Anki. Response:", response);
  71. }
  72. },
  73. onerror: function(error) {
  74. console.error("Error sending content to Anki:", error);
  75. }
  76. });
  77. }
  78.  
  79. // Add event listener for the keyboard shortcut (Ctrl+Shift+Y)
  80. document.addEventListener('keydown', function(event) {
  81. console.log("Keydown event detected:", event);
  82. if (event.ctrlKey && event.shiftKey && event.code === 'KeyY') {
  83. console.log("Ctrl+Shift+Y detected");
  84. copyHtmlToAnki();
  85. }
  86. });
  87.  
  88. // Register the menu command to Tampermonkey menu
  89. GM_registerMenuCommand("Copy HTML to Anki", function() {
  90. console.log("Menu command selected");
  91. copyHtmlToAnki();
  92. });
  93.  
  94. console.log("Copy HTML to Anki script loaded");
  95. })();