Skip to content

Commit a4134b8

Browse files
committed
service worker caching (thanks claude)
1 parent 6d11b98 commit a4134b8

9 files changed

Lines changed: 415 additions & 82 deletions

File tree

static/assets/scripts/helpers.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,16 +700,18 @@ async function enforceDonationLockout(goalAmount = 500) {
700700
border-radius: 16px;
701701
box-shadow: 0 4px 24px rgba(0, 0, 0, 0.15);
702702
text-align: center;
703+
color: #333;
703704
`;
704705

705706
container.innerHTML = `
706707
<h2 style="margin-top: 0; color: #333;">Support CCPorted</h2>
707708
<p style="color: #555; font-size: 16px; margin-bottom: 16px;">
708709
We did not meet our donation goal of $${goalAmount}. CCPorted may shut down unless we can raise more support. If this site has helped you access or enjoy games, please consider donating.
709710
</p>
710-
<p style="font-weight: bold; margin-bottom: 16px;">
711+
<p style="font-weight: bold; margin-bottom: 16px; color: #333;">
711712
Current Raised: $${currentAmount.toFixed(2)} / $${goalAmount}
712713
</p>
714+
<p>PS: <b><a href = "https://discord.gg/GDEFRBTT3Z">HAVE YOU JOINED THE DISCORD?</a></b></p>
713715
<a href="https://ko-fi.com/ccported" target="_blank" style="
714716
display: block;
715717
background-color: #29abe0;

static/assets/scripts/navigation.js

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ function onload() {
2424
}
2525
}
2626

27-
28-
const header = document.querySelector('header');
29-
if (header) {
30-
// Header animation
31-
document.addEventListener('DOMContentLoaded', () => {
27+
window.addEventListener('DOMContentLoaded', () => {
28+
const header = document.querySelector('header');
29+
if (header) {
30+
// Header animation
3231
onload();
3332
// Hamburger menu functionality
3433
const hamburger = document.createElement('div');
@@ -129,30 +128,30 @@ if (header) {
129128
updateHeader();
130129
}, 250);
131130
});
132-
});
133-
window.addEventListener('scroll', () => {
134-
const header = document.querySelector('header');
135-
if (window.scrollY > scrollThreshold) {
136-
header.classList.add('shadow');
137-
} else {
138-
header.classList.remove('shadow');
139-
}
140-
// check if ad is visible
141-
const ads = document.querySelectorAll(".tad");
142-
ads.forEach(ad => {
143-
if (ad.getAttribute("data-loaded-check") == "true") return;
144-
if (ad.getBoundingClientRect().top < window.innerHeight) {
145-
// allow a 5s loading grace period
146-
setTimeout(() => {
147-
// check if ad loaded (will have Iframe)
148-
if (ad.querySelector("iframe")) {
149-
ad.setAttribute("data-loaded-check", "true");
150-
ad.style.backgroundImage = "none";
151-
} else {
152-
ad.remove();
153-
}
154-
}, 5000)
131+
window.addEventListener('scroll', () => {
132+
const header = document.querySelector('header');
133+
if (window.scrollY > scrollThreshold) {
134+
header.classList.add('shadow');
135+
} else {
136+
header.classList.remove('shadow');
155137
}
138+
// check if ad is visible
139+
const ads = document.querySelectorAll(".tad");
140+
ads.forEach(ad => {
141+
if (ad.getAttribute("data-loaded-check") == "true") return;
142+
if (ad.getBoundingClientRect().top < window.innerHeight) {
143+
// allow a 5s loading grace period
144+
setTimeout(() => {
145+
// check if ad loaded (will have Iframe)
146+
if (ad.querySelector("iframe")) {
147+
ad.setAttribute("data-loaded-check", "true");
148+
ad.style.backgroundImage = "none";
149+
} else {
150+
ad.remove();
151+
}
152+
}, 5000)
153+
}
154+
});
156155
});
157-
});
158-
}
156+
}
157+
});

static/assets/styles/home.css

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ header.shadow {
5656
}
5757

5858
.container {
59-
margin-top: 100px;
60-
padding: 0px;
61-
display: flex;
62-
flex-direction: column;
63-
justify-content: center;
59+
margin-top: 100px;
60+
padding: 0px;
61+
display: flex;
62+
flex-direction: column;
63+
justify-content: center;
64+
width: 100%;
65+
align-items: center;
6466
}
6567

6668
.search {

static/big_game_script.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
window.ccPorted = window.ccPorted || {};
2+
23
(() => {
34
const COGNITO_DOMAIN = "https://us-west-2lg1qptg2n.auth.us-west-2.amazoncognito.com";
45
const CLIENT_ID = "4d6esoka62s46lo4d398o3sqpi";
@@ -10,7 +11,68 @@ window.ccPorted = window.ccPorted || {};
1011
const gameIDExtract = gameIDExtractRG.exec(window.location.pathname);
1112
const parentOrigin = (framed && document.location.ancestorOrigins.length > 0) ? new URL(document.location.ancestorOrigins[0]).origin : null;
1213
const gameID = (typeof window.ccPorted.gameID != "undefined" && window.ccPorted.gameID != "undefined") ? window.ccPorted.gameID : false || window.gameID || ((gameIDExtract) ? gameIDExtract[1] : "Unknown Game");
14+
// Register service worker immediately
15+
if ('serviceWorker' in navigator) {
16+
window.addEventListener('load', () => {
17+
// Register the service worker from the root path
18+
navigator.serviceWorker.register('/game_worker.js', { scope: '/' })
19+
.then(registration => {
20+
console.log('Game service worker registered:', registration);
21+
22+
// Detect if this game is running in an iframe
23+
const isInIframe = window !== window.top;
24+
if (isInIframe) {
25+
// Notify parent frame that we support caching
26+
window.parent.postMessage({
27+
action: 'CACHE_ENABLED',
28+
gameId: gameID
29+
}, '*');
30+
31+
// Setup communication channel with parent frame
32+
setupCacheControl();
33+
}
34+
})
35+
.catch(error => {
36+
console.error('Service worker registration failed:', error);
37+
});
38+
});
39+
}
40+
// Setup communication with the parent frame for cache control
41+
function setupCacheControl() {
42+
window.addEventListener('message', event => {
43+
// Verify the origin
44+
if (event.origin !== parentOrigin) return;
45+
if (!event.data || !event.data.action) return;
46+
// Only handle cache-control requests
47+
if (event.data.type !== 'CACHE_CONTROL') return;
48+
switch (event.data.action) {
49+
case 'CLEAR_CACHE':
50+
// Clear the cache through the service worker
51+
if (navigator.serviceWorker.controller) {
52+
const messageChannel = new MessageChannel();
53+
messageChannel.port1.onmessage = event => {
54+
console.log('Cache cleared:', event.data);
55+
};
1356

57+
navigator.serviceWorker.controller.postMessage({
58+
action: 'CLEAR_CACHE'
59+
}, [messageChannel.port2]);
60+
}
61+
break;
62+
63+
case 'CACHE_STATUS':
64+
// Report cache status back to parent
65+
navigator.serviceWorker.getRegistration().then(registration => {
66+
event.source.postMessage({
67+
action: 'CACHE_STATUS_RESPONSE',
68+
active: !!registration?.active,
69+
size: null // We don't have an easy way to measure cache size
70+
}, event.origin);
71+
});
72+
break;
73+
}
74+
});
75+
}
1476
class Leaderboard {
1577
constructor(gameID) {
1678
if (!gameID) {

0 commit comments

Comments
 (0)