-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbsky_profile_latest_posts.js
More file actions
418 lines (353 loc) 路 12.2 KB
/
Copy pathbsky_profile_latest_posts.js
File metadata and controls
418 lines (353 loc) 路 12.2 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
javascript: (function() {
/* @title: Show BlueSky Latest Posts on Hover */
/* @description: Displays latest posts from a BlueSky user when hovering over their profile */
/* @domains: bsky.app */
console.log('Starting Bluesky Profile Hover Posts Viewer...');
/* Remove existing styles if present */
let existingStyles = document.getElementById('bsky-hover-styles');
if (existingStyles) {
existingStyles.remove();
}
/* Create styles matching native Bsky theme */
const styles = `
#bsky-hover-dialog {
position: fixed;
background: #16202A;
border: 1px solid #2A3F4F;
border-radius: 12px;
z-index: 10000;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.32);
font-family: InterVariable, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
color: #F1F3F5;
width: 400px;
max-width: 90vw;
max-height: 500px;
overflow: hidden;
pointer-events: none;
opacity: 0;
transition: opacity 0.2s ease;
}
#bsky-hover-dialog.visible {
pointer-events: auto;
opacity: 1;
}
#bsky-hover-header {
padding: 16px;
border-bottom: 1px solid #2A3F4F;
display: flex;
align-items: center;
gap: 12px;
}
#bsky-hover-avatar {
width: 32px;
height: 32px;
border-radius: 50%;
object-fit: cover;
border: 1px solid #2A3F4F;
}
#bsky-hover-user {
flex: 1;
}
#bsky-hover-name {
margin: 0;
font-size: 15px;
font-weight: 600;
color: #F1F3F5;
}
#bsky-hover-handle {
margin: 0;
font-size: 13px;
color: #93ADC8;
}
#bsky-hover-content {
padding: 0;
overflow-y: auto;
max-height: 400px;
}
.bsky-post-item {
padding: 16px;
border-bottom: 1px solid #2A3F4F;
}
.bsky-post-item:last-child {
border-bottom: none;
}
.bsky-post-date {
font-size: 12px;
color: #788E9F;
margin-bottom: 8px;
}
.bsky-post-text {
color: #F1F3F5;
font-size: 14px;
line-height: 1.4;
margin: 0;
word-wrap: break-word;
}
.bsky-post-stats {
display: flex;
gap: 16px;
margin-top: 12px;
font-size: 12px;
color: #788E9F;
}
.bsky-post-stat {
display: flex;
align-items: center;
gap: 4px;
}
.bsky-loading {
text-align: center;
color: #788E9F;
padding: 20px;
}
.bsky-loading-icon {
font-size: 16px;
margin-bottom: 8px;
animation: spin 1s linear infinite;
}
.bsky-empty {
text-align: center;
color: #788E9F;
padding: 20px;
}
.bsky-error {
text-align: center;
color: #EF4444;
padding: 20px;
}
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
`;
/* Add styles to page */
const styleElement = document.createElement('style');
styleElement.id = 'bsky-hover-styles';
styleElement.textContent = styles;
document.head.appendChild(styleElement);
/* Create dialog element */
let dialog = document.createElement('div');
dialog.id = 'bsky-hover-dialog';
document.body.appendChild(dialog);
let currentHoverTimeout;
let currentProfile = null;
let hoverEnabled = true;
/* Function to show/hide hover functionality */
function toggleHoverFunctionality() {
hoverEnabled = !hoverEnabled;
if (!hoverEnabled) {
hideDialog();
if (currentHoverTimeout) {
clearTimeout(currentHoverTimeout);
}
console.log('Bluesky hover functionality disabled. Press Esc again to re-enable.');
} else {
console.log('Bluesky hover functionality enabled.');
}
}
/* Function to extract profile identifier from link */
function extractProfileIdentifier(linkElement) {
const href = linkElement.getAttribute('href');
if (!href) return null;
const match = href.match(/\/profile\/([^\/\?]+)/);
return match ? match[1] : null;
}
/* Function to format relative time */
function formatRelativeTime(dateString) {
const date = new Date(dateString);
const now = new Date();
const diffMs = now - date;
const diffMins = Math.floor(diffMs / (1000 * 60));
const diffHours = Math.floor(diffMs / (1000 * 60 * 60));
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
if (diffMins < 1) return 'now';
if (diffMins < 60) return `${diffMins}m`;
if (diffHours < 24) return `${diffHours}h`;
if (diffDays < 7) return `${diffDays}d`;
return date.toLocaleDateString();
}
/* Function to fetch and display posts */
async function showProfilePosts(profileId, mouseX, mouseY, profileElement) {
if (!hoverEnabled || currentProfile === profileId) return; /* Skip if disabled or duplicate */
currentProfile = profileId;
console.log('Fetching posts for profile:', profileId);
/* Position dialog to the right of cursor */
dialog.style.left = `${mouseX + 20}px`;
dialog.style.top = `${mouseY - 50}px`;
/* Adjust position if dialog would go off screen */
const dialogRect = dialog.getBoundingClientRect();
const windowWidth = window.innerWidth;
const windowHeight = window.innerHeight;
if (mouseX + 420 > windowWidth) {
dialog.style.left = `${mouseX - 420}px`;
}
if (mouseY + 400 > windowHeight) {
dialog.style.top = `${windowHeight - 450}px`;
}
/* Extract profile info from hovered element */
const avatarImg = profileElement.querySelector('img[alt*="avatar"], img[data-testid="userAvatarImage"]');
const nameElement = profileElement.querySelector('[style*="font-weight: 600"], [class*="font-weight"]');
const handleElement = profileElement.querySelector('[dir="auto"]:last-child, [style*="color: rgb(147, 165, 183)"]');
const avatarSrc = avatarImg ? avatarImg.src : '';
const displayName = nameElement ? nameElement.textContent.trim() : profileId;
const handle = handleElement ? handleElement.textContent.trim() : `@${profileId}`;
/* Show loading state */
dialog.innerHTML = `
<div id="bsky-hover-header">
${avatarSrc ? `<img id="bsky-hover-avatar" src="${avatarSrc}" alt="Avatar">` : ''}
<div id="bsky-hover-user">
<h3 id="bsky-hover-name">${displayName}</h3>
<p id="bsky-hover-handle">${handle}</p>
</div>
</div>
<div id="bsky-hover-content">
<div class="bsky-loading">
<div class="bsky-loading-icon">馃攧</div>
<div>Loading posts...</div>
</div>
</div>
`;
dialog.classList.add('visible');
try {
const response = await fetch(`https://public.api.bsky.app/xrpc/app.bsky.feed.getAuthorFeed?actor=${profileId}&limit=5&filter=posts_no_replies`);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const data = await response.json();
console.log('Posts data:', data);
const content = document.getElementById('bsky-hover-content');
if (!data.feed || data.feed.length === 0) {
content.innerHTML = `
<div class="bsky-empty">
<p>No recent posts found.</p>
</div>
`;
return;
}
const postsHtml = data.feed.map(item => {
const post = item.post;
const record = post.record;
const stats = {
replies: post.replyCount || 0,
reposts: post.repostCount || 0,
likes: post.likeCount || 0,
quotes: post.quoteCount || 0
};
const relativeTime = formatRelativeTime(record.createdAt);
const postText = record.text || 'No text content';
return `
<div class="bsky-post-item">
<div class="bsky-post-date">${relativeTime}</div>
<p class="bsky-post-text">${postText}</p>
<div class="bsky-post-stats">
<span class="bsky-post-stat">馃挰 ${stats.replies}</span>
<span class="bsky-post-stat">馃攧 ${stats.reposts}</span>
<span class="bsky-post-stat">鉂わ笍 ${stats.likes}</span>
${stats.quotes > 0 ? `<span class="bsky-post-stat">馃挱 ${stats.quotes}</span>` : ''}
</div>
</div>
`;
}).join('');
content.innerHTML = postsHtml;
} catch (error) {
console.error('Error fetching posts:', error);
const content = document.getElementById('bsky-hover-content');
content.innerHTML = `
<div class="bsky-error">
<p><strong>Error loading posts</strong></p>
<p style="font-size: 12px;">${error.message}</p>
</div>
`;
}
}
/* Function to hide dialog */
function hideDialog() {
dialog.classList.remove('visible');
currentProfile = null;
}
/* Add hover listeners to profile links */
function addHoverListeners() {
const profileLinks = document.querySelectorAll('a[href*="/profile/"]');
console.log(`Found ${profileLinks.length} profile links`);
profileLinks.forEach(link => {
if (link.dataset.bskyHoverAdded) return; /* Skip if already processed */
link.dataset.bskyHoverAdded = 'true';
link.addEventListener('mouseenter', function(e) {
if (!hoverEnabled) return; /* Skip if hover is disabled */
const profileId = extractProfileIdentifier(this);
if (!profileId) return;
/* Clear any existing timeout */
if (currentHoverTimeout) {
clearTimeout(currentHoverTimeout);
}
/* Set timeout to show dialog after brief delay */
currentHoverTimeout = setTimeout(() => {
showProfilePosts(profileId, e.clientX, e.clientY, this);
}, 500);
});
link.addEventListener('mouseleave', function(e) {
/* Clear timeout if mouse leaves before delay */
if (currentHoverTimeout) {
clearTimeout(currentHoverTimeout);
}
/* Hide dialog after short delay */
setTimeout(() => {
const dialogRect = dialog.getBoundingClientRect();
const mouseX = e.clientX;
const mouseY = e.clientY;
/* Only hide if mouse is not over dialog */
if (mouseX < dialogRect.left || mouseX > dialogRect.right ||
mouseY < dialogRect.top || mouseY > dialogRect.bottom) {
hideDialog();
}
}, 100);
});
});
}
/* Add escape key listener with capture and priority handling */
function handleEscapeKey(e) {
if (e.key === 'Escape') {
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
toggleHoverFunctionality();
}
}
document.addEventListener('keydown', handleEscapeKey, true); /* Use capture phase */
document.addEventListener('keyup', handleEscapeKey, true); /* Also capture keyup */
/* Add listener for dialog mouse events */
dialog.addEventListener('mouseenter', function() {
/* Keep dialog visible when hovering over it */
if (currentHoverTimeout) {
clearTimeout(currentHoverTimeout);
}
});
dialog.addEventListener('mouseleave', function() {
hideDialog();
});
/* Initial setup */
addHoverListeners();
/* Re-run when page content changes (for dynamic loading) */
const observer = new MutationObserver(function(mutations) {
let shouldUpdate = false;
mutations.forEach(function(mutation) {
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
shouldUpdate = true;
}
});
if (shouldUpdate) {
setTimeout(addHoverListeners, 100);
}
});
observer.observe(document.body, { childList: true, subtree: true });
console.log('Bluesky Profile Hover Posts Viewer initialized! Press Esc to toggle hover functionality.');
/* Cleanup function (for development) */
window.bskyHoverCleanup = function() {
observer.disconnect();
document.removeEventListener('keydown', handleEscapeKey, true);
document.removeEventListener('keyup', handleEscapeKey, true);
dialog.remove();
styleElement.remove();
console.log('Bluesky Profile Hover Posts Viewer cleaned up');
};
})();