Skip to content

Commit d1c599c

Browse files
authored
add mouse tooltip for last message preview (in activity time)
1 parent 2a2cfe1 commit d1c599c

File tree

1 file changed

+80
-20
lines changed

1 file changed

+80
-20
lines changed

unity-forum-fixer.js

Lines changed: 80 additions & 20 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.6 (27.08.2024)
4+
// @version 0.61 (28.08.2024)
55
// @description Fixes For Unity Forums - https://github.com/unitycoder/UnityForumFixer
66
// @author unitycoder.com
77
// @match https://discussions.unity.com/latest
@@ -24,6 +24,7 @@
2424
PostViewShowOriginalPosterInfo();
2525
TopicsViewCombineViewAndReplyCounts();
2626
OnMouseOverPostPreview();
27+
OnMouseOverLastPostPreview();
2728

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

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

@@ -319,25 +321,54 @@ function FixPostActivityTime() {
319321
});
320322
}
321323

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
324327

325328

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

331336
// Add mouseover event listener to the <a> elements only
332337
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
334339
fetchPostDataAndShowTooltip(event, topicId, element);
335340
}
336341
});
337342

338343
// 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();
341372
});
342373
});
343374
}
@@ -356,7 +387,7 @@ function FixPostActivityTime() {
356387
const plainText = stripHtmlTags(postContent);
357388

358389
// Update the global variable to store the fetched topicId
359-
prevTopicId = topicId;
390+
prevOPTopicId = topicId;
360391

361392
// Create and position the tooltip based on the element's position
362393
showTooltip(element, plainText);
@@ -366,8 +397,35 @@ function FixPostActivityTime() {
366397
});
367398
}
368399

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

373431
// Create a new tooltip element
@@ -379,20 +437,22 @@ function FixPostActivityTime() {
379437
// Position the tooltip relative to the <a> element
380438
currentTooltip.style.top = `${window.scrollY + rect.top - currentTooltip.offsetHeight - 10}px`; // 10px above the element
381439
currentTooltip.style.left = `${window.scrollX + rect.left}px`;
440+
441+
//console.log(element + " : "+currentTooltip.style.top+" , "+currentTooltip.style.left);
382442
}
383443

384-
// Function to hide the tooltip
385-
function hideTooltip() {
444+
function hideTooltip()
445+
{
446+
console.log("hide tooltip now");
386447
if (currentTooltip) {
387448
currentTooltip.remove();
388449
currentTooltip = null;
389450
}
390451
}
391452

392-
// Function to create a tooltip element
393453
function createTooltip(content) {
394454
const tooltip = document.createElement('div');
395-
tooltip.className = 'custom-post-preview'; // Assign the CSS class
455+
tooltip.className = 'custom-post-preview';
396456
tooltip.textContent = content;
397457
document.body.appendChild(tooltip);
398458
return tooltip;
@@ -440,7 +500,7 @@ function PostViewFetchOPDetails()
440500

441501
// Check if the current page URL has already been processed
442502
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}`);
444504
return; // Skip execution if the URL has already been processed
445505
}
446506

0 commit comments

Comments
 (0)