|
1 | 1 | // ==UserScript==
|
2 | 2 | // @name UnityForumFixer
|
3 | 3 | // @namespace https://unitycoder.com/
|
4 |
| -// @version 0.52 (26.08.2024) |
| 4 | +// @version 0.6 (27.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
|
|
23 | 23 | FixPostActivityTime();
|
24 | 24 | PostViewShowOriginalPosterInfo();
|
25 | 25 | TopicsViewCombineViewAndReplyCounts();
|
| 26 | + OnMouseOverPostPreview(); |
26 | 27 |
|
27 | 28 | setTimeout(OnUpdate, 1000); // run loop to update activity times (since some script changes them back to original..)
|
28 | 29 | });
|
@@ -131,6 +132,8 @@ function AppendCustomCSS()
|
131 | 132 | .combined-views-number {color: var(--primary); margin-left: auto;text-align: right;}
|
132 | 133 | .custom-post-username {margin-bottom:3px;color: var(--primary);}
|
133 | 134 | .custom-user-creation-date {width:45px;margin-top:6px;font: 13px 'Inter', sans-serif !important; color: rgb(150, 150, 150);}
|
| 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; } |
| 136 | + |
134 | 137 |
|
135 | 138 | `;
|
136 | 139 | document.head.appendChild(style);
|
@@ -316,6 +319,91 @@ function FixPostActivityTime() {
|
316 | 319 | });
|
317 | 320 | }
|
318 | 321 |
|
| 322 | + let prevTopicId = ''; // Global variable to store the previously fetched topicId |
| 323 | + let currentTooltip = null; // Global variable to store the currently visible tooltip |
| 324 | + |
| 325 | + |
| 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 | + const topicId = element.getAttribute('data-topic-id'); |
| 330 | + |
| 331 | + // Add mouseover event listener to the <a> elements only |
| 332 | + element.addEventListener('mouseover', function (event) { |
| 333 | + if (topicId !== prevTopicId) { // Check if the post data was already fetched |
| 334 | + fetchPostDataAndShowTooltip(event, topicId, element); |
| 335 | + } |
| 336 | + }); |
| 337 | + |
| 338 | + // Add mouseout event listener to hide tooltip |
| 339 | + element.addEventListener('mouseout', function () { |
| 340 | + hideTooltip(); |
| 341 | + }); |
| 342 | + }); |
| 343 | + } |
| 344 | + |
| 345 | + // Function to fetch data and display tooltip |
| 346 | + function fetchPostDataAndShowTooltip(event, topicId, element) |
| 347 | + { |
| 348 | + const url = `https://discussions.unity.com/t/${topicId}.json`; |
| 349 | + |
| 350 | + fetch(url) |
| 351 | + .then(response => response.json()) |
| 352 | + .then(data => { |
| 353 | + // Extract necessary data from JSON (limit to 250 characters) |
| 354 | + const rawPostContent = data['post_stream']['posts'][0]['cooked']; |
| 355 | + const postContent = rawPostContent.length > 250 ? rawPostContent.substring(0, 250) + "..." : rawPostContent; |
| 356 | + const plainText = stripHtmlTags(postContent); |
| 357 | + |
| 358 | + // Update the global variable to store the fetched topicId |
| 359 | + prevTopicId = topicId; |
| 360 | + |
| 361 | + // Create and position the tooltip based on the element's position |
| 362 | + showTooltip(element, plainText); |
| 363 | + }) |
| 364 | + .catch(error => { |
| 365 | + console.error('Error fetching post data:', error); |
| 366 | + }); |
| 367 | + } |
| 368 | + |
| 369 | + // Function to create and show the tooltip |
| 370 | + function showTooltip(element, content) { |
| 371 | + hideTooltip(); // Ensure any existing tooltip is removed first |
| 372 | + |
| 373 | + // Create a new tooltip element |
| 374 | + currentTooltip = createTooltip(content); |
| 375 | + |
| 376 | + // Get the bounding rectangle of the <a> element |
| 377 | + const rect = element.getBoundingClientRect(); |
| 378 | + |
| 379 | + // Position the tooltip relative to the <a> element |
| 380 | + currentTooltip.style.top = `${window.scrollY + rect.top - currentTooltip.offsetHeight - 10}px`; // 10px above the element |
| 381 | + currentTooltip.style.left = `${window.scrollX + rect.left}px`; |
| 382 | + } |
| 383 | + |
| 384 | + // Function to hide the tooltip |
| 385 | + function hideTooltip() { |
| 386 | + if (currentTooltip) { |
| 387 | + currentTooltip.remove(); |
| 388 | + currentTooltip = null; |
| 389 | + } |
| 390 | + } |
| 391 | + |
| 392 | + // Function to create a tooltip element |
| 393 | + function createTooltip(content) { |
| 394 | + const tooltip = document.createElement('div'); |
| 395 | + tooltip.className = 'custom-post-preview'; // Assign the CSS class |
| 396 | + tooltip.textContent = content; |
| 397 | + document.body.appendChild(tooltip); |
| 398 | + return tooltip; |
| 399 | + } |
| 400 | + |
| 401 | + function stripHtmlTags(html) |
| 402 | + { |
| 403 | + const tempDiv = document.createElement("div"); // Create a temporary <div> element |
| 404 | + tempDiv.innerHTML = html; // Set its inner HTML to the input HTML string |
| 405 | + return tempDiv.textContent || tempDiv.innerText || ""; // Return the text content without HTML tags |
| 406 | + } |
319 | 407 |
|
320 | 408 |
|
321 | 409 | // POST VIEW
|
@@ -464,3 +552,4 @@ function formatDateString(date)
|
464 | 552 | return date.toLocaleDateString('en-GB', { day: '2-digit', month: 'short', year: 'numeric' });
|
465 | 553 | }
|
466 | 554 | }
|
| 555 | + |
0 commit comments