-
Notifications
You must be signed in to change notification settings - Fork 0
/
recent-reviews.js
285 lines (261 loc) · 9.98 KB
/
recent-reviews.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
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
// Constants
const TIME_PERIODS = ['total', 'inPastYear', 'inPastMonth'];
const PERCENT_COLORS = [
{ pct: 0.0, color: { r: 0xff, g: 0x00, b: 0 } },
{ pct: 0.5, color: { r: 0xff, g: 0xff, b: 0 } },
{ pct: 1.0, color: { r: 0x00, g: 0xff, b: 0 } },
];
// State
let reviewMap = {};
let currentOption = 'total';
let fullPercentage = null;
let isScrolling = false;
let lastPercentage = null;
let lastPlaceName = '';
const reviewData = {
reviewsScores: Object.fromEntries(TIME_PERIODS.map((period) => [period, 0])),
trustedReviews: Object.fromEntries(TIME_PERIODS.map((period) => [period, 0])),
totalReviews: Object.fromEntries(TIME_PERIODS.map((period) => [period, 0])),
};
// Helper functions
const reset = () => {
reviewMap = {};
Object.values(reviewData).forEach((data) => TIME_PERIODS.forEach((period) => (data[period] = 0)));
fullPercentage = calculateFullPercentage();
};
const getColorForPercentage = (pct) => {
const i = PERCENT_COLORS.findIndex((color) => pct <= color.pct);
const [lower, upper] = [
PERCENT_COLORS[Math.max(0, i - 1)],
PERCENT_COLORS[Math.min(PERCENT_COLORS.length - 1, i)],
];
const rangePct = (pct - lower.pct) / (upper.pct - lower.pct);
const color = ['r', 'g', 'b'].reduce((acc, key) => {
acc[key] = Math.round(lower.color[key] + rangePct * (upper.color[key] - lower.color[key]));
return acc;
}, {});
return { textColor: 'black', backgroundColor: `rgb(${color.r},${color.g},${color.b})` };
};
const applyColors = (element, percentage, isTrusted = false) => {
const styles = isTrusted
? { color: 'black', backgroundColor: percentage < 0.35 ? 'red' : 'aquamarine' }
: getColorForPercentage(percentage);
Object.assign(element.style, styles);
};
const processReview = (review) => {
const { rating, reviewerNumberOfReviews } = review;
const isTrusted = reviewerNumberOfReviews > 2;
TIME_PERIODS.forEach((period) => {
if (period === 'total' || review[period]) {
reviewData.totalReviews[period]++;
if (isTrusted) {
reviewData.trustedReviews[period]++;
reviewData.reviewsScores[period] += rating === 5 ? 1 : rating === 1 ? -1 : 0;
}
}
});
};
const parseReviews = (reviews) => {
Array.from(reviews).forEach((review) => {
const reviewId = review.getAttribute('data-review-id');
if (reviewMap[reviewId]) return;
const rating =
review
.querySelector('[role="img"].kvMYJc')
?.getAttribute('aria-label')
.match(/(\d)(?<!stars)/)?.[0] ||
review.querySelector('span.fzvQIb')?.innerText.match(/\d/)?.[0];
const reviewerNumberOfReviews =
review.querySelector('.RfnDt')?.innerText.match(/(\d+)/)?.[0] || 1;
const reviewDate = review.querySelector('.rsqaWe, .xRkPPb')?.innerText || '';
const reviewData = {
rating: parseInt(rating),
reviewerNumberOfReviews: parseInt(reviewerNumberOfReviews),
inPastYear: !reviewDate.includes('year'),
inPastMonth: !reviewDate.includes('year') && !reviewDate.includes('month'),
};
processReview(reviewData);
reviewMap[reviewId] = reviewData;
});
updateUI();
};
const getReviewScorePercentage = () => {
const { reviewsScores, trustedReviews } = reviewData;
return reviewsScores[currentOption] / trustedReviews[currentOption] || 0;
};
const calculateFullPercentage = () => {
const reviewRows = document.querySelectorAll('tr[role="img"]');
if (reviewRows.length < 5) return null;
const extractNumber = (str) => {
const match = str.match(/(\d+(?:[.,]\d+)*)\s*(?:reviews?|$)/);
return match ? parseInt(match[1].replace(/[.,]/g, ''), 10) : 0;
};
const fiveStars = extractNumber(reviewRows[0].getAttribute('aria-label'));
const oneStars = extractNumber(reviewRows[4].getAttribute('aria-label'));
const allReviews = extractNumber(document.querySelector('.jANrlb>.fontBodySmall')?.innerText);
if (!allReviews) return null;
return Math.round(((fiveStars - oneStars) / allReviews) * 100);
};
// UI functions
const createUIElements = () => {
const container = document.createElement('div');
container.id = 'reviews-container';
['reviews-score', 'trusted-reviews', 'review-trend'].forEach((id) => {
const element = document.createElement('div');
element.id = id;
container.appendChild(element);
});
const controlsContainer = document.createElement('div');
controlsContainer.className = 'controls-container';
const selectElement = document.createElement('select');
selectElement.id = 'select-option';
TIME_PERIODS.forEach((option) => {
const optionElement = document.createElement('option');
optionElement.value = option;
optionElement.innerText =
option === 'total' ? 'Total' : option === 'inPastYear' ? 'Past Year' : 'Past Month';
selectElement.appendChild(optionElement);
});
selectElement.onchange = (e) => {
currentOption = e.target.value;
updateUI();
};
controlsContainer.appendChild(selectElement);
const scrollButtonsContainer = document.createElement('div');
scrollButtonsContainer.className = 'scroll-buttons';
['Auto-scroll (⌥)', '↑'].forEach((text, index) => {
const button = document.createElement('button');
button.id = index === 0 ? 'scroll-button' : 'scroll-to-top-button';
button.innerText = text;
button.onclick = index === 0 ? scrollUntilStabilized : scrollToFirstReview;
scrollButtonsContainer.appendChild(button);
});
controlsContainer.appendChild(scrollButtonsContainer);
container.appendChild(controlsContainer);
document.body.appendChild(container);
};
const updateUI = () => {
const scoreElement = document.querySelector('#reviews-score');
const trustedElement = document.querySelector('#trusted-reviews');
const trendElement = document.querySelector('#review-trend');
if (!scoreElement) {
createUIElements();
return updateUI();
}
const { reviewsScores, trustedReviews, totalReviews } = reviewData;
const recentReviewScorePercentage = getReviewScorePercentage();
scoreElement.innerText = `${Math.round(
reviewsScores[currentOption] * recentReviewScorePercentage
)} - ${Math.round(recentReviewScorePercentage * 100)}%`;
trustedElement.innerText = `${
trustedReviews[currentOption]
} trusted reviews in this period (${Math.round(
(trustedReviews[currentOption] / totalReviews[currentOption]) * 100
)}%)`;
applyColors(scoreElement, recentReviewScorePercentage);
applyColors(trustedElement, trustedReviews[currentOption] / totalReviews[currentOption], true);
if (fullPercentage !== null) {
const recentPercentage = Math.round(recentReviewScorePercentage * 100);
const difference = recentPercentage - fullPercentage;
const trendText = difference >= 0 ? 'Positive' : 'Negative';
const trendColor =
difference >= 0
? `rgb(0, ${Math.min(255, 128 + Math.abs(difference) * 5)}, 0)`
: `rgb(${Math.min(255, 128 + Math.abs(difference) * 5)}, 0, 0)`;
trendElement.innerText = `${trendText} recent reviews trend (${
difference >= 0 ? '+' : ''
}${difference}%)`;
trendElement.style.backgroundColor = trendColor;
trendElement.classList.remove('trend-unavailable');
} else {
trendElement.innerText = 'Recent trend data not available';
trendElement.classList.add('trend-unavailable');
}
};
// Main logic
const queueReviews = () => {
const reviews = document.querySelectorAll('.jftiEf.fontBodyMedium');
const newReviews = Array.from(reviews).filter(
(review) => !reviewMap[review.getAttribute('data-review-id')]
);
if (newReviews.length) parseReviews(newReviews);
};
const observer = new MutationObserver(() => {
const placeName = location.href.match(/(?:place\/)([^\/]+)/)?.[1];
const reviews = document.querySelectorAll('.jftiEf.fontBodyMedium');
if (reviews.length > 4) {
if (placeName !== lastPlaceName) {
lastPlaceName = placeName;
reset();
}
queueReviews();
} else {
reset();
document.querySelector('#reviews-container')?.remove();
}
});
// Scrolling functions
const scrollToLastReview = () => {
const reviews = document.querySelectorAll('[data-review-id]');
const lastReview = [...reviews].pop();
if (lastReview) lastReview.scrollIntoView();
else console.error('No review found');
};
const checkStabilization = (currentPercentage) => {
if (lastPercentage === null) {
lastPercentage = currentPercentage;
return false;
}
const isStabilized = Math.abs(currentPercentage - lastPercentage) <= 1;
lastPercentage = currentPercentage;
return isStabilized;
};
const waitForNewReviews = async (timeout = 5000) => {
const startTime = Date.now();
const initialReviewCount = document.querySelectorAll('[data-review-id]').length;
while (Date.now() - startTime < timeout) {
await new Promise((resolve) => setTimeout(resolve, 100));
if (document.querySelectorAll('[data-review-id]').length > initialReviewCount) return true;
}
return false;
};
const scrollUntilStabilized = async () => {
if (isScrolling) return;
isScrolling = true;
lastPercentage = null;
while (isScrolling) {
scrollToLastReview();
const newReviewsAppeared = await waitForNewReviews();
if (!newReviewsAppeared) {
console.log('No new reviews appeared after 5 seconds. Stopping scroll.');
break;
}
queueReviews();
const newPercentage = Math.round(getReviewScorePercentage() * 100);
if (checkStabilization(newPercentage)) break;
updateUI();
await new Promise((resolve) => setTimeout(resolve, 100));
}
isScrolling = false;
updateUI();
console.log('Scrolling finished. Reviews have stabilized or all reviews have been loaded.');
};
const scrollToFirstReview = () => {
document.querySelector('.fontDisplayLarge')?.scrollIntoView();
};
// Initialize
const loadStyles = () => {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = 'styles.css';
document.head.appendChild(link);
};
loadStyles();
observer.observe(document.body, { childList: true, subtree: true });
// Update keyboard shortcut for Mac
document.addEventListener('keydown', (event) => {
if (event.altKey) {
event.preventDefault();
scrollUntilStabilized();
}
});