Copy HTML to Anki

Copy specific parts of HTML text and send them to Anki, converting relative URLs to absolute URLs. Trigger with Ctrl+Shift+Y or via Tampermonkey menu.

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

  1. // ==UserScript==
  2. // @name Copy HTML to Anki
  3. // @namespace http://tampermonkey.net/
  4. // @version 2.8
  5. // @description Copy specific parts of HTML text and send them 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. (function() {
  15. 'use strict';
  16. function copyHtmlToAnki() {
  17. // Function to convert relative URLs to absolute URLs
  18. function makeAbsolute(url) {
  19. return new URL(url, document.baseURI).href;
  20. }
  21. // Clone the document to manipulate it
  22. let docClone = document.documentElement.cloneNode(true);
  23. // Convert all relative URLs to absolute URLs
  24. let elements = docClone.querySelectorAll('[src], [href]');
  25. elements.forEach(function(element) {
  26. if (element.hasAttribute('src')) {
  27. element.setAttribute('src', makeAbsolute(element.getAttribute('src')));
  28. }
  29. if (element.hasAttribute('href')) {
  30. element.setAttribute('href', makeAbsolute(element.getAttribute('href')));
  31. }
  32. });
  33. // Extract the text content of specific parts needed
  34. let frontElement = docClone.querySelector('.container.card');
  35. let frontField = frontElement ? frontElement.innerHTML : '';
  36. console.log("Front Field:", frontField);
  37.  
  38. let questionField = docClone.querySelector('form.question h3')?.innerText.trim() || '';
  39. console.log("Question Field:", questionField);
  40. let optionField = Array.from(docClone.querySelectorAll('.options .option'))
  41. .map(option => option.innerText.trim())
  42. .filter(text => text)
  43. .map(text => `<li>${text}</lis>`)
  44. .join('') || '';
  45. console.log("Option Field:", optionField);
  46. let backField = Array.from(docClone.querySelectorAll('.options .option.correct'))
  47. .map(option => option.innerText.trim())
  48. .filter(text => text)
  49. .map(text => `<li>${text}</li>`)
  50. .join('') || '';
  51. console.log("Answer Field:", backField);
  52. //let extraField = docClone.querySelector('.results.container.collected .feedback-container .text')?.innerText.trim() || '';
  53. //console.log("Additional Info Field:", extraField);
  54. let extraElement = docClone.querySelector('.results.container.collected .feedback-container .text');
  55. let extraField = extraElement ? extraElement.innerHTML : '';
  56. console.log("Additional Info Field:", extraField);
  57.  
  58.  
  59. // Create the note fields
  60. let noteFields = {
  61. "Front": frontField,
  62. "Question": questionField,
  63. "Options": optionField,
  64. "Back": backField,
  65. "Extra": extraField
  66. };
  67. console.log("Note fields to be sent to Anki:", noteFields);
  68. GM_xmlhttpRequest({
  69. method: "POST",
  70. url: "http://localhost:8765",
  71. data: JSON.stringify({
  72. "action": "addNote",
  73. "version": 6,
  74. "params": {
  75. "note": {
  76. "deckName": "Default",
  77. "modelName": "Basic Build",
  78. "fields": noteFields,
  79. "tags": ["newimport"]
  80. }
  81. }
  82. }),
  83. headers: {
  84. "Content-Type": "application/json"
  85. },
  86. onload: function(response) {
  87. console.log("Response from AnkiConnect:", response);
  88. if (response.status === 200) {
  89. console.log("Note fields sent to Anki successfully!");
  90. } else {
  91. console.error("Failed to send note fields to Anki.");
  92. }
  93. }
  94. });
  95. }
  96. // Add event listener for the keyboard shortcut (Ctrl+Shift+Y)
  97. document.addEventListener('keydown', function(event) {
  98. if (event.ctrlKey && event.shiftKey && event.code === 'KeyY') {
  99. copyHtmlToAnki();
  100. }
  101. });
  102. // Register the menu command to Tampermonkey menu
  103. GM_registerMenuCommand("Copy HTML to Anki", copyHtmlToAnki);
  104. })();