-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJustEat.user.js
More file actions
365 lines (334 loc) · 13.8 KB
/
JustEat.user.js
File metadata and controls
365 lines (334 loc) · 13.8 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// ==UserScript==
// @name Just Eat Custom Userscript (UK)
// @namespace https://github.com/S4N-T0S/JustEat-userscript
// @version 1.1
// @description Customizable Just Eat filter with dark mode option
// @match https://www.just-eat.co.uk/*
// @grant GM_setValue
// @grant GM_getValue
// @license MIT
// @updateURL https://github.com/S4N-T0S/JustEat-userscript/raw/main/JustEat.user.js
// @downloadURL https://github.com/S4N-T0S/JustEat-userscript/raw/main/JustEat.user.js
// @homepageURL https://github.com/S4N-T0S/JustEat-userscript
// @supportURL https://github.com/S4N-T0S/JustEat-userscript/issues
// ==/UserScript==
(function () {
'use strict';
// Function to get or set the user's settings from localStorage
const config = {
minRating: parseFloat(GM_getValue('minRating', 4.5)),
minReviews: parseInt(GM_getValue('minReviews', 50), 10),
excludeNew: GM_getValue('excludeNew', true),
filterNoReviews: GM_getValue('filterNoReviews', true),
hideMode: GM_getValue('hideMode', false),
enableDarkMode: GM_getValue('enableDarkMode', true),
};
// Inject an options menu
function createOptionsMenu() {
const menuHtml = `
<style>
#je-options-menu {
position: fixed;
bottom: 60px;
left: 10px;
width: 320px;
height: auto;
max-height: 80vh;
background: #fff;
padding: 0;
border-radius: 10px;
box-shadow: 0 0 12px rgba(0,0,0,0.2);
z-index: 9999;
font-family: sans-serif;
display: none;
flex-direction: column;
}
#je-options-header {
padding: 15px;
border-bottom: 1px solid #ddd;
}
#je-options-header h3 {
margin: 0;
font-size: 18px;
color: #333;
}
#je-options-content {
padding: 15px;
overflow-y: auto;
flex-grow: 1;
}
.je-setting {
display: flex;
justify-content: space-between;
align-items: center;
margin: 10px 0;
font-size: 14px;
}
input[type="number"] {
width: 60px;
padding: 3px;
}
.switch {
position: relative;
display: inline-block;
width: 40px;
height: 20px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0; left: 0; right: 0; bottom: 0;
background-color: #ccc;
transition: 0.4s;
border-radius: 20px;
}
.slider:before {
position: absolute;
content: "";
height: 16px; width: 16px;
left: 2px; bottom: 2px;
background-color: white;
transition: 0.4s;
border-radius: 50%;
}
input:checked + .slider {
background-color: #4CAF50;
}
input:checked + .slider:before {
transform: translateX(20px);
}
#je-options-footer {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px 15px;
border-top: 1px solid #ddd;
background: #f9f9f9;
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
}
#saveSettings {
padding: 8px 12px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
font-size: 14px;
cursor: pointer;
}
.je-footer-link {
color: #24292f;
text-decoration: none;
font-size: 14px;
}
#settings-icon {
position: fixed;
bottom: 10px;
left: 10px;
background-color: orange;
color: white;
border: none;
border-radius: 50%;
padding: 10px;
z-index: 10000;
cursor: pointer;
}
</style>
<div id="je-options-menu">
<div id="je-options-header">
<h3>JustEat Script Settings</h3>
</div>
<div id="je-options-content">
<div class="je-setting">
<label for="minRating">Minimum Rating (1–5)</label>
<input type="number" id="minRating" value="${config.minRating}" step="0.1" min="1" max="5">
</div>
<div class="je-setting">
<label for="minReviews">Minimum Reviews</label>
<input type="number" id="minReviews" value="${config.minReviews}" min="0">
</div>
<div class="je-setting">
<label for="excludeNew">Exclude New Restaurants</label>
<label class="switch">
<input type="checkbox" id="excludeNew" ${config.excludeNew ? 'checked' : ''}>
<span class="slider"></span>
</label>
</div>
<div class="je-setting">
<label for="filterNoReviews">Exclude Restaurants with No Reviews</label>
<label class="switch">
<input type="checkbox" id="filterNoReviews" ${config.filterNoReviews ? 'checked' : ''}>
<span class="slider"></span>
</label>
</div>
<div class="je-setting">
<label for="hideMode">Remove Instead of Fade</label>
<label class="switch">
<input type="checkbox" id="hideMode" ${config.hideMode ? 'checked' : ''}>
<span class="slider"></span>
</label>
</div>
<div class="je-setting">
<label for="enableDarkMode">Enable Dark Mode</label>
<label class="switch">
<input type="checkbox" id="enableDarkMode" ${config.enableDarkMode ? 'checked' : ''}>
<span class="slider"></span>
</label>
</div>
</div>
<div id="je-options-footer">
<a href="https://github.com/S4N-T0S/JustEat-userscript" target="_blank" class="je-footer-link">GitHub</a>
<button id="saveSettings">Save Settings</button>
</div>
</div>
<button id="settings-icon">⚙️</button>
`;
const menuContainer = document.createElement('div');
menuContainer.innerHTML = menuHtml;
document.body.appendChild(menuContainer);
const settingsIcon = document.getElementById('settings-icon');
const optionsMenu = document.getElementById('je-options-menu');
settingsIcon.addEventListener('click', () => {
const isHidden = getComputedStyle(optionsMenu).display === 'none';
optionsMenu.style.display = isHidden ? 'flex' : 'none';
});
document.getElementById('saveSettings').addEventListener('click', () => {
config.minRating = parseFloat(document.getElementById('minRating').value);
config.minReviews = parseInt(document.getElementById('minReviews').value, 10);
config.excludeNew = document.getElementById('excludeNew').checked;
config.filterNoReviews = document.getElementById('filterNoReviews').checked;
config.hideMode = document.getElementById('hideMode').checked;
config.enableDarkMode = document.getElementById('enableDarkMode').checked;
GM_setValue('minRating', config.minRating);
GM_setValue('minReviews', config.minReviews);
GM_setValue('excludeNew', config.excludeNew);
GM_setValue('filterNoReviews', config.filterNoReviews);
GM_setValue('hideMode', config.hideMode);
GM_setValue('enableDarkMode', config.enableDarkMode);
window.location.reload();
});
}
// Add dark mode CSS
const darkModeCSS = `
html {
filter: invert(1) hue-rotate(180deg);
}
img, svg, iframe, video {
filter: invert(1) hue-rotate(180deg);
}
div[class*="image-tile-style_image-wrapper"] {
filter: invert(1) hue-rotate(180deg);
}
`;
function applyDarkMode() {
if (!config.enableDarkMode) return;
// Add dark mode CSS
if (!document.getElementById('je-dark-mode-style')) {
const style = document.createElement('style');
style.id = 'je-dark-mode-style';
style.textContent = darkModeCSS;
document.head.appendChild(style);
}
// Fix images loaded after DOM updates
const media = document.querySelectorAll('img, svg, iframe, video');
media.forEach(el => {
el.style.filter = 'invert(1) hue-rotate(180deg) !important';
});
}
// Function to parse numbers from text (e.g., for reviews count)
function parseNumber(text) {
if (!text) return 0;
const match = text.match(/\d+/g);
if (!match) return 0;
return parseInt(match.join(''), 10);
}
// Check if a restaurant is new
function isNewRestaurant(card) {
// Updated selector for the "New" tag, keeping old ones for fallback
return (
card.querySelector('pie-tag[data-qa="tag-new"]') ||
card.querySelector("div[class*='restaurant-card_new-tag__']") ||
card.querySelector('div[data-qa="tag-new"]')
);
}
// Process each restaurant card
function processRestaurantCard(card) {
if (card.dataset.processed === 'true') return;
try {
const anchor = card.querySelector('a');
if (!anchor) return;
const isNew = isNewRestaurant(card);
if (isNew && config.excludeNew) {
if (config.hideMode) {
card.parentElement.remove();
} else {
card.style.backgroundColor = '#ffcccc';
card.style.opacity = '0.3';
}
card.dataset.processed = 'true';
return;
}
const ratingDiv = anchor.querySelector("div[class*='restaurant-ratings_rating']");
// Updated selector for reviews, which are now in a <span>
const votesSpan = anchor.querySelector("span[class*='restaurant-ratings_votes']");
// If ratingDiv or votesSpan doesn't exist, it likely has no reviews
if (!ratingDiv || !votesSpan) {
if (config.filterNoReviews) {
// Apply the filter to restaurants with no reviews
if (config.hideMode) {
card.parentElement.remove();
} else {
card.style.backgroundColor = '#ffcccc';
card.style.opacity = '0.3';
}
}
card.dataset.processed = 'true';
return;
}
// parseFloat correctly extracts the leading number from a string like "5 Excellent"
const rating = parseFloat(ratingDiv.textContent.trim());
const reviews = parseNumber(votesSpan.textContent);
if (isNaN(rating) || isNaN(reviews)) return;
if (rating < config.minRating || reviews < config.minReviews) {
if (config.hideMode) {
card.parentElement.remove();
} else {
card.style.backgroundColor = '#ffcccc';
card.style.opacity = '0.5';
}
}
card.dataset.processed = 'true';
} catch (e) {
console.error("Error processing restaurant card:", e);
}
}
// Scan all restaurant cards on the page
function scanAllCards() {
const cards = document.querySelectorAll("div[class*='restaurant-card-shell_card']");
cards.forEach(processRestaurantCard);
}
// Check if tab is focused for reapplication of filters
function isTabFocused() {
return !document.hidden;
}
// Apply changes periodically
function reapplyFixes() {
scanAllCards();
applyDarkMode();
}
// Initialize the options menu and apply settings
createOptionsMenu();
// Repeat every 1s
setInterval(() => {
if (isTabFocused()) {
reapplyFixes();
}
}, 1000);
// Initial apply of settings
applyDarkMode();
})();