Skip to content

Commit b20580e

Browse files
authored
Update unity-forum-fixer.js
Fix closing pinned topics (unity forum bug)
1 parent 8a43836 commit b20580e

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

unity-forum-fixer.js

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// ==UserScript==
22
// @name UnityForumFixer
33
// @namespace https://unitycoder.com/
4-
// @version 0.81 (21.12.2024)
4+
// @version 0.82 (27.01.2025)
55
// @description Fixes For Unity Forums - https://github.com/unitycoder/UnityForumFixer
66
// @author unitycoder.com
77
// @match https://discussions.unity.com/latest
88
// @match https://discussions.unity.com/t/*
9+
// @require https://code.jquery.com/jquery-3.6.0.min.js
910
// ==/UserScript==
1011

1112

@@ -25,6 +26,8 @@
2526
TopicsViewCombineViewAndReplyCounts();
2627
OnMouseOverPostPreview();
2728
AddOnHoverOpenNotificationPanel();
29+
FixPinButton();
30+
2831

2932
// update notification panel icons
3033
const currentUserButton = document.getElementById('toggle-current-user');
@@ -675,6 +678,72 @@ function AddOnHoverOpenNotificationPanel() {
675678
}
676679

677680

681+
function FixPinButton() {
682+
// Select all buttons with the .topic-status class
683+
const buttons = document.querySelectorAll('.topic-statuses .topic-status');
684+
685+
// Get the CSRF token from the meta tags in the page head
686+
const csrfToken = document.querySelector('meta[name="csrf-token"]')?.content;
687+
688+
// Check if the CSRF token is available
689+
if (!csrfToken) {
690+
console.error('CSRF token not found');
691+
return;
692+
}
693+
694+
if (buttons.length > 0) {
695+
buttons.forEach((button) => {
696+
// Add a click event listener to each button
697+
button.addEventListener('click', function (event) {
698+
event.preventDefault(); // Prevent the default link behavior
699+
700+
// Find the post ID by looking for the nearest <a> tag with a valid href
701+
const postLink = button.closest('td').querySelector('a.title');
702+
const postId = postLink?.getAttribute('data-topic-id');
703+
704+
if (!postId) {
705+
console.error('Post ID not found for this button');
706+
return;
707+
}
708+
709+
console.log('Post ID:', postId); // Debug log to confirm the ID
710+
711+
// Construct the PUT request URL using the extracted ID
712+
const url = `https://discussions.unity.com/t/${postId}/clear-pin`;
713+
714+
// Send a PUT request with the CSRF token
715+
$.ajax({
716+
url: url,
717+
type: 'PUT',
718+
headers: {
719+
'X-CSRF-Token': csrfToken, // Include the CSRF token
720+
},
721+
success: function (response) {
722+
console.log('PUT request successful:', response);
723+
724+
// Hide the parent row of the clicked button
725+
const parentRow = button.closest('tr');
726+
if (parentRow) {
727+
parentRow.style.display = 'none'; // Hides the row
728+
console.log(`Row with Post ID ${postId} hidden.`);
729+
} else {
730+
console.error('Parent row not found.');
731+
}
732+
},
733+
error: function (xhr, status, error) {
734+
console.error('XHR Status:', xhr.status);
735+
console.error('XHR Response Text:', xhr.responseText);
736+
console.error('Error:', error || 'Unknown error');
737+
alert('Error sending request.');
738+
},
739+
});
740+
});
741+
});
742+
} else {
743+
console.error('No notification buttons found.');
744+
}
745+
}
746+
678747

679748

680749
// HELPER METHODS

0 commit comments

Comments
 (0)