api.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // JavaScript
  2. "use strict";
  3. var oGrammalecteAPI = {
  4. // Thes script might be reloaded, don’t use const or let.
  5. // functions callable from within pages
  6. // to be sent to the content-cript via an event “GrammalecteCall”
  7. sVersion: "1.0",
  8. generateNodeId: function (xNode) {
  9. xNode.id = "grammalecte_generated_id_" + Date.now().toString(36) + "_" + (Math.floor(Math.random() * (1000000))).toString(36);
  10. console.log("[Grammalecte API] generated id:", xNode.id);
  11. return xNode.id;
  12. },
  13. openPanelForNode: function (vNode) {
  14. // Parameter: a HTML node or the identifier of a HTML node
  15. if (vNode instanceof HTMLElement) {
  16. let sNodeId = vNode.id || this.generateNodeId(vNode);
  17. let xEvent = new CustomEvent("GrammalecteCall", { detail: JSON.stringify({sCommand: "openPanelForNode", sNodeId: sNodeId}) });
  18. document.dispatchEvent(xEvent);
  19. }
  20. else if (typeof(vNode) === "string" && document.getElementById(vNode)) {
  21. let xEvent = new CustomEvent("GrammalecteCall", { detail: JSON.stringify({sCommand: "openPanelForNode", sNodeId: vNode}) });
  22. document.dispatchEvent(xEvent);
  23. }
  24. else {
  25. console.log("[Grammalecte API] Error: parameter is not a HTML node with an identifier.");
  26. }
  27. },
  28. openPanelForText: function (sText, vNode=null) {
  29. // Parameter: text to analyze, and optionaly a node to send results to.
  30. if (typeof(sText) === "string") {
  31. let sNodeId = "";
  32. if (vNode instanceof HTMLElement) {
  33. sNodeId = vNode.id || this.generateNodeId(vNode);
  34. }
  35. else if (typeof(vNode) === "string" && document.getElementById(vNode)) {
  36. sNodeId = vNode;
  37. }
  38. else {
  39. console.log("[Grammalecte API] No node identifier. No event, no result will be sent.")
  40. }
  41. let xEvent = new CustomEvent("GrammalecteCall", { detail: JSON.stringify({sCommand: "openPanelForText", sText: sText, sNodeId: sNodeId}) });
  42. document.dispatchEvent(xEvent);
  43. } else {
  44. console.log("[Grammalecte API] Error: parameter is not a text.");
  45. }
  46. },
  47. parseNode: function (vNode) {
  48. /* Parameter: a HTML node (with a identifier) or the identifier of a HTML node.
  49. The result will be sent as an event “GrammalecteResult” to the node.
  50. */
  51. if (vNode instanceof HTMLElement) {
  52. let sNodeId = vNode.id || this.generateNodeId(vNode);
  53. let xEvent = new CustomEvent("GrammalecteCall", { detail: JSON.stringify({sCommand: "parseNode", sNodeId: sNodeId}) });
  54. document.dispatchEvent(xEvent);
  55. }
  56. else if (typeof(vNode) === "string" && document.getElementById(vNode)) {
  57. let xEvent = new CustomEvent("GrammalecteCall", { detail: JSON.stringify({sCommand: "parseNode", sNodeId: vNode}) });
  58. document.dispatchEvent(xEvent);
  59. }
  60. else {
  61. console.log("[Grammalecte API] Error: parameter is not a HTML node with an identifier.");
  62. }
  63. },
  64. parseText: function (sText, vNode) {
  65. // Parameter: text to analyze, and a node to send results to.
  66. if (typeof(sText) === "string") {
  67. if (vNode instanceof HTMLElement) {
  68. let sNodeId = vNode.id || this.generateNodeId(vNode);
  69. let xEvent = new CustomEvent("GrammalecteCall", { detail: JSON.stringify({sCommand: "parseText", sText: sText, sNodeId: sNodeId}) });
  70. document.dispatchEvent(xEvent);
  71. }
  72. else if (typeof(vNode) === "string" && document.getElementById(vNode)) {
  73. let xEvent = new CustomEvent("GrammalecteCall", { detail: JSON.stringify({sCommand: "parseText", sText: sText, sNodeId: vNode}) });
  74. document.dispatchEvent(xEvent);
  75. }
  76. else {
  77. console.log("[Grammalecte API] Error: parameter is not a HTML node with an identifier.");
  78. }
  79. } else {
  80. console.log("[Grammalecte API] Error: parameter is not a text.");
  81. }
  82. },
  83. getSpellSuggestions: function (sWord, sDestination, sRequestId="") {
  84. /* parameters:
  85. - sWord (string)
  86. - sDestination: HTML identifier (string) -> the result will be sent as an event “GrammalecteResult” to destination node
  87. - sRequestId: custom identifier for the request (string) [default = ""]
  88. */
  89. if (typeof(sWord) === "string" && typeof(sDestination) === "string" && typeof(sRequestId) === "string") {
  90. let xEvent = new CustomEvent("GrammalecteCall", { detail: JSON.stringify({sCommand: "getSpellSuggestions", sWord: sWord, sDestination: sDestination, sRequestId: sRequestId}) });
  91. document.dispatchEvent(xEvent);
  92. } else {
  93. console.log("[Grammalecte API] Error: one or several parameters aren’t string.");
  94. }
  95. }
  96. }
  97. /*
  98. Tell to the webpage that the Grammalecte API is ready.
  99. */
  100. document.dispatchEvent(new Event('GrammalecteLoaded'));