Show visitor count on my cloudflare page

Hello, I have a cloudflare worker https://live.lofi.radio/ which shows: 20 I want with HTML/Javascript show this number on https:lofi.radio But I get this error: Access to fetch at 'https://live.lofi.radio/' from origin 'https://lofi.radio' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. Is there a way you can show output from a cloudflare worker on a cloudflare page?
I do not want to use iframe, also javascript fetch seems not to work.
9 Replies
Geert Jan Sloos
Geert Jan SloosOP•2y ago
Thanks for your reply I added this to my cloudflare worker: // Reference: https://developers.cloudflare.com/workers/examples/cors-header-proxy const corsHeaders = { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET,HEAD,POST,OPTIONS", "Access-Control-Max-Age": "86400", }
Geert Jan Sloos
Geert Jan SloosOP•2y ago
Return JSON · Cloudflare Workers docs
Documentation for Cloudflare Workers, a serverless execution environment that allows you to create entirely new applications or augment existing ones …
Chaika
Chaika•2y ago
Your worker needs to return those headers just like in that return json example, but just with the other headers you need as well, not sure what you mean about with "workers.js" path, path isn't involved What is your current return response code in your worker?
Geert Jan Sloos
Geert Jan SloosOP•2y ago
Something like this?
Chaika
Chaika•2y ago
you're talking about how you added them to the request in the quick edit dev panel? nah that won't work. Can you share the code you have currently? If not the full code, at least the part where you return the response
Geert Jan Sloos
Geert Jan SloosOP•2y ago
It shows the 57, i want to show this 57 on my cloudflare page (index.html)
Geert Jan Sloos
Geert Jan SloosOP•2y ago
My code in worker.js: addEventListener("fetch", (event) => { event.respondWith( handleRequest(event.request).catch( (err) => new Response(err.stack, { status: 500 }) ) ); }); addEventListener('fetch', event => { event.respondWith(handleRequest(event.request)); }); async function handleRequest(request) { const today = new Date().toISOString().split('T')[0]; const query = query { viewer { zones(filter: { zoneTag: "XXX" }) { httpRequests1dGroups(filter: { date: "${today}" }, limit: 1) { uniq { uniques } } } } } ; const data = JSON.stringify({ query }); const response = await fetch('https://api.cloudflare.com/client/v4/graphql', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer XXXXXXX' }, body: data }); if (!response.ok) { throw new Error('Error occurred while fetching data'); } const result = await response.json(); const uniques = result.data.viewer.zones[0].httpRequests1dGroups[0].uniq.uniques || 0; return new Response(${uniques}, { headers: { 'Content-Type': 'text/plain' }, }); }
Chaika
Chaika•2y ago
change the last part
....
headers: { 'Content-Type': 'text/plain', 'Access-Control-Allow-Origin': 'https://lofi.radio' },
....
}
....
headers: { 'Content-Type': 'text/plain', 'Access-Control-Allow-Origin': 'https://lofi.radio' },
....
}
Geert Jan Sloos
Geert Jan SloosOP•2y ago
Thanks it works!!!! you have a donation page somewhere 🙂 ?
Want results from more Discord servers?
Add your server