api.js 5.1 KB

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