1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
+
|
// JavaScript
"use strict";
const oGrammalecteAPI = {
// functions callable from within pages
// to be sent to the content-cript via an event “GrammalecteCall”
sVersion: "1.0",
openPanel: function (arg1) {
// Parameter: a text, a node, or the identifier of a node.
let xNode = null;
if (typeof(arg1) === 'string') {
if (document.getElementById(arg1)) {
xNode = document.getElementById(arg1);
} else {
this.openPanelForText(arg1);
}
}
else if (arg1 instanceof HTMLElement) {
xNode = arg1;
}
if (xNode) {
if (xNode.tagName == "INPUT" || xNode.tagName == "TEXTAREA" || xNode.tagName == "IFRAME" || xNode.isContentEditable) {
this.openPanelForNode(xNode);
} else {
this.openPanelForText(xNode.innerText);
}
}
},
openPanelForNode: function (vNode) {
// Parameter: a HTML node or the identifier of a HTML node
if (vNode instanceof HTMLElement) {
let xEvent = new CustomEvent("GrammalecteCall", { detail: {sCommand: "openPanelForNode", xNode: vNode} });
document.dispatchEvent(xEvent);
}
else if (typeof(vNode) === "string" && document.getElementById(vNode)) {
let xEvent = new CustomEvent("GrammalecteCall", { detail: {sCommand: "openPanelForNode", xNode: document.getElementById(vNode)} });
document.dispatchEvent(xEvent);
}
else {
console.log("[Grammalecte API] Error: parameter is not a HTML node.");
}
},
openPanelForText: function (sText) {
// Parameter: text to analyze
openPanelForText: function (sText, vNode=null) {
// Parameter: text to analyze, and optionaly a node to send results to.
if (typeof(sText) === "string") {
let xNode = null;
if (vNode instanceof HTMLElement) {
xNode = vNode;
}
else if (typeof(vNode) === "string" && document.getElementById(vNode)) {
xNode = document.getElementById(vNode);
}
let xEvent = new CustomEvent("GrammalecteCall", { detail: {sCommand: "openPanelForText", sText: sText} });
let xEvent = new CustomEvent("GrammalecteCall", { detail: {sCommand: "openPanelForText", sText: sText, xNode: xNode} });
document.dispatchEvent(xEvent);
} else {
console.log("[Grammalecte API] Error: parameter is not a text.");
}
},
parseNode: function (vNode) {
|
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
let xEvent = new CustomEvent("GrammalecteCall", { detail: {sCommand: "parseNode", xNode: document.getElementById(vNode)} });
document.dispatchEvent(xEvent);
}
else {
console.log("[Grammalecte API] Error: parameter is not a HTML node or doesn’t have an identifier.");
}
},
parseText: function (sText, vNode) {
// Parameter: text to analyze, and a node to send results to.
if (typeof(sText) === "string") {
if (vNode instanceof HTMLElement && vNode.id) {
let xEvent = new CustomEvent("GrammalecteCall", { detail: {sCommand: "parseText", sText: sText, xNode: vNode} });
document.dispatchEvent(xEvent);
}
else if (typeof(vNode) === "string" && document.getElementById(vNode)) {
let xEvent = new CustomEvent("GrammalecteCall", { detail: {sCommand: "parseText", sText: sText, xNode: document.getElementById(vNode)} });
document.dispatchEvent(xEvent);
}
else {
console.log("[Grammalecte API] Error: parameter is not a HTML node or doesn’t have an identifier.");
}
} else {
console.log("[Grammalecte API] Error: parameter is not a text.");
}
},
getSpellSuggestions: function (sWord, sDestination, sRequestId="") {
/* parameters:
- sWord (string)
- sDestination: HTML identifier (string) -> the result will be sent as an event “GrammalecteResult” to destination node
- sRequestId: custom identifier for the request (string) [default = ""]
*/
|