Guest User

greasemonkey snippet

a guest
May 2nd, 2025
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.40 KB | Source Code | 0 0
  1. [CODE=javascript]// ==UserScript==
  2.  
  3. // @name         Auto-Click & Save Code Block
  4.  
  5. // @namespace    http://tampermonkey.net/
  6.  
  7. // @version      1.0
  8.  
  9. // @description  Clicks a button every 10 seconds, extracts code block content, and saves it to a file
  10.  
  11. // @match        <YOUR WEBSITE URL>
  12.  
  13. // @author         ashk
  14.  
  15. // @grant        GM_download
  16.  
  17. // ==/UserScript==
  18.  
  19.  
  20. (function() {
  21.  
  22.     'use strict';
  23.  
  24.  
  25.     // Configuration: Adjust these selectors as needed
  26.  
  27.     const BUTTON_SELECTOR = "button"; // CSS selector for the button to click
  28.  
  29.     const CODE_BLOCK_SELECTOR = "code"; // CSS selector for the code block to extract
  30.  
  31.     const SAVE_INTERVAL = 10000; // 10 seconds (in milliseconds)
  32.  
  33.     const FILE_NAME = "code_snippet.txt"; // Output filename
  34.  
  35.  
  36.     function extractAndSaveCode() {
  37.  
  38.         try {
  39.  
  40.             // Find the button and click it
  41.  
  42.             const button = document.querySelector(BUTTON_SELECTOR);
  43.  
  44.             if (button) {
  45.  
  46.                 button.click();
  47.  
  48.                 console.log("Button clicked.");
  49.  
  50.             } else {
  51.  
  52.                 console.warn("Button not found with selector:", BUTTON_SELECTOR);
  53.  
  54.                 return;
  55.  
  56.             }
  57.  
  58.  
  59.             // Wait a moment for the code block to appear (adjust delay if needed)
  60.  
  61.             setTimeout(() => {
  62.  
  63.                 const codeBlock = document.querySelector(CODE_BLOCK_SELECTOR);
  64.  
  65.                 if (codeBlock) {
  66.  
  67.                     const codeContent = codeBlock.textContent;
  68.  
  69.                     console.log("Extracted code:", codeContent);
  70.  
  71.  
  72.                     // Save to file (Tampermonkey/Greasemonkey)
  73.  
  74.                     GM_download({
  75.  
  76.                         url: "data:text/plain;charset=utf-8," + encodeURIComponent(codeContent),
  77.  
  78.                         name: FILE_NAME,
  79.  
  80.                         saveAs: true // Lets user choose where to save
  81.  
  82.                     });
  83.  
  84.                 } else {
  85.  
  86.                     console.warn("Code block not found with selector:", CODE_BLOCK_SELECTOR);
  87.  
  88.                 }
  89.  
  90.             }, 1000); // Wait 1 second for the code block to load
  91.  
  92.         } catch (err) {
  93.  
  94.             console.error("Error:", err);
  95.  
  96.         }
  97.  
  98.     }
  99.  
  100.  
  101.     // Run every 10 seconds
  102.  
  103.     setInterval(extractAndSaveCode, SAVE_INTERVAL);
  104.  
  105.     console.log("Auto-click & save script started. Interval:", SAVE_INTERVAL / 1000, "seconds");
  106.  
  107. })();[/CODE]
Tags: greasemonkey
Advertisement
Add Comment
Please, Sign In to add comment