-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyscript.js
More file actions
161 lines (134 loc) · 5.99 KB
/
myscript.js
File metadata and controls
161 lines (134 loc) · 5.99 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//Removes elements from array by value
function removeA(arr) {
var what, a = arguments, L = a.length, ax;
while (L > 1 && arr.length) {
what = a[--L];
while ((ax= arr.indexOf(what)) !== -1) {
arr.splice(ax, 1);
}
}
return arr;
}
var spamWordsArr=[]; //contains all the spam words
chrome.storage.sync.get("blockkk", function (obj) {
spamWordsArr=obj.blockkk;
console.log('Blocked Comments: ',spamWordsArr);
if (typeof spamWordsArr==='undefined') { // This occurs when the extension is first used as nothing would be stored
spamWordsArr=[];
};
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "helloNewSpamWord") // adding new spam word to list
{
spamWordsArr.push(request.spam);
chrome.storage.sync.set({'blockkk': spamWordsArr}, function() {
console.log('blocked ',request.spam);
});
sendResponse(spamWordsArr);
}
if (request.greeting == "needMyData") // Called when popUp is opened, send the list to display in the frontend.
{
sendResponse(spamWordsArr);
}
if (request.greeting == "unBlockThisWord") // Removing the given comment from the list of blocked comments
{
removeA(spamWordsArr, request.spam);
chrome.storage.sync.set({'blockkk': spamWordsArr}, function() {
console.log('removed from block list: ',request.spam);
});
sendResponse(spamWordsArr);
}
});
window.onload = function(){
// foo(?:(?!foo).)*?boo
spamWordsArr.forEach( function(spamWord) {
var variableReg=new RegExp(spamWord);
//
var commonReg = /\<div class="clearfix" data-reactid="((?:(?!\>).)*?)"\>(?:(?!\<div class="clearfix").)*/g;
var myRegexp=new RegExp(commonReg.source+variableReg.source);
// console.log(myRegexp);
var match = myRegexp.exec(document.body.innerHTML);
if (match) {
// console.log(match[1]);
var rr=document.querySelectorAll("[data-reactid='"+match[1]+"']");
rr[0].parentNode.removeChild(rr[0]);
}
});
// Adding a button to the first comment... not needed right now...
var Loll={
list: null,
link: null
};
Loll.list = document.createElement('li');
Loll.list.className = "_4fn6 _3zm-";
Loll.link = document.createElement('a');
Loll.link.className = "_1ayn";
Loll.link.href = "#";
Loll.link.innerHTML = 'Loll..';
Loll.list.appendChild(Loll.link);
var fbList;
var tinyman;
if( (fbList = document.getElementsByClassName('UFICommentContentBlock')[0]) )
tinyman = fbList.lastChild;
fbList.insertBefore(Loll.list, tinyman);
// var closeB=document.getElementsByClassName('UFICommentCloseButton _50zy _50-0 _50z- _5upp _42ft');
// console.log(closeB);
// for(var i=0;i<closeB.length;i++){
// closeB[i].onclick = function() { console.log('blah'); };
// }
};
$(document).bind('DOMNodeInserted', function(event) {
// A new node was inserted into the DOM
// event.target is a reference to the newly inserted node
spamWordsArr.forEach( function(spamWord) {
//finding out where the comment is present in the html and trying to catch every thing starting from its main parent div
// so that ill get its data-reactid which is unique so that I can remove it
var variableReg=new RegExp(spamWord);
var commonReg = /\<div class="clearfix" data-reactid="((?:(?!\>).)*?)"\>(?:(?!\<div class="clearfix").)*/g;
var myRegexp=new RegExp(commonReg.source+variableReg.source);
var match = myRegexp.exec(event.target.innerHTML);
// console.log("loll");
if (match) {
// console.log(match[1]);
var rr1=event.target.querySelectorAll("[data-reactid='"+match[1]+"']");
rr1[0].parentNode.removeChild(rr1[0]);
}
});
//adding a function to all the 'x' buttons to the right of any comment
var closeB=document.getElementsByClassName('UFICommentCloseButton _50zy _50-0 _50z- _5upp _42ft');
var printObj;
for(var i=0;i<closeB.length;i++){
// closeB[i].onclick = spamIt(closeB[i]);
(function(i) {
closeB[i].onclick = function() {
spamIt(closeB[i]);
}
})(i);
}
});
});
var spamIt=function(nod) {
console.log('blah');
var r = confirm("Do you want to block this comment completely...?");
// I fell the most awesome thing is this.. ^_^ . Starting from the button whose onclick event is called
// travesered through the DOM to get the text of the comment to add it block list
if (r == true) {
console.log('yes');
var strr=nod.parentNode.firstChild.lastChild.previousSibling.firstChild.firstChild.innerText;
// console.log(strr);
// console.log(escapeRegExp(strr));
spamWordsArr.push(escapeRegExp(strr));
chrome.storage.sync.set({'blockkk': spamWordsArr}, function() {
console.log('saved');
});
} else {
console.log('no');
}
};
//http://stackoverflow.com/a/6969486/2466168
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}