Guest User

Untitled

a guest
Jan 30th, 2025
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Reddit Approve Button
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.3
  5. // @description Adds an "Approve" button next to user comments and an "Approve User" button on old.reddit.com
  6. // @author Kimi
  7. // @match https://old.reddit.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // Function to show a toast notification
  15. function showToast(message, duration = 3000) {
  16. const toast = document.createElement('div');
  17. toast.textContent = message;
  18. toast.style.position = 'fixed';
  19. toast.style.bottom = '20px';
  20. toast.style.right = '20px';
  21. toast.style.padding = '10px';
  22. toast.style.margin = '10px';
  23. toast.style.borderRadius = '5px';
  24. toast.style.background = 'rgba(0, 0, 0, 0.7)';
  25. toast.style.color = 'white';
  26. toast.style.zIndex = '9999';
  27. toast.style.maxWidth = '300px';
  28. toast.style.textAlign = 'center';
  29. toast.style.transition = 'opacity 0.5s';
  30.  
  31. document.body.appendChild(toast);
  32.  
  33. setTimeout(() => {
  34. toast.style.opacity = '0';
  35. setTimeout(() => document.body.removeChild(toast), 500);
  36. }, duration);
  37. }
  38.  
  39. // Function to approve a comment
  40. async function approveComment(fullname) {
  41. try {
  42. const modhash = await fetch('/api/me.json', { credentials: 'include' })
  43. .then(response => response.json())
  44. .then(data => data.data.modhash);
  45.  
  46. const response = await fetch('/api/approve', {
  47. method: 'POST',
  48. headers: {
  49. 'Content-Type': 'application/x-www-form-urlencoded',
  50. },
  51. body: `id=${fullname}&uh=${modhash}`,
  52. credentials: 'include',
  53. });
  54.  
  55. if (response.ok) {
  56. showToast('Comment approved!', 2000);
  57. } else {
  58. showToast('Failed to approve comment.', 2000);
  59. }
  60. } catch (error) {
  61. console.error('Error approving comment:', error);
  62. showToast('Error approving comment. Check console for details.', 2000);
  63. }
  64. }
  65.  
  66. // Function to approve a user
  67. async function approveUser(username, subreddit) {
  68. try {
  69. const modhash = await fetch('/api/me.json', { credentials: 'include' })
  70. .then(response => response.json())
  71. .then(data => data.data.modhash);
  72.  
  73. const response = await fetch(`/r/${subreddit}/api/approve`, {
  74. method: 'POST',
  75. headers: {
  76. 'Content-Type': 'application/x-www-form-urlencoded',
  77. },
  78. body: `name=${username}&uh=${modhash}`,
  79. credentials: 'include',
  80. });
  81.  
  82. if (response.ok) {
  83. showToast('User approved!', 2000);
  84. } else {
  85. showToast('Failed to approve user.', 2000);
  86. }
  87. } catch (error) {
  88. console.error('Error approving user:', error);
  89. showToast('Error approving user. Check console for details.', 2000);
  90. }
  91. }
  92.  
  93. // Function to inject approve buttons
  94. function injectApproveButtons() {
  95. document.querySelectorAll('.comment .tagline a.author').forEach(authorLink => {
  96. const comment = authorLink.closest('.comment');
  97. const fullname = comment.getAttribute('data-fullname');
  98. const existingButton = comment.querySelector('.approve-button');
  99. if (existingButton) return; // Skip if the button already exists
  100.  
  101. const approveButton = document.createElement('button');
  102. approveButton.textContent = 'Approve';
  103. approveButton.className = 'approve-button'; // Add a unique class
  104. approveButton.style.marginLeft = '5px';
  105. approveButton.style.padding = '2px 5px';
  106. approveButton.style.border = '1px solid #555';
  107. approveButton.style.borderRadius = '3px';
  108. approveButton.style.background = '#333';
  109. approveButton.style.color = 'white';
  110. approveButton.style.cursor = 'pointer';
  111. approveButton.addEventListener('click', () => approveComment(fullname));
  112. authorLink.parentNode.insertBefore(approveButton, authorLink.nextSibling);
  113. });
  114. }
  115.  
  116. // Function to inject approve user buttons
  117. function injectApproveUserButtons() {
  118. document.querySelectorAll('.comment .tagline a.author').forEach(authorLink => {
  119. const comment = authorLink.closest('.comment');
  120. const username = authorLink.textContent.trim();
  121. const subreddit = window.location.pathname.split('/')[2];
  122. const existingButton = comment.querySelector('.approve-user-button');
  123. if (existingButton) return; // Skip if the button already exists
  124.  
  125. const approveUserButton = document.createElement('button');
  126. approveUserButton.textContent = 'Approve User';
  127. approveUserButton.className = 'approve-user-button'; // Add a unique class
  128. approveUserButton.style.marginLeft = '5px';
  129. approveUserButton.style.padding = '2px 5px';
  130. approveUserButton.style.border = '1px solid #555';
  131. approveUserButton.style.borderRadius = '3px';
  132. approveUserButton.style.background = '#333';
  133. approveUserButton.style.color = 'white';
  134. approveUserButton.style.cursor = 'pointer';
  135. approveUserButton.addEventListener('click', () => approveUser(username, subreddit));
  136. authorLink.parentNode.insertBefore(approveUserButton, authorLink.nextSibling);
  137. });
  138. }
  139.  
  140. // Inject buttons on page load
  141. injectApproveButtons();
  142. injectApproveUserButtons();
  143.  
  144. // Inject buttons on new comments loaded via AJAX
  145. const observer = new MutationObserver(() => {
  146. injectApproveButtons();
  147. injectApproveUserButtons();
  148. });
  149. observer.observe(document.body, {
  150. childList: true,
  151. subtree: true,
  152. });
  153.  
  154. // Ensure the observer is disconnected when the page is unloaded
  155. window.addEventListener('beforeunload', () => observer.disconnect());
  156. })();
Advertisement
Add Comment
Please, Sign In to add comment