Skip to content

Commit 7fa0b0f

Browse files
authored
fix forum post preview tooltip (hide on mouseover, if its stuck)
1 parent 740e2f7 commit 7fa0b0f

File tree

1 file changed

+22
-80
lines changed

1 file changed

+22
-80
lines changed

unity-forum-fixer.js

Lines changed: 22 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name UnityForumFixer
33
// @namespace https://unitycoder.com/
4-
// @version 0.61 (28.08.2024)
4+
// @version 0.7 (19.09.2024)
55
// @description Fixes For Unity Forums - https://github.com/unitycoder/UnityForumFixer
66
// @author unitycoder.com
77
// @match https://discussions.unity.com/latest
@@ -24,7 +24,6 @@
2424
PostViewShowOriginalPosterInfo();
2525
TopicsViewCombineViewAndReplyCounts();
2626
OnMouseOverPostPreview();
27-
OnMouseOverLastPostPreview();
2827

2928
setTimeout(OnUpdate, 1000); // run loop to update activity times (since some script changes them back to original..)
3029
});
@@ -135,6 +134,7 @@ function AppendCustomCSS()
135134
.custom-user-creation-date {width:45px;margin-top:6px;font: 13px 'Inter', sans-serif !important; color: rgb(150, 150, 150);}
136135
.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; }
137136
137+
138138
`;
139139
document.head.appendChild(style);
140140
}
@@ -286,10 +286,8 @@ function TopicsViewCombineViewAndReplyCounts()
286286
}
287287
}
288288

289-
function FixPostActivityTime()
290-
{
291-
document.querySelectorAll('.relative-date').forEach(function (el)
292-
{
289+
function FixPostActivityTime() {
290+
document.querySelectorAll('.relative-date').forEach(function (el) {
293291
const dataTime = parseInt(el.getAttribute('data-time'), 10);
294292
if (!dataTime) return;
295293

@@ -321,55 +319,24 @@ function FixPostActivityTime()
321319
});
322320
}
323321

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
327324

328325

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) {
334329
const topicId = element.getAttribute('data-topic-id');
335330

336331
// Add mouseover event listener to the <a> elements only
337332
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
339334
fetchPostDataAndShowTooltip(event, topicId, element);
340335
}
341336
});
342337

343338
// 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();});
373340
});
374341
}
375342

@@ -387,7 +354,7 @@ function FixPostActivityTime()
387354
const plainText = stripHtmlTags(postContent);
388355

389356
// Update the global variable to store the fetched topicId
390-
prevOPTopicId = topicId;
357+
prevTopicId = topicId;
391358

392359
// Create and position the tooltip based on the element's position
393360
showTooltip(element, plainText);
@@ -397,35 +364,8 @@ function FixPostActivityTime()
397364
});
398365
}
399366

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) {
429369
hideTooltip(); // Ensure any existing tooltip is removed first
430370

431371
// Create a new tooltip element
@@ -438,21 +378,23 @@ function FixPostActivityTime()
438378
currentTooltip.style.top = `${window.scrollY + rect.top - currentTooltip.offsetHeight - 10}px`; // 10px above the element
439379
currentTooltip.style.left = `${window.scrollX + rect.left}px`;
440380

441-
//console.log(element + " : "+currentTooltip.style.top+" , "+currentTooltip.style.left);
381+
currentTooltip.addEventListener('mouseover', function () {
382+
hideTooltip();
383+
});
442384
}
443385

444-
function hideTooltip()
445-
{
446-
console.log("hide tooltip now");
386+
// Function to hide the tooltip
387+
function hideTooltip() {
447388
if (currentTooltip) {
448389
currentTooltip.remove();
449390
currentTooltip = null;
450391
}
451392
}
452393

394+
// Function to create a tooltip element
453395
function createTooltip(content) {
454396
const tooltip = document.createElement('div');
455-
tooltip.className = 'custom-post-preview';
397+
tooltip.className = 'custom-post-preview'; // Assign the CSS class
456398
tooltip.textContent = content;
457399
document.body.appendChild(tooltip);
458400
return tooltip;
@@ -500,7 +442,7 @@ function PostViewFetchOPDetails()
500442

501443
// Check if the current page URL has already been processed
502444
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}`);
504446
return; // Skip execution if the URL has already been processed
505447
}
506448

0 commit comments

Comments
 (0)