forked from nubwett/Detox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.mjs
57 lines (51 loc) · 1.38 KB
/
content.mjs
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
/*const wrap = document.body;
console.log(wrap);
for each dom in the doc body
send dom info to api
api returns something
changes inner html to censor
*/
var xhr = new XMLHttpRequest();
var url =
"https://detox.cognitiveservices.azure.com/text/analytics/v2.1/sentiment";
xhr.open("POST", url, false);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader(
"Ocp-Apim-Subscription-Key",
"f4e6a7f4c5df438bbc037cf19f65ddc8"
);
xhr.onreadystatechange = function() {
// Call a function when the state changes.
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
console.log(xhr.responseText);
}
};
var data = {
documents: [
{
language: "en",
id: "1",
text: "Hello world. This is some input text that I love."
},
{
language: "en",
id: "2",
text: "It's incredibly sunny outside! I'm so happy."
}
]
};
xhr.send(data);
var elements = document.getElementsByTagName("*");
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
for (var j = 0; j < element.childNodes.length; j++) {
var node = element.childNodes[j];
if (node.nodeType === 3) {
var text = node.nodeValue;
var replacedText = text.replace(/cheese/gi, "chick");
if (replacedText !== text) {
element.replaceChild(document.createTextNode(replacedText), node);
}
}
}
}