-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
167 lines (141 loc) · 4.27 KB
/
background.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
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
162
163
164
165
166
167
// chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
// console.log(request);
// sendResponse({ farewell: "goodbye" });
// });
let customSlogans = [];
const userBannedSlogansList = document.getElementById("userBannedSlogansList");
const bannedSlogansList = document.getElementById("bannedSlogansList");
const page1 = document.getElementById("page1");
const page2btn = document.getElementById("s_page2");
const page1btn = document.getElementById("s_page1");
const container = document.getElementById("container");
const customSloganTextbox = document.getElementById("customSlogan");
const addSlogan = document.getElementById("add");
const customBanWord = document.getElementById("customBanWord");
const set = document.getElementById("set");
const parseWiki = document.getElementById("parseWiki");
function sleep(seconds) {
var e = new Date().getTime() + seconds * 1000;
while (new Date().getTime() <= e) {}
}
chrome.storage.sync.get("savedCustomSlogans", function(items) {
if (items.savedCustomSlogans) {
customSlogans = items.savedCustomSlogans;
// updateUserBannedList("");
}
});
parseWiki.onchange = () => {
chrome.storage.sync.set({ parseWiki: parseWiki.checked }, function() {});
};
chrome.storage.sync.get("customBanWord", function(items) {
if (items.customBanWord) {
customBanWord.placeholder = "Current: " + items.customBanWord;
}
});
chrome.storage.sync.get("parseWiki", function(items) {
if (items.parseWiki != undefined) {
parseWiki.checked = items.parseWiki;
}
});
set.onclick = () => {
saveNewBanWord();
};
customBanWord.onkeyup = e => {
if (e.keyCode == 13) {
saveNewBanWord();
}
};
customSloganTextbox.onkeyup = e => {
if (e.keyCode == 13) {
addNew();
} else {
updateUserBannedList(customSloganTextbox.value);
}
};
addSlogan.onclick = () => {
addNew();
};
function saveNewBanWord() {
chrome.storage.sync.set({ customBanWord: customBanWord.value }, function() {
customBanWord.placeholder = "Current: " + customBanWord.value;
customBanWord.value = "";
});
}
function addNew() {
const customSlogan = customSloganTextbox.value.toLowerCase();
if (!customSlogans.includes(customSlogan)) {
customSlogans.push(customSlogan);
chrome.storage.sync.set({ savedCustomSlogans: customSlogans }, function() {
customSloganTextbox.value = "";
updateUserBannedList("");
});
}
}
function updateUserBannedList(filter) {
filter = filter.toLowerCase();
if (customSlogans.length == 0) {
userBannedSlogansList.innerHTML = fullDiv(
"Type in word in the textbox and press '+' to add a new word"
);
return;
}
userBannedSlogansList.innerHTML = "";
customSlogans.forEach((slogan, i) => {
if (slogan.includes(filter)) {
let sloganDOM = span(
span(i + 1) +
span(slogan) +
button("removeSlogan-" + i, "<i class='minus' />")
);
userBannedSlogansList.innerHTML += sloganDOM;
}
});
setTimeout(() => {
for (let i = 0; i < customSlogans.length; i++) {
let button = document.getElementById("removeSlogan-" + i);
button.name = i;
button.addEventListener("click", e => {
customSlogans.splice(i, 1);
chrome.storage.sync.set(
{ savedCustomSlogans: customSlogans },
function() {
updateUserBannedList(customSloganTextbox.value);
}
);
});
}
});
}
page2btn.onclick = () => {
page1.style.marginLeft = "-400px";
};
page1btn.onclick = () => {
page1.style.marginLeft = "0px";
};
function fullDiv(text) {
return "<div class='fullDiv'>" + span(text) + "</div>";
}
function span(text) {
return "<span>" + text + "</span>";
}
function button(id, text) {
return "<button id = " + id + ">" + text + "</button>";
}
//get banned slogans
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, { action: "getBannedSlogans" }, function(
response
) {
if (response) {
let resultContent = "";
if (response.length == 0) {
resultContent = fullDiv("Hooray, no banned words!");
} else {
response.forEach((slogan, i) => {
resultContent += span(span(i + 1) + span(slogan));
});
}
bannedSlogansList.innerHTML = resultContent;
}
});
});