Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [CODE=javascript]// ==UserScript==
- // @name Auto-Click & Save Code Block
- // @namespace http://tampermonkey.net/
- // @version 1.0
- // @description Clicks a button every 10 seconds, extracts code block content, and saves it to a file
- // @match <YOUR WEBSITE URL>
- // @author ashk
- // @grant GM_download
- // ==/UserScript==
- (function() {
- 'use strict';
- // Configuration: Adjust these selectors as needed
- const BUTTON_SELECTOR = "button"; // CSS selector for the button to click
- const CODE_BLOCK_SELECTOR = "code"; // CSS selector for the code block to extract
- const SAVE_INTERVAL = 10000; // 10 seconds (in milliseconds)
- const FILE_NAME = "code_snippet.txt"; // Output filename
- function extractAndSaveCode() {
- try {
- // Find the button and click it
- const button = document.querySelector(BUTTON_SELECTOR);
- if (button) {
- button.click();
- console.log("Button clicked.");
- } else {
- console.warn("Button not found with selector:", BUTTON_SELECTOR);
- return;
- }
- // Wait a moment for the code block to appear (adjust delay if needed)
- setTimeout(() => {
- const codeBlock = document.querySelector(CODE_BLOCK_SELECTOR);
- if (codeBlock) {
- const codeContent = codeBlock.textContent;
- console.log("Extracted code:", codeContent);
- // Save to file (Tampermonkey/Greasemonkey)
- GM_download({
- url: "data:text/plain;charset=utf-8," + encodeURIComponent(codeContent),
- name: FILE_NAME,
- saveAs: true // Lets user choose where to save
- });
- } else {
- console.warn("Code block not found with selector:", CODE_BLOCK_SELECTOR);
- }
- }, 1000); // Wait 1 second for the code block to load
- } catch (err) {
- console.error("Error:", err);
- }
- }
- // Run every 10 seconds
- setInterval(extractAndSaveCode, SAVE_INTERVAL);
- console.log("Auto-click & save script started. Interval:", SAVE_INTERVAL / 1000, "seconds");
- })();[/CODE]
Advertisement
Add Comment
Please, Sign In to add comment