Advertisement
Guest User

Tampermonkey script

a guest
Aug 28th, 2024
860
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Click on Search Input Field and Select Subreddit
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.2
  5. // @description Clicks on the search input field, opens the dropdown, and selects the subreddit link, retrying if the search input is not found immediately
  6. // @author Paradox109
  7. // @match *://*/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // Define the subreddit name as a variable
  15. const subredditName = 'LivestreamFail';
  16.  
  17. // Check if the current page URL matches the desired URL
  18. if (window.location.href !== 'https://new.reddit.com/message/messages') {
  19. console.log('Not on the correct page, exiting script.');
  20. return; // Exit the script if not on the correct page
  21. }
  22.  
  23. let retryInterval; // Variable to hold the interval ID
  24.  
  25. // Function to click on the search input field
  26. function clickSearchInput() {
  27. const searchInput = document.querySelector('.h-jI8r2f9ozTNqu_2TBeY');
  28.  
  29. if (searchInput) {
  30. searchInput.focus(); // Focus the input field
  31. searchInput.click(); // Click the input field
  32. console.log('Search input field clicked.');
  33.  
  34. // Stop retrying and perform actions after opening the dropdown
  35. clearInterval(retryInterval);
  36. openDropdownMenu();
  37. } else {
  38. console.log('Search input field not found, retrying...');
  39. }
  40. }
  41.  
  42. // Function to click on the dropdown menu and wait for it to appear
  43. function openDropdownMenu() {
  44. const dropdownButton = document.querySelector('button.h-jI8r2f9ozTNqu_2TBeY');
  45.  
  46. if (dropdownButton) {
  47. dropdownButton.click(); // Click the dropdown button
  48. console.log('Dropdown menu clicked.');
  49.  
  50. // Wait for the dropdown menu to appear and then select the subreddit link
  51. waitForDropdownToAppear();
  52. } else {
  53. console.log('Dropdown button not found.');
  54. }
  55. }
  56.  
  57. // Function to wait for the dropdown menu to appear and then click the subreddit link
  58. function waitForDropdownToAppear() {
  59. const observer = new MutationObserver((mutationsList) => {
  60. for (const mutation of mutationsList) {
  61. if (mutation.type === 'childList') {
  62. const menu = document.querySelector('[role="menu"]');
  63. if (menu) {
  64. console.log('Dropdown menu appeared.');
  65.  
  66. // Look for the subreddit link and click it
  67. const subredditLink = menu.querySelector(`a[href="/r/${subredditName}/"]`);
  68. if (subredditLink) {
  69. subredditLink.click(); // Click the subreddit link
  70. console.log(`r/${subredditName} link clicked.`);
  71. observer.disconnect(); // Stop observing after the action is done
  72. clearInterval(retryInterval); // Stop retrying
  73. } else {
  74. console.log(`Subreddit link for r/${subredditName} not found.`);
  75. }
  76. }
  77. }
  78. }
  79. });
  80.  
  81. // Start observing for changes in the document body
  82. observer.observe(document.body, { childList: true, subtree: true });
  83. }
  84.  
  85. // Function to start the retry loop for clicking the search input field
  86. function startRetryLoop() {
  87. retryInterval = setInterval(clickSearchInput, 100); // Retry every 1 second
  88. }
  89.  
  90. // Wait for the page to load completely before executing the script
  91. window.addEventListener('load', function() {
  92. // Start the retry loop after the page has loaded
  93. startRetryLoop();
  94. });
  95.  
  96. })();
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement