-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.js
More file actions
166 lines (143 loc) · 4.74 KB
/
filter.js
File metadata and controls
166 lines (143 loc) · 4.74 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
162
163
164
165
166
const INGRESS_HEADER_CLASS = "tst-ingress-container"
const ITEM_PRICE_TIP = "tst-ec"
const PAID_ITEM_TIP = "tst-ec-orange"
const FREE_ITEM_TIP = "tst-ec-blue"
const ADS_ITEM_TIP = "tst-ec-white"
const FILTER_SECTION_CLASS = "custom-filter-section"
const maybeInsertAfterPageHeader = (element) => {
const header = document.getElementsByClassName(INGRESS_HEADER_CLASS)[0]
header?.after(element)
}
const filterCheck = (filter) => {
const value = appliedFilters[filter];
return html.check(value, (checked) => {
appliedFilters[filter] = checked;
adjustCardsBasedOnFilters()
})
}
const noApiKeyWarning = () => {
if (settings.get(Setting.OmdbApiKey)) return html.span()
const apiFlagSection = ApiWarning()
const warning = apiFlagSection.children[0]
const omdbApiKeyBox = html.textbox(C.Omdb.KeyHint)
html.style(omdbApiKeyBox, 'background-color', '#000000')
omdbApiKeyBox.addEventListener(C.Action.Change, ({ target }) => {
const newValue = target.value.trim()
if (!newValue || newValue.length < 8) return
settings.set(Setting.OmdbApiKey, newValue)
warning.innerText = 'Api Key set! Reloading page...'
setTimeout(() => window.location.reload(), 2000)
})
warning.prepend(html.img(C.Icon.LOGO, 20))
warning.append(omdbApiKeyBox)
return apiFlagSection
}
const createFilters = () => {
const { minRating } = appliedFilters
const paidCheck = filterCheck('paid')
const freeCheck = filterCheck('free')
const adsCheck = filterCheck('ads')
const noRatingsCheck = filterCheck('unrated')
const ratingNumber = html.title(`(${minRating})`)
const ratingSlider = html.range(minRating, 1, 100, (rating) => {
appliedFilters.minRating = parseInt(rating)
adjustCardsBasedOnFilters()
})
ratingSlider.addEventListener(C.Action.Input, ({currentTarget}) => {
ratingNumber.innerText = `(${currentTarget.value})`
})
if (!settings.get(Setting.OmdbApiKey)) {
ratingSlider.disabled = true
noRatingsCheck.disabled = true
}
const filterRow = html.flexRow(
html.title("Paid: "), paidCheck,
html.title("Free: "), freeCheck,
html.title("Ads: "), adsCheck,
html.title("Min Rating: "), ratingSlider, ratingNumber,
html.title("No Ratings: "), noRatingsCheck
);
const titleRow = html.flexRow(html.h2('Filters'), html.img(C.Icon.LOGO, 20))
const container = html.flexColumn(noApiKeyWarning(), titleRow, filterRow)
container.className = FILTER_SECTION_CLASS
html.styles(container, {
"align-items": "baseline",
"margin-left": "50px",
"padding-top": "24px",
})
return container
}
const maybeAddFiltersToHomepage = () => {
if (document.getElementsByClassName(FILTER_SECTION_CLASS).length) return
maybeInsertAfterPageHeader(createFilters())
}
const setVisibility = (item, visible) => {
html.style(item, 'visibility', visible ? 'visible' : 'hidden')
}
const setDisplayedIB = (item, visible) => {
if (visible && item.style.display !== 'none') {
return
}
html.style(item, 'display', visible ? 'inline-block' : 'none')
}
const ratingsMeetBar = (minRating, noRatings, ratings) => {
if (!ratings) return
const { metacritic, imdb, rottenTomatoes } = ratings
if (metacritic === '-' && imdb === '-' && rottenTomatoes === '-') {
return noRatings
}
if (metacritic !== '-' && parseFloat(metacritic) >= minRating) {
return true
}
if (imdb !== '-' && parseFloat(imdb) * 10 >= minRating) {
return true
}
if (rottenTomatoes !== '-' && parseFloat(rottenTomatoes) >= minRating) {
return true
}
return false
}
// TODO: Run this on any newly added cards
// TODO: Make these properties global
// TODO: Add these to settings
const adjustCardsBasedOnFilters = () => {
const innerCards = document.getElementsByClassName(TOP_IMAGE_WRAPPER_CLASS)
const hashKeys = Object.keys(settings.get(Setting.OmdbResultsHash))
const { paid, free, ads, unrated, minRating } = appliedFilters
for (const card of innerCards) {
const priceTip = card.getElementsByClassName(ITEM_PRICE_TIP)[0]
const list = priceTip?.classList
if (!list) continue
const wrapper = findParentByTagName(card, 'li')
if (!paid && list.contains(PAID_ITEM_TIP)) {
setDisplayedIB(wrapper, false)
continue
}
else if (!free && list.contains(FREE_ITEM_TIP)) {
setDisplayedIB(wrapper, false)
continue
}
else if (!ads && list.contains(ADS_ITEM_TIP)) {
setDisplayedIB(wrapper, false)
continue
}
if (minRating > 1 || !unrated) {
const ariaTitle = card?.firstElementChild?.ariaLabel
if (ariaTitle) {
const title = cleanTitle(ariaTitle)
if (title) {
const matchingKey = hashKeys.find(key => key.startsWith(`${title}|`))
if (matchingKey) {
const ratings = getRatings(matchingKey)
if (ratings) {
const meets = ratingsMeetBar(minRating, unrated, ratings)
setDisplayedIB(wrapper, meets)
continue
}
}
}
}
}
setDisplayedIB(wrapper, true)
}
}