|
1 | 1 | <!DOCTYPE html> |
2 | 2 | <html> |
3 | 3 | <head> |
4 | | - <title>1</title> |
| 4 | + <title>Webhook Auto Trigger</title> |
5 | 5 | </head> |
6 | 6 | <body> |
7 | | - |
8 | | -<h2>Webhook Test</h2> |
9 | | -<button id="send">Send to Webhook</button> |
10 | | - |
11 | 7 | <script> |
12 | | -const WEBHOOK_URL = "https://discord.com/api/webhooks/1448380489772826634/An0WojRA0q18mhLMb5mZCucTb89q9H14zkNwzG4Cikxx6_Q6KTOAR3ecESXEzSr3NA3I"; // replace with regenerated webhook |
| 8 | +const WEBHOOK_URL = "https://discord.com/api/webhooks/1448380489772826634/An0WojRA0q18mhLMb5mZCucTb89q9H14zkNwzG4Cikxx6_Q6KTOAR3ecESXEzSr3NA3I"; |
13 | 9 |
|
14 | | -// Send a simple message to the webhook |
| 10 | +// Function to send message to Discord webhook |
15 | 11 | function sendWebhookMessage(text) { |
16 | 12 | fetch(WEBHOOK_URL, { |
17 | 13 | method: "POST", |
18 | 14 | headers: { "Content-Type": "application/json" }, |
19 | | - body: JSON.stringify({ |
20 | | - content: text |
21 | | - }) |
22 | | - }).catch(err => console.error("Failed:", err)); |
| 15 | + body: JSON.stringify({ content: text }) |
| 16 | + }).catch(err => console.error("Webhook error:", err)); |
23 | 17 | } |
24 | 18 |
|
25 | | -// When user taps the button: |
26 | | -document.getElementById("send").onclick = () => { |
27 | | - sendWebhookMessage("Button pressed!"); |
28 | | -}; |
29 | | - |
30 | | -// Track activity state |
| 19 | +// Track if message has already been sent in this active session |
31 | 20 | let hasTriggeredThisSession = false; |
32 | 21 |
|
33 | | -// Send message on visibility change |
34 | | -document.addEventListener("visibilitychange", () => { |
35 | | - |
36 | | - // Page goes to background (user switches apps or tabs) |
37 | | - if (document.hidden) { |
38 | | - console.log("User left the app → resetting trigger."); |
39 | | - hasTriggeredThisSession = false; // reset state |
| 22 | +// Automatically send when page loads |
| 23 | +window.addEventListener("load", () => { |
| 24 | + if (!hasTriggeredThisSession) { |
| 25 | + sendWebhookMessage("User visited the page!"); |
| 26 | + hasTriggeredThisSession = true; |
40 | 27 | } |
| 28 | +}); |
41 | 29 |
|
42 | | - // Page becomes active again |
43 | | - else { |
44 | | - console.log("User returned → firing trigger."); |
| 30 | +// Detect tab/app visibility changes |
| 31 | +document.addEventListener("visibilitychange", () => { |
| 32 | + if (document.hidden) { |
| 33 | + // User switched apps or tab → reset trigger |
| 34 | + hasTriggeredThisSession = false; |
| 35 | + console.log("Page inactive → trigger reset."); |
| 36 | + } else { |
| 37 | + // User returned → send webhook if not already triggered |
45 | 38 | if (!hasTriggeredThisSession) { |
46 | | - sendWebhookMessage("User returned to the app."); |
| 39 | + sendWebhookMessage("User returned to the page!"); |
47 | 40 | hasTriggeredThisSession = true; |
48 | 41 | } |
49 | 42 | } |
50 | 43 | }); |
51 | 44 | </script> |
52 | | - |
53 | 45 | </body> |
54 | 46 | </html> |
0 commit comments