-
Notifications
You must be signed in to change notification settings - Fork 1
/
twitter.js
136 lines (119 loc) · 4.36 KB
/
twitter.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
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
var observer = new MutationObserver(() => {
initLabels();
addEvent();
});
var config = { attributes: false, childList: true, subtree: true, characterData: false };
observer.observe(document.body, config);
function initLabels() {
// deleteFreshAccounts();
addUnassignedUserButtons(getCommentsList());
chrome.storage.local.get('blockList', (res) => {
const blockedUsersSet = new Set(res.blockList);
addLabelToUsersFromList(getCommentsList(), blockedUsersSet)
});
}
function addUnassignedUserButtons(userList, useParent = true) {
userList.forEach((el) => {
const button = createUnassignedUserButton(el.innerText);
const parent = useParent ? el.parentNode : el;
const isUnassigned = parent.getElementsByClassName('unassigned')[0];
const isTroll = parent.getElementsByClassName('troll')[0];
if (!isUnassigned && !isTroll) {
parent.appendChild(button)
}
})
}
function createUnassignedUserButton(username) {
const button = document.createElement('button');
button.className = 'unassigned';
button.innerText = 'Oznacz';
button.addEventListener('click',(e) => {
e.preventDefault();
e.stopPropagation();
addToBlockList(username);
},true);
return button
}
function addTrollUserButton(elementList, isParent = true) {
elementList.map((el) => {
const button = document.createElement('button');
const username = el.innerText
button.addEventListener('click',(e) => {
e.preventDefault();
e.stopPropagation();
deleteLabelFromList(username);
},true);
button.className = 'troll';
button.innerText = 'troll';
const parent = isParent ? el.parentNode : el;
const isTroll = parent.getElementsByClassName('troll')[0];
if (!isTroll) {
parent.appendChild(button)
}
const isUnassigned = parent.getElementsByClassName('unassigned')[0];
if (isUnassigned) {
parent.removeChild(isUnassigned)
}
})
}
function addEvent() {
document.addEventListener("click", ()=>setTimeout(initLabels, 1000));
}
function getFreshAccounts(){
return Array.from(document.getElementsByClassName('color-0 showProfileSummary'));
}
function getCommentsList(){
const allAccounts = Array.from(document.querySelectorAll("div[dir=ltr] > span"))
return allAccounts.filter((el) => el.innerText.startsWith('@'))
}
function clearBlockList() {
chrome.storage.local.get('blockList', (res) => {
chrome.storage.local.set({blockList: []}, () => {
console.log('Clean');
});
});
}
function addLabelToUsersFromList(userList, blockedList, isParent = true) {
const trollUsers = userList.filter((el) => blockedList.has(el.innerText));
addTrollUserButton(trollUsers, isParent);
addUnassignedUserButtons(userList);
}
function addToBlockList(username) {
chrome.storage.local.get('blockList', (res) => {
const oldStorage = res.blockList;
const newStorage = oldStorage ? [...oldStorage, username] : [username];
const blockedUsersSet = new Set(newStorage);
chrome.storage.local.set({blockList: newStorage}, () => {
console.log(`${username} added to block list`);
});
addLabelToUsersFromList(getCommentsList(), blockedUsersSet);
});
}
function deleteLabelFromList(username) {
chrome.storage.local.get('blockList', (res) => {
const oldStorage = res.blockList;
let storageSet = new Set(oldStorage)
if(oldStorage){
storageSet.delete(username)
deleteLabelFromPage(username);
addLabelToUsersFromList(getCommentsList(), storageSet);
addUnassignedUserButtons(getCommentsList());
}
chrome.storage.local.set({blockList: Array.from(storageSet)}, () => {
console.log(`${username} deleted from block list`);
});
});
}
function deleteLabelFromPage(userName) {
const filtered = getCommentsList().filter((el) => el.innerText === userName);
filtered.forEach((el) =>
{
const parent = el.parentNode;
trollEl = parent.getElementsByClassName('troll')[0]
if(trollEl){
parent.removeChild(trollEl);
const button = createUnassignedUserButton(el.innerText);
parent.appendChild(button);
}
})
}