|
/** |
|
* ποΈ HOW TO INSTRUCTIONS |
|
* 1. Open Claude Desktop |
|
* 2. Go to Help -> Enable Developer Mode |
|
* 3. Navigate Developer Tools window named "Developer Tools - https://claude.ai" |
|
* 4. Go to "Console" tab |
|
* 5. Type "allow pasting" and hit Enter |
|
* 6. Paste this snippet and hit Enter |
|
* |
|
* Created by @rvanbaalen |
|
* Inpired by @RafalWilinski / original version of auto-approve script: |
|
* https://gist.github.com/RafalWilinski/3416a497f94ee2a0c589a8d930304950 |
|
*/ |
|
|
|
// --- Configuration & Registry --- |
|
|
|
// Global cooldown tracking |
|
let lastActionTime = 0; |
|
const GLOBAL_COOLDOWN_MS = 2000; // 2 seconds cooldown |
|
|
|
// Initialize the registry on the window object if it doesn't exist |
|
window.autoActionsRegistry = window.autoActionsRegistry || []; |
|
|
|
// --- Action Class Definitions --- |
|
|
|
// (Optional) Base class for structure - useful for type hinting or shared logic later |
|
class BaseAction { |
|
constructor(name) { |
|
if (!name) throw new Error("Action must have a name."); |
|
this.name = name; |
|
} |
|
|
|
// Checks if conditions are met. Returns data needed for execute() or falsy if not met. |
|
check() { |
|
console.warn(`Action "${this.name}" is missing check() implementation.`); |
|
return false; |
|
} |
|
|
|
// Performs the action using data from check(). |
|
execute(data) { |
|
console.warn(`Action "${this.name}" is missing execute() implementation.`); |
|
} |
|
} |
|
|
|
if (window.myMutationObserver) { |
|
console.log("Disconnecting previous observer..."); |
|
window.myMutationObserver.disconnect(); |
|
} |
|
|
|
console.log("Setting up new Mutation Observer..."); |
|
|
|
const observer = new MutationObserver((mutations) => { |
|
const now = Date.now(); |
|
// Check global cooldown |
|
if (now - lastActionTime < GLOBAL_COOLDOWN_MS) { |
|
console.log("π Global cooldown active, skipping mutation check."); |
|
return; |
|
} |
|
|
|
// Iterate through registered actions |
|
for (const actionInstance of window.autoActionsRegistry) { |
|
try { |
|
// Check if the action's conditions are met |
|
const actionData = actionInstance.check(); |
|
|
|
if (actionData) { |
|
console.log(`β
[${actionInstance.name}] Conditions met. Preparing to execute.`); |
|
// Conditions met, execute the action |
|
actionInstance.execute(actionData); |
|
|
|
// Set global cooldown |
|
lastActionTime = now; |
|
console.log(`β±οΈ [${actionInstance.name}] Action executed. Cooldown started.`); |
|
|
|
// IMPORTANT: Stop checking other actions for this mutation batch |
|
// Prevents multiple actions firing for the same change and respects cooldown |
|
break; |
|
} else { |
|
// console.log(`- [${actionInstance.name}] Conditions not met.`); // Can be noisy |
|
} |
|
} catch (error) { |
|
console.error(`Error during action check/execute for "${actionInstance.name}":`, error); |
|
// Optionally continue to the next action or break, depending on desired robustness |
|
// continue; |
|
} |
|
} |
|
}); |
|
|
|
// Start observing the document body for subtree and child list changes |
|
observer.observe(document.body, { |
|
childList: true, |
|
subtree: true, |
|
}); |
|
|
|
// Store the observer instance on window to manage it later (e.g., disconnect) |
|
window.myMutationObserver = observer; |
|
|
|
console.log("β
Observer started. Watching for changes..."); |
|
console.log("Registered actions:", window.autoActionsRegistry.map(a => a.name)); |