Anki-Connect-addNotes

本插件通过向页面注入一个功能按钮,来调用Anki-Connect向Anki中添加卡片。

  1. // ==UserScript==
  2. // @name Anki-Connect-addNotes
  3. // @namespace */*
  4. // @version 0.1
  5. // @description 本插件通过向页面注入一个功能按钮,来调用Anki-Connect向Anki中添加卡片。
  6. // @author otc
  7. // @match */*
  8. // @icon *
  9. // @grant GM_xmlhttpRequest
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15. function addNotes(){
  16. var mdUrl = ['<a href="',document.URL,'">',document.title,'</a>'].join("")
  17. var data = JSON.stringify({
  18. "action": "addNotes",
  19. "version": 6,
  20. "params": {
  21. "notes": [
  22. {
  23. "deckName": "Default",
  24. "modelName": "Basic",
  25. "fields": {
  26. "Front": mdUrl
  27. },
  28. "tags": []
  29. }
  30. ]
  31. }
  32. });
  33. GM_xmlhttpRequest({
  34. method: "post",
  35. url: 'http://127.0.0.1:8765',
  36. data: data,
  37. headers: { "Content-Type":"application/json" },
  38. onload: function(r) {
  39. console.log("添加至Anki成功!",r);
  40. }
  41. });
  42. }
  43.  
  44. function addButton(){
  45. let Container = document.createElement('div');
  46. Container.id = "add-notes-container";
  47. Container.style.position="fixed"
  48. Container.style.left="0"
  49. Container.style.top="0"
  50. Container.style['z-index']="999999"
  51. Container.innerHTML =`<button id="addNotes" style="position:absolute; left:30px; top:20px">
  52. Add2Anki
  53. </button>
  54. `
  55.  
  56. document.body.appendChild(Container);
  57.  
  58. let button = document.getElementById("addNotes");
  59. button.onclick=addNotes;
  60. }
  61. addButton();
  62. })();