-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.js
82 lines (75 loc) · 2.77 KB
/
logic.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var sensitivity = 1.5;
var count = 0;
function cssHideText(ele, imageUrl){
ele.css("text-indent","-9999px");
ele.css("color","transparent");
ele.css("background-image", "url(" + imageUrl + ")");
ele.css("background-size", "contain");
}
function cssShowText(ele){
ele.css("text-indent","0px");
ele.css("color","black");
ele.css("background-image", "none");
}
function hasSomeParentTheClass(element, classname) {
var found = false;
element.parents().each(function(){
if ($(this).hasClass(classname)) {found = true;}
});
return found;
}
function hideToxicComments(divName){
chrome.storage.local.get("sensitivity", function(items){
if (items["sensitivity"] != undefined){
sensitivity = parseFloat(items["sensitivity"]);
}
var comments = [];
var commentSpanArr = [];
$(divName).each(function(){
if($(this).attr('id') != "1" && !hasSomeParentTheClass($(this), "side")){
comments.push($(this).text());
commentSpanArr.push($(this));
$(this).attr("id","1");
}
})
if (comments.length > 0) {
console.log("sending request to 'comments' server");
console.log("Level of sensitivity is:" + sensitivity);
// console.log(JSON.stringify(comments));
$.ajax({
method: 'POST',
url:"https://hateblockapi.azurewebsites.net/api/Toxicity?code=1Rfug4qf3Ra8Uos7F7kZR2NMpYNNGS4B5hiJPp/5HutMsMGHD9893g==",
crossDomain : true,
data: JSON.stringify(comments),
success:function(str){
updateComments(str, commentSpanArr);
},
error:function(XMLHttpRequest, textStatus, errorThrown){
console.log("we got an error back from the server." + JSON.stringify(errorThrown));
}
});
}
});
}
function updateComments(results, commentSpanArr) {
for (var i = 0; i < results.length; i++) {
var score = parseFloat(results[i]);
var isToxic = score < sensitivity;
var commentSpan = commentSpanArr[i];
if (isToxic){
count++;
chrome.runtime.sendMessage(count);
var toxicImageUrl = chrome.extension.getURL("images/toxic-comment.png");
console.log("Comment is toxic");
cssHideText(commentSpan, toxicImageUrl);
commentSpan.hover(function(){
cssShowText($(this));
}, function(){
cssHideText($(this), toxicImageUrl);
});
}else{
// This is a non toxic comment, don't change it
console.log("Comment is non toxic");
}
}
}