-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup-script.js
161 lines (128 loc) · 5.19 KB
/
popup-script.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
//---------------- focus directly on search bar when window loads----------------
const searchInput = document.querySelector("#search");
window.onload = function() {
searchInput.focus();
}
//---------------- toggle button (turn on/off) code ----------------
let toggleChoice = null;
let choiceSelected = null;
let toggleHistory = browser.storage.local.get("toggle");
toggleHistory.then(
item => {
const toggleOn = document.querySelector("#toggle-button");
toggleChoice = item.toggle;
toggleOn.textContent = toggleChoice ? "TURN OFF" : "TURN ON";
toggleOn.addEventListener("click", function() {
toggleChoice = !toggleChoice;
toggleOn.textContent = toggleChoice ? "TURN OFF" : "TURN ON";
choiceSelected = browser.storage.local.set({
toggle : toggleChoice,
});
if (choiceSelected){
browser.runtime.sendMessage({
toggle : toggleChoice,
});
}
});
},
error => console.log(`Error: ${error}`)
);
//---------------- settings page ----------------
const settingPage = {
url: '../help-settings-page/help-settings-content.html',
}
const settingButton = document.querySelector('#settings-button');
settingButton.addEventListener("click", () => {
browser.windows.create(settingPage);
})
//---------------- page navigation ----------------
// might need refactoring and optimization, inside pageScroll, the 2 if-blocks
let currentTabIndex = 0; //making search and tab change compatible
const nextButton = document.querySelector("#next");
const previousButton = document.querySelector("#previous");
let emojiCounter = 0;
const emojiPages = document.querySelectorAll(".emoji-page");
const lastEmojiPage = emojiPages.length;
let emoteCounter = 0;
const emotePages = document.querySelectorAll(".emote-page");
const lastEmotePage = emotePages.length;
const pageScroll = (event) => {
const navigationButton = event.target;
if (isEmojiTabOpen){
const prevCounter = emojiCounter;
if (navigationButton.id === "previous")
emojiCounter = emojiCounter === 0 ? lastEmojiPage-1:emojiCounter-1;
else
emojiCounter = (emojiCounter + 1) % lastEmojiPage;
emojiPages[emojiCounter].style.display = "block";
emojiPages[prevCounter].style.display = "none";
}
if (isEmoteTabOpen) {
const prevCounter = emoteCounter;
if (navigationButton.id === "previous")
emoteCounter = emoteCounter === 0 ? lastEmotePage-1:emoteCounter-1;
else
emoteCounter = (emoteCounter + 1) % lastEmotePage;
emotePages[emoteCounter].style.display = "block";
emotePages[prevCounter].style.display = "none";
}
}
nextButton.addEventListener("click", pageScroll);
previousButton.addEventListener("click", pageScroll);
//---------------- search result ----------------
const searchPage = document.querySelector('#search-results-page');
const tabPages = document.querySelectorAll('.tab-page');
const allEmojis = Array.from(document.querySelectorAll(".emoji-button"));
const allEmotes = Array.from(document.querySelectorAll(".emote-button"));
// add clipboard functionality to emote buttons, for emojis check
// ./recent-emotes-script.js
allEmotes.forEach(emoteButton => emoteButton.addEventListener("click",
() => navigator.clipboard.writeText(emoteButton.textContent)));
function filterEmojis(searchChoice){
let input = searchInput.value.toLowerCase();
emojiStyleReset();
if (input == ""){
searchPage.style.display = "none";
allTabPages[currentTabIndex].style.display = "flex"; // defined below
return;
}
tabPages.forEach(tabPage => tabPage.style.display = "none");
searchPage.style.display = "block";
let searchResults = new Set();
let searchCollection = allEmojis;
if (searchChoice == "default-emote-search"){
searchCollection = allEmotes;
}
searchCollection.forEach(emoji => {
if (emoji.id.indexOf(input) > -1)
searchResults.add(emoji);
});
searchResults.forEach(buttonNode => searchPage.append(buttonNode));
}
function emojiStyleReset(){
searchPage.innerHTML = "";
}
function addSearchEvent(response){
searchInput.addEventListener("keyup",
() => filterEmojis(response.searchChoice));
}
browser.storage.local.get("searchChoice")
.then(addSearchEvent)
.catch(error => console.log(`Error: ${error}`));
//---------------- tab change ----------------
const tabChangeButtons = document.querySelectorAll(".tab");
const allTabPages = document.querySelectorAll(".tab-page");
let isEmojiTabOpen = false;
let isEmoteTabOpen = false;
tabChangeButtons.forEach((tabButton, buttonIndex) =>
tabButton.addEventListener("click",event => toggleTabs(event, buttonIndex))
);
function toggleTabs(event, index){
allTabPages.forEach(tabPage => tabPage.style.display = "none");
allTabPages[index].style.display = "flex";
tabChangeButtons.forEach(tab => tab.classList.remove("selected-tab"));
event.target.classList.add("selected-tab");
isEmojiTabOpen = index == 2 ? true : false;
isEmoteTabOpen = index == 3 ? true : false;
currentTabIndex = index;
}