Skip to content

Commit 1ab1022

Browse files
committed
edit
1 parent 8fea819 commit 1ab1022

File tree

3 files changed

+67
-44
lines changed

3 files changed

+67
-44
lines changed

html/error-template.html

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
.footer { color: #aaa; font-size: 14px; margin-top: 40px; }
1414
.report-btn { background: #ef4444; color: white; border: none; border-radius: 8px; padding: 10px 20px; font-size: 16px; cursor: pointer; margin-top: 20px; }
1515
.report-btn:hover { background: #dc2626; }
16+
.report-btn[data-enabled="false"] { display: none; } /* Hide button if reporting is disabled */
1617
</style>
1718
</head>
1819
<body>
@@ -22,7 +23,7 @@
2223
<div id="error-message" class="error-message">ERROR_MESSAGE</div>
2324
<img class="gif" src="ERROR_GIF" alt="Error illustration">
2425
<!-- New report button -->
25-
<button id="open-report-modal" class="report-btn" data-link="" data-site="">Signaler cette erreur</button>
26+
<button id="open-report-modal" class="report-btn" data-link="" data-site="" data-enabled="false">Signaler cette erreur</button>
2627
</div>
2728
<!-- Embedded front-end script for modal and reporting -->
2829
<script>
@@ -175,18 +176,32 @@
175176
}
176177
// ====== wire the button ======
177178
const wireButton = () => {
178-
ensureModal()
179-
const btn = document.getElementById('open-report-modal')
180-
if (!btn) return
181-
btn.addEventListener('click', e => {
182-
e.preventDefault()
183-
showModal()
184-
})
185-
// Set dynamic attributes
186-
btn.setAttribute('data-link', location.href)
187-
btn.setAttribute('data-site', location.hostname)
188-
}
189-
document.addEventListener('DOMContentLoaded', wireButton)
179+
ensureModal();
180+
const btn = document.getElementById('open-report-modal');
181+
if (!btn) return;
182+
183+
// Dynamically set the button's visibility based on ENABLE_REPORTING_ERRORS
184+
fetch('/worker/api/env')
185+
.then(res => res.json())
186+
.then(env => {
187+
const isEnabled = env.ENABLE_REPORTING_ERRORS === true || env.ENABLE_REPORTING_ERRORS === 'true';
188+
btn.setAttribute('data-enabled', isEnabled);
189+
if (isEnabled) {
190+
btn.addEventListener('click', e => {
191+
e.preventDefault();
192+
showModal();
193+
});
194+
// Set dynamic attributes
195+
btn.setAttribute('data-link', location.href);
196+
btn.setAttribute('data-site', location.hostname);
197+
}
198+
})
199+
.catch(() => {
200+
btn.setAttribute('data-enabled', false);
201+
});
202+
};
203+
204+
document.addEventListener('DOMContentLoaded', wireButton);
190205
</script>
191206
</body>
192207
</html>

worker.js

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -79,41 +79,48 @@ async function injectBanner(response, bannerMessage) {
7979
return new Response(text, response);
8080
}
8181

82+
// New function to handle /report-error POST for Discord webhook
83+
async function handleReportError(request, env) {
84+
try {
85+
const { fullName, errorCode, siteName, redirectUrl } = await request.json();
86+
if (!fullName || !errorCode || !siteName || !redirectUrl) {
87+
return new Response(JSON.stringify({ ok: false, error: 'Missing required fields' }), { status: 400, headers: { 'Content-Type': 'application/json' } });
88+
}
89+
const embed = {
90+
title: '🆘 Erreur Signalée',
91+
color: 0xef4444, // Red color
92+
fields: [
93+
{ name: 'Nom / Prénom', value: fullName, inline: true },
94+
{ name: 'Code d\'Erreur', value: errorCode, inline: true },
95+
{ name: 'Site', value: siteName, inline: true },
96+
{ name: 'URL', value: redirectUrl, inline: false }
97+
],
98+
timestamp: new Date().toISOString()
99+
};
100+
const webhookResponse = await fetch(env.DISCORD_WEBHOOK_URL, {
101+
method: 'POST',
102+
headers: { 'Content-Type': 'application/json' },
103+
body: JSON.stringify({ embeds: [embed] })
104+
});
105+
if (!webhookResponse.ok) {
106+
throw new Error(`Webhook failed: ${webhookResponse.status}`);
107+
}
108+
return new Response(JSON.stringify({ ok: true }), { headers: { 'Content-Type': 'application/json' } });
109+
} catch (error) {
110+
console.error('Report error handling failed:', error);
111+
return new Response(JSON.stringify({ ok: false, error: 'Internal error' }), { status: 500, headers: { 'Content-Type': 'application/json' } });
112+
}
113+
}
114+
82115
export default {
83116
async fetch(request, env, ctx) {
84117
const host = request.headers.get('host');
85118
const url = new URL(request.url);
86119

87-
// New: Handle /report-error POST for Discord webhook
88-
if (url.pathname === '/report-error' && request.method === 'POST') {
89-
try {
90-
const { fullName, errorCode, siteName, redirectUrl } = await request.json();
91-
if (!fullName || !errorCode || !siteName || !redirectUrl) {
92-
return new Response(JSON.stringify({ ok: false, error: 'Missing required fields' }), { status: 400, headers: { 'Content-Type': 'application/json' } });
93-
}
94-
const embed = {
95-
title: '🆘 Erreur Signalée',
96-
color: 0xef4444, // Red color
97-
fields: [
98-
{ name: 'Nom / Prénom', value: fullName, inline: true },
99-
{ name: 'Code d\'Erreur', value: errorCode, inline: true },
100-
{ name: 'Site', value: siteName, inline: true },
101-
{ name: 'URL', value: redirectUrl, inline: false }
102-
],
103-
timestamp: new Date().toISOString()
104-
};
105-
const webhookResponse = await fetch(env.DISCORD_WEBHOOK_URL, {
106-
method: 'POST',
107-
headers: { 'Content-Type': 'application/json' },
108-
body: JSON.stringify({ embeds: [embed] })
109-
});
110-
if (!webhookResponse.ok) {
111-
throw new Error(`Webhook failed: ${webhookResponse.status}`);
112-
}
113-
return new Response(JSON.stringify({ ok: true }), { headers: { 'Content-Type': 'application/json' } });
114-
} catch (error) {
115-
console.error('Report error handling failed:', error);
116-
return new Response(JSON.stringify({ ok: false, error: 'Internal error' }), { status: 500, headers: { 'Content-Type': 'application/json' } });
120+
// Handle /report-error POST for Discord webhook using the new function
121+
if(env.ENABLE_REPORTING_ERRORS === true || env.ENABLE_REPORTING_ERRORS === 'true') {
122+
if (url.pathname === '/report-error' && request.method === 'POST') {
123+
return await handleReportError(request, env);
117124
}
118125
}
119126

wrangler.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ ENABLE_CACHE = true
2424
CACHE_TTL_MS = 60000
2525

2626
# New: Discord webhook URL for error reports
27-
DISCORD_WEBHOOK_URL = "https://discord.com/api/webhooks/1411329377538674870/Y4ikyN_RCGFCu5DfRbGuxhIdA8mqLFckpu_zmHY5t6ewEehd9VRHxuz-VeGTE6t6bssC"
27+
ENABLE_REPORTING_ERRORS = true
28+
REPORTING_ERRORS_DISCORD_WEBHOOK_URL = "https://discord.com/api/webhooks/1411329377538674870/Y4ikyN_RCGFCu5DfRbGuxhIdA8mqLFckpu_zmHY5t6ewEehd9VRHxuz-VeGTE6t6bssC"
2829

2930

3031

0 commit comments

Comments
 (0)