1
1
// ==UserScript==
2
2
// @name UnityForumFixer
3
3
// @namespace https://unitycoder.com/
4
- // @version 0.6 (27 .08.2024)
4
+ // @version 0.61 (28 .08.2024)
5
5
// @description Fixes For Unity Forums - https://github.com/unitycoder/UnityForumFixer
6
6
// @author unitycoder.com
7
7
// @match https://discussions.unity.com/latest
24
24
PostViewShowOriginalPosterInfo ( ) ;
25
25
TopicsViewCombineViewAndReplyCounts ( ) ;
26
26
OnMouseOverPostPreview ( ) ;
27
+ OnMouseOverLastPostPreview ( ) ;
27
28
28
29
setTimeout ( OnUpdate , 1000 ) ; // run loop to update activity times (since some script changes them back to original..)
29
30
} ) ;
@@ -134,7 +135,6 @@ function AppendCustomCSS()
134
135
.custom-user-creation-date {width:45px;margin-top:6px;font: 13px 'Inter', sans-serif !important; color: rgb(150, 150, 150);}
135
136
.custom-post-preview { position: absolute; max-width: 450px; max-height: 200px; background-color: white; border: 1px solid black; padding: 5px; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); z-index: 1000; }
136
137
137
-
138
138
` ;
139
139
document . head . appendChild ( style ) ;
140
140
}
@@ -286,8 +286,10 @@ function TopicsViewCombineViewAndReplyCounts()
286
286
}
287
287
}
288
288
289
- function FixPostActivityTime ( ) {
290
- document . querySelectorAll ( '.relative-date' ) . forEach ( function ( el ) {
289
+ function FixPostActivityTime ( )
290
+ {
291
+ document . querySelectorAll ( '.relative-date' ) . forEach ( function ( el )
292
+ {
291
293
const dataTime = parseInt ( el . getAttribute ( 'data-time' ) , 10 ) ;
292
294
if ( ! dataTime ) return ;
293
295
@@ -319,25 +321,54 @@ function FixPostActivityTime() {
319
321
} ) ;
320
322
}
321
323
322
- let prevTopicId = '' ; // Global variable to store the previously fetched topicId
323
- let currentTooltip = null ; // Global variable to store the currently visible tooltip
324
+ let prevOPTopicId = '' ; // Global variable to store the previously fetched topicId for orignal poster
325
+ let prevLastTopicId = '' ; // Global variable to store the previously fetched topicId for last poster
326
+ let currentTooltip = null ; // Global variable to store the currently visible tooltip
324
327
325
328
326
- // Initialize the mouseover event handler
327
- function OnMouseOverPostPreview ( ) {
328
- document . querySelectorAll ( 'a.title.raw-link.raw-topic-link[data-topic-id]' ) . forEach ( function ( element ) {
329
+ // Initialize the mouseover event handler
330
+ function OnMouseOverPostPreview ( )
331
+ {
332
+ document . querySelectorAll ( 'a.title.raw-link.raw-topic-link[data-topic-id]' ) . forEach ( function ( element )
333
+ {
329
334
const topicId = element . getAttribute ( 'data-topic-id' ) ;
330
335
331
336
// Add mouseover event listener to the <a> elements only
332
337
element . addEventListener ( 'mouseover' , function ( event ) {
333
- if ( topicId !== prevTopicId ) { // Check if the post data was already fetched
338
+ if ( topicId !== prevOPTopicId ) { // Check if the post data was already fetched
334
339
fetchPostDataAndShowTooltip ( event , topicId , element ) ;
335
340
}
336
341
} ) ;
337
342
338
343
// Add mouseout event listener to hide tooltip
339
- element . addEventListener ( 'mouseout' , function ( ) {
340
- hideTooltip ( ) ;
344
+ element . addEventListener ( 'mouseleave' , function ( ) {
345
+ console . log ( "hide OnMouseOverPostPreview" ) ;
346
+ hideTooltip ( ) ;
347
+ } ) ;
348
+ } ) ;
349
+ }
350
+
351
+
352
+ function OnMouseOverLastPostPreview ( )
353
+ {
354
+ document . querySelectorAll ( 'a.post-activity' ) . forEach ( function ( element )
355
+ {
356
+ const topicId = element . href . match ( / \/ t \/ [ ^ \/ ] + \/ ( \d + ) / ) [ 1 ] ; // Extract the topic ID from the href attribute
357
+ //console.log(">>>>> "+topicId);
358
+
359
+ // Add mouseover event listener to the <a> elements only
360
+ element . addEventListener ( 'mouseover' , function ( event )
361
+ {
362
+ console . log ( ">> mouseover OnMouseOverLastPostPreview" ) ;
363
+ if ( topicId !== prevLastTopicId ) { // Check if the post data was already fetched
364
+ fetchLastReplyDataAndShowTooltip ( event , topicId , element ) ;
365
+ }
366
+ } ) ;
367
+
368
+ // Add mouseleave event listener to hide tooltip
369
+ element . addEventListener ( 'mouseleave' , function ( ) {
370
+ console . log ( "<< mouseleave OnMouseOverLastPostPreview" ) ;
371
+ //hideTooltip();
341
372
} ) ;
342
373
} ) ;
343
374
}
@@ -356,7 +387,7 @@ function FixPostActivityTime() {
356
387
const plainText = stripHtmlTags ( postContent ) ;
357
388
358
389
// Update the global variable to store the fetched topicId
359
- prevTopicId = topicId ;
390
+ prevOPTopicId = topicId ;
360
391
361
392
// Create and position the tooltip based on the element's position
362
393
showTooltip ( element , plainText ) ;
@@ -366,8 +397,35 @@ function FixPostActivityTime() {
366
397
} ) ;
367
398
}
368
399
369
- // Function to create and show the tooltip
370
- function showTooltip ( element , content ) {
400
+ // now uses title attribute, had some issues opening tooltip in right position
401
+ function fetchLastReplyDataAndShowTooltip ( event , topicId , element )
402
+ {
403
+ const url = `https://discussions.unity.com/t/${ topicId } /posts.json` ;
404
+ // console.log("fetching: " + url);
405
+
406
+ fetch ( url )
407
+ . then ( response => response . json ( ) )
408
+ . then ( data => {
409
+ // Extract the last post from the post_stream array
410
+ const posts = data [ 'post_stream' ] [ 'posts' ] ;
411
+ const lastPostContent = posts [ posts . length - 1 ] [ 'cooked' ] ;
412
+ const postContent = lastPostContent . length > 350 ? lastPostContent . substring ( 0 , 350 ) + "..." : lastPostContent ;
413
+ const plainText = stripHtmlTags ( postContent ) ;
414
+
415
+ // Update the global variable to store the fetched topicId
416
+ prevLastTopicId = topicId ;
417
+
418
+ // Set the tooltip content as the title attribute of the element
419
+ element . setAttribute ( 'title' , plainText ) ;
420
+ } )
421
+ . catch ( error => {
422
+ console . error ( 'Error fetching last reply data:' , error ) ;
423
+ } ) ;
424
+ }
425
+
426
+ function showTooltip ( element , content )
427
+ {
428
+ console . log ( "show tooltip now! " + content )
371
429
hideTooltip ( ) ; // Ensure any existing tooltip is removed first
372
430
373
431
// Create a new tooltip element
@@ -379,20 +437,22 @@ function FixPostActivityTime() {
379
437
// Position the tooltip relative to the <a> element
380
438
currentTooltip . style . top = `${ window . scrollY + rect . top - currentTooltip . offsetHeight - 10 } px` ; // 10px above the element
381
439
currentTooltip . style . left = `${ window . scrollX + rect . left } px` ;
440
+
441
+ //console.log(element + " : "+currentTooltip.style.top+" , "+currentTooltip.style.left);
382
442
}
383
443
384
- // Function to hide the tooltip
385
- function hideTooltip ( ) {
444
+ function hideTooltip ( )
445
+ {
446
+ console . log ( "hide tooltip now" ) ;
386
447
if ( currentTooltip ) {
387
448
currentTooltip . remove ( ) ;
388
449
currentTooltip = null ;
389
450
}
390
451
}
391
452
392
- // Function to create a tooltip element
393
453
function createTooltip ( content ) {
394
454
const tooltip = document . createElement ( 'div' ) ;
395
- tooltip . className = 'custom-post-preview' ; // Assign the CSS class
455
+ tooltip . className = 'custom-post-preview' ;
396
456
tooltip . textContent = content ;
397
457
document . body . appendChild ( tooltip ) ;
398
458
return tooltip ;
@@ -440,7 +500,7 @@ function PostViewFetchOPDetails()
440
500
441
501
// Check if the current page URL has already been processed
442
502
if ( currentPageURL === prevPageURL ) {
443
- console . log ( `Skipping fetch for already processed page URL: ${ currentPageURL } ` ) ;
503
+ // console.log(`Skipping fetch for already processed page URL: ${currentPageURL}`);
444
504
return ; // Skip execution if the URL has already been processed
445
505
}
446
506
0 commit comments