-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOldRedditNewProfilePictures-UniversalVersion.user.js
294 lines (278 loc) · 7.67 KB
/
OldRedditNewProfilePictures-UniversalVersion.user.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
286
287
288
289
290
291
292
293
294
// ==UserScript==
// @name Old Reddit with New Reddit Profile Pictures - Universal Version
// @namespace https://github.com/Nick2bad4u/UserStyles
// @version 4.2
// @description Injects new Reddit profile pictures into Old Reddit and Reddit-Stream.com next to the username. Caches in localstorage.
// @author Nick2bad4u
// @match *://*.reddit.com/*
// @match *://reddit-stream.com/*
// @connect reddit.com
// @connect reddit-stream.com
// @grant GM_xmlhttpRequest
// @updateURL https://github.com/Nick2bad4u/UserStyles/raw/refs/heads/main/OldRedditNewProfilePictures-UniversalVersion.user.js
// @downloadURL https://github.com/Nick2bad4u/UserStyles/raw/refs/heads/main/OldRedditNewProfilePictures-UniversalVersion.user.js
// @homepageURL https://github.com/Nick2bad4u/UserStyles
// @license Unlicense
// @resource https://www.google.com/s2/favicons?sz=64&domain=reddit.com
// @icon https://www.google.com/s2/favicons?sz=64&domain=reddit.com
// @icon64 https://www.google.com/s2/favicons?sz=64&domain=reddit.com
// @run-at document-start
// @tag reddit
// ==/UserScript==
(function () {
'use strict';
console.log('Script loaded');
let profilePictureCache = JSON.parse(
localStorage.getItem('profilePictureCache') ||
'{}',
);
let cacheTimestamps = JSON.parse(
localStorage.getItem('cacheTimestamps') ||
'{}',
);
const CACHE_DURATION = 7 * 24 * 60 * 60 * 1000; // 7 days in milliseconds
const MAX_CACHE_SIZE = 25000; // Maximum number of cache entries
function saveCache() {
localStorage.setItem(
'profilePictureCache',
JSON.stringify(profilePictureCache),
);
localStorage.setItem(
'cacheTimestamps',
JSON.stringify(cacheTimestamps),
);
}
function flushOldCache() {
console.log('Flushing old cache');
const now = Date.now();
for (const username in cacheTimestamps) {
if (
now - cacheTimestamps[username] >
CACHE_DURATION
) {
console.log(
`Deleting cache for ${username}`,
);
delete profilePictureCache[username];
delete cacheTimestamps[username];
}
}
saveCache();
console.log('Old cache entries flushed');
}
function limitCacheSize() {
const cacheEntries = Object.keys(
profilePictureCache,
);
if (cacheEntries.length > MAX_CACHE_SIZE) {
console.log(
'Cache size exceeded, removing oldest entries',
);
const sortedEntries = cacheEntries.sort(
(a, b) =>
cacheTimestamps[a] - cacheTimestamps[b],
);
const entriesToRemove = sortedEntries.slice(
0,
cacheEntries.length - MAX_CACHE_SIZE,
);
entriesToRemove.forEach((username) => {
delete profilePictureCache[username];
delete cacheTimestamps[username];
});
saveCache();
console.log('Cache size limited');
}
}
async function fetchProfilePictures(usernames) {
console.log('Fetching profile pictures');
const uncachedUsernames = usernames.filter(
(username) =>
!profilePictureCache[username] &&
username !== '[deleted]' &&
username !== '[removed]',
);
if (uncachedUsernames.length === 0) {
console.log('All usernames are cached');
return usernames.map(
(username) =>
profilePictureCache[username],
);
}
console.log(
`Fetching profile pictures for: ${uncachedUsernames.join(', ')}`,
);
const fetchPromises = uncachedUsernames.map(
async (username) => {
try {
const response = await fetch(
`https://www.reddit.com/user/${username}/about.json`,
);
if (!response.ok) {
console.error(
`Error fetching profile picture for ${username}: ${response.statusText}`,
);
return null;
}
const data = await response.json();
if (data.data && data.data.icon_img) {
const profilePictureUrl =
data.data.icon_img.split('?')[0];
profilePictureCache[username] =
profilePictureUrl;
cacheTimestamps[username] =
Date.now();
saveCache();
console.log(
`Fetched profile picture: ${username}`,
);
return profilePictureUrl;
} else {
console.warn(
`No profile picture found for: ${username}`,
);
return null;
}
} catch (error) {
console.error(
`Error fetching profile picture for ${username}:`,
error,
);
return null;
}
},
);
const results = await Promise.all(
fetchPromises,
);
limitCacheSize();
return usernames.map(
(username) => profilePictureCache[username],
);
}
async function injectProfilePictures(comments) {
console.log(
`Comments found: ${comments.length}`,
);
const usernames = Array.from(comments)
.map((comment) =>
comment.textContent.trim(),
)
.filter(
(username) =>
username !== '[deleted]' &&
username !== '[removed]',
);
const profilePictureUrls =
await fetchProfilePictures(usernames);
comments.forEach((comment, index) => {
const username = usernames[index];
const profilePictureUrl =
profilePictureUrls[index];
if (
profilePictureUrl &&
!comment.previousElementSibling?.classList.contains(
'profile-picture',
)
) {
console.log(
`Injecting profile picture: ${username}`,
);
const img = document.createElement('img');
img.src = profilePictureUrl;
img.classList.add('profile-picture');
img.onerror = () => {
img.style.display = 'none';
};
img.addEventListener('click', () => {
window.open(
profilePictureUrl,
'_blank',
);
});
comment.insertAdjacentElement(
'beforebegin',
img,
);
const enlargedImg =
document.createElement('img');
enlargedImg.src = profilePictureUrl;
enlargedImg.classList.add(
'enlarged-profile-picture',
);
document.body.appendChild(enlargedImg);
img.addEventListener('mouseover', () => {
enlargedImg.style.display = 'block';
const rect =
img.getBoundingClientRect();
enlargedImg.style.top = `${rect.top + window.scrollY + 20}px`;
enlargedImg.style.left = `${rect.left + window.scrollX + 20}px`;
});
img.addEventListener('mouseout', () => {
enlargedImg.style.display = 'none';
});
}
});
console.log('Profile pictures injected');
}
function setupObserver() {
console.log('Setting up observer');
const observer = new MutationObserver(
(mutations) => {
const comments =
document.querySelectorAll(
'.author, .c-username',
);
if (comments.length > 0) {
console.log('New comments detected');
observer.disconnect();
injectProfilePictures(comments);
}
},
);
observer.observe(document.body, {
childList: true,
subtree: true,
});
console.log('Observer initialized');
}
function runScript() {
flushOldCache();
console.log(
'Cache loaded:',
profilePictureCache,
);
setupObserver();
}
window.addEventListener('load', () => {
console.log('Page loaded');
runScript();
setInterval(runScript, 10000); // Run every 10 seconds
});
const style = document.createElement('style');
style.textContent = `
.profile-picture {
width: 20px;
height: 20px;
border-radius: 50%;
margin-right: 5px;
transition: transform 0.2s ease-in-out;
position: relative;
z-index: 1;
cursor: pointer;
}
.enlarged-profile-picture {
width: 250px;
height: 250px;
border-radius: 50%;
position: absolute;
display: none;
z-index: 1000;
pointer-events: none;
outline: 3px solid #000;
box-shadow: 0 4px 8px rgba(0, 0, 0, 1);
background-color: rgba(0, 0, 0, 1);
}
`;
document.head.appendChild(style);
})();