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 => `<p>${text}</p>`)
  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. // Create the note fields
  55. let noteFields = {
  56. "Front": frontField,
  57. "Question": questionField,
  58. "Options": optionField,
  59. "Back": backField,
  60. "Extra": extraField
  61. };
  62. console.log("Note fields to be sent to Anki:", noteFields);
  63. GM_xmlhttpRequest({
  64. method: "POST",
  65. url: "http://localhost:8765",
  66. data: JSON.stringify({
  67. "action": "addNote",
  68. "version": 6,
  69. "params": {
  70. "note": {
  71. "deckName": "Default",
  72. "modelName": "Basic Build",
  73. "fields": noteFields,
  74. "tags": ["newimport"]
  75. }
  76. }
  77. }),
  78. headers: {
  79. "Content-Type": "application/json"
  80. },
  81. onload: function(response) {
  82. console.log("Response from AnkiConnect:", response);
  83. if (response.status === 200) {
  84. console.log("Note fields sent to Anki successfully!");
  85. } else {
  86. console.error("Failed to send note fields to Anki.");
  87. }
  88. }
  89. });
  90. }
  91. // Add event listener for the keyboard shortcut (Ctrl+Shift+Y)
  92. document.addEventListener('keydown', function(event) {
  93. if (event.ctrlKey && event.shiftKey && event.code === 'KeyY') {
  94. copyHtmlToAnki();
  95. }
  96. });
  97. // Register the menu command to Tampermonkey menu
  98. GM_registerMenuCommand("Copy HTML to Anki", copyHtmlToAnki);
  99. })();