Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Click on Search Input Field and Select Subreddit
- // @namespace http://tampermonkey.net/
- // @version 1.2
- // @description Clicks on the search input field, opens the dropdown, and selects the subreddit link, retrying if the search input is not found immediately
- // @author Paradox109
- // @match *://*/*
- // @grant none
- // ==/UserScript==
- (function() {
- 'use strict';
- // Define the subreddit name as a variable
- const subredditName = 'LivestreamFail';
- // Check if the current page URL matches the desired URL
- if (window.location.href !== 'https://new.reddit.com/message/messages') {
- console.log('Not on the correct page, exiting script.');
- return; // Exit the script if not on the correct page
- }
- let retryInterval; // Variable to hold the interval ID
- // Function to click on the search input field
- function clickSearchInput() {
- const searchInput = document.querySelector('.h-jI8r2f9ozTNqu_2TBeY');
- if (searchInput) {
- searchInput.focus(); // Focus the input field
- searchInput.click(); // Click the input field
- console.log('Search input field clicked.');
- // Stop retrying and perform actions after opening the dropdown
- clearInterval(retryInterval);
- openDropdownMenu();
- } else {
- console.log('Search input field not found, retrying...');
- }
- }
- // Function to click on the dropdown menu and wait for it to appear
- function openDropdownMenu() {
- const dropdownButton = document.querySelector('button.h-jI8r2f9ozTNqu_2TBeY');
- if (dropdownButton) {
- dropdownButton.click(); // Click the dropdown button
- console.log('Dropdown menu clicked.');
- // Wait for the dropdown menu to appear and then select the subreddit link
- waitForDropdownToAppear();
- } else {
- console.log('Dropdown button not found.');
- }
- }
- // Function to wait for the dropdown menu to appear and then click the subreddit link
- function waitForDropdownToAppear() {
- const observer = new MutationObserver((mutationsList) => {
- for (const mutation of mutationsList) {
- if (mutation.type === 'childList') {
- const menu = document.querySelector('[role="menu"]');
- if (menu) {
- console.log('Dropdown menu appeared.');
- // Look for the subreddit link and click it
- const subredditLink = menu.querySelector(`a[href="/r/${subredditName}/"]`);
- if (subredditLink) {
- subredditLink.click(); // Click the subreddit link
- console.log(`r/${subredditName} link clicked.`);
- observer.disconnect(); // Stop observing after the action is done
- clearInterval(retryInterval); // Stop retrying
- } else {
- console.log(`Subreddit link for r/${subredditName} not found.`);
- }
- }
- }
- }
- });
- // Start observing for changes in the document body
- observer.observe(document.body, { childList: true, subtree: true });
- }
- // Function to start the retry loop for clicking the search input field
- function startRetryLoop() {
- retryInterval = setInterval(clickSearchInput, 100); // Retry every 1 second
- }
- // Wait for the page to load completely before executing the script
- window.addEventListener('load', function() {
- // Start the retry loop after the page has loaded
- startRetryLoop();
- });
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement