1
1
// ==UserScript==
2
2
// @name UnityForumFixer
3
3
// @namespace https://unitycoder.com/
4
- // @version 0.61 (28.08 .2024)
4
+ // @version 0.7 (19.09 .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 ( ) ;
28
27
29
28
setTimeout ( OnUpdate , 1000 ) ; // run loop to update activity times (since some script changes them back to original..)
30
29
} ) ;
@@ -135,6 +134,7 @@ function AppendCustomCSS()
135
134
.custom-user-creation-date {width:45px;margin-top:6px;font: 13px 'Inter', sans-serif !important; color: rgb(150, 150, 150);}
136
135
.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; }
137
136
137
+
138
138
` ;
139
139
document . head . appendChild ( style ) ;
140
140
}
@@ -286,10 +286,8 @@ function TopicsViewCombineViewAndReplyCounts()
286
286
}
287
287
}
288
288
289
- function FixPostActivityTime ( )
290
- {
291
- document . querySelectorAll ( '.relative-date' ) . forEach ( function ( el )
292
- {
289
+ function FixPostActivityTime ( ) {
290
+ document . querySelectorAll ( '.relative-date' ) . forEach ( function ( el ) {
293
291
const dataTime = parseInt ( el . getAttribute ( 'data-time' ) , 10 ) ;
294
292
if ( ! dataTime ) return ;
295
293
@@ -321,55 +319,24 @@ function FixPostActivityTime()
321
319
} ) ;
322
320
}
323
321
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
322
+ let prevTopicId = '' ; // Global variable to store the previously fetched topicId
323
+ let currentTooltip = null ; // Global variable to store the currently visible tooltip
327
324
328
325
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
- {
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 ) {
334
329
const topicId = element . getAttribute ( 'data-topic-id' ) ;
335
330
336
331
// Add mouseover event listener to the <a> elements only
337
332
element . addEventListener ( 'mouseover' , function ( event ) {
338
- if ( topicId !== prevOPTopicId ) { // Check if the post data was already fetched
333
+ if ( topicId !== prevTopicId ) { // Check if the post data was already fetched
339
334
fetchPostDataAndShowTooltip ( event , topicId , element ) ;
340
335
}
341
336
} ) ;
342
337
343
338
// Add mouseout event listener to hide tooltip
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();
372
- } ) ;
339
+ element . addEventListener ( 'mouseout' , function ( ) { hideTooltip ( ) ; } ) ;
373
340
} ) ;
374
341
}
375
342
@@ -387,7 +354,7 @@ function FixPostActivityTime()
387
354
const plainText = stripHtmlTags ( postContent ) ;
388
355
389
356
// Update the global variable to store the fetched topicId
390
- prevOPTopicId = topicId ;
357
+ prevTopicId = topicId ;
391
358
392
359
// Create and position the tooltip based on the element's position
393
360
showTooltip ( element , plainText ) ;
@@ -397,35 +364,8 @@ function FixPostActivityTime()
397
364
} ) ;
398
365
}
399
366
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 )
367
+ // Function to create and show the tooltip
368
+ function showTooltip ( element , content ) {
429
369
hideTooltip ( ) ; // Ensure any existing tooltip is removed first
430
370
431
371
// Create a new tooltip element
@@ -438,21 +378,23 @@ function FixPostActivityTime()
438
378
currentTooltip . style . top = `${ window . scrollY + rect . top - currentTooltip . offsetHeight - 10 } px` ; // 10px above the element
439
379
currentTooltip . style . left = `${ window . scrollX + rect . left } px` ;
440
380
441
- //console.log(element + " : "+currentTooltip.style.top+" , "+currentTooltip.style.left);
381
+ currentTooltip . addEventListener ( 'mouseover' , function ( ) {
382
+ hideTooltip ( ) ;
383
+ } ) ;
442
384
}
443
385
444
- function hideTooltip ( )
445
- {
446
- console . log ( "hide tooltip now" ) ;
386
+ // Function to hide the tooltip
387
+ function hideTooltip ( ) {
447
388
if ( currentTooltip ) {
448
389
currentTooltip . remove ( ) ;
449
390
currentTooltip = null ;
450
391
}
451
392
}
452
393
394
+ // Function to create a tooltip element
453
395
function createTooltip ( content ) {
454
396
const tooltip = document . createElement ( 'div' ) ;
455
- tooltip . className = 'custom-post-preview' ;
397
+ tooltip . className = 'custom-post-preview' ; // Assign the CSS class
456
398
tooltip . textContent = content ;
457
399
document . body . appendChild ( tooltip ) ;
458
400
return tooltip ;
@@ -500,7 +442,7 @@ function PostViewFetchOPDetails()
500
442
501
443
// Check if the current page URL has already been processed
502
444
if ( currentPageURL === prevPageURL ) {
503
- // console.log(`Skipping fetch for already processed page URL: ${currentPageURL}`);
445
+ console . log ( `Skipping fetch for already processed page URL: ${ currentPageURL } ` ) ;
504
446
return ; // Skip execution if the URL has already been processed
505
447
}
506
448
0 commit comments