G+
async function saveToCache(url, content) {
const db = await openDB();
return new Promise((resolve, reject) => {
const transaction = db.transaction(STORE_NAME, 'readwrite');
const store = transaction.objectStore(STORE_NAME);
const request = store.put({ url: url, content: content, timestamp: Date.now() });
request.onsuccess = () => resolve();
request.onerror = () => reject(new Error('Cache error'));
});
}
async function getFileFromCache(url) {
const db = await openDB();
return new Promise((resolve, reject) => {
const transaction = db.transaction(STORE_NAME, 'readonly');
const store = transaction.objectStore(STORE_NAME);
const request = store.get(url);
request.onsuccess = async (event) => {
const data = event.target.result;
if (data && (Date.now() - data.timestamp) < CACHE_DURATION) {
resolve(data.content);
} else {
try {
const response = await fetch(url);
const text = await response.text();
await saveToCache(url, text);
resolve(text);
} catch (err) {
reject(err);
}
}
};
request.onerror = () => reject(new Error('DB error'));
});
}
function PlayTo(button) {
const iframe = document.getElementById("fr");
getFileFromCache(FILE_URL).then((text) => {
iframe.contentDocument.open();
iframe.contentDocument.write(text);
iframe.contentDocument.close();
iframe.style.display = "block";
button.style.display = "none";
}).catch((err) => {
console.error('Error:', err);
alert('Oyun yüklenemedi.');
});
}
function openFullscreen() {
getFileFromCache(FILE_URL).then((text) => {
const newWindow = window.open("", "_blank");
if (newWindow) {
newWindow.document.open();
newWindow.document.write(text);
newWindow.document.close();
} else {
alert('Tam ekran açılamadı.');
}
}).catch((err) => {
console.error('Error:', err);
alert('Tam ekran yüklenemedi.');
});
}