Download assets/js/game-save-interceptor.js from EDUrocks/EDUrocks-3.0-frontend-source-code: direct link, hf CLI and curl.
- Browser
- Download file 8.67 kB
-
https://huggingface.co/datasets/EDUrocks/EDUrocks-3.0-frontend-source-code/resolve/main/assets/js/game-save-interceptor.js
- Command line
-
hf download hf://datasets/EDUrocks/EDUrocks-3.0-frontend-source-code/assets/js/game-save-interceptor.js
-
curl -L -o game-save-interceptor.js https://huggingface.co/datasets/EDUrocks/EDUrocks-3.0-frontend-source-code/resolve/main/assets/js/game-save-interceptor.js
8.67 kB
| /** | |
| * EDUrocks 3.0 - Game Save Interceptor (Injected into game iframes) | |
| * This script is injected into game iframes to intercept Unity PlayerPrefs and other save mechanisms | |
| * Lightweight version that syncs with the parent window's SaveSync system | |
| */ | |
| (function() { | |
| 'use strict'; | |
| console.log('[Game Save Interceptor] Loading...'); | |
| // Get game info from parent window | |
| let gameId = null; | |
| let gameName = null; | |
| let authToken = null; | |
| let isLoggedIn = false; | |
| try { | |
| if (window.parent && window.parent.EDUrocks) { | |
| gameId = window.parent.EDUrocks.currentGameId; | |
| gameName = window.parent.EDUrocks.currentGameName; | |
| } | |
| authToken = localStorage.getItem('edurocks_auth_token'); | |
| isLoggedIn = !!authToken; | |
| } catch (e) { | |
| console.warn('[Game Save Interceptor] Cannot access parent window (cross-origin)'); | |
| } | |
| if (!isLoggedIn) { | |
| console.log('[Game Save Interceptor] User not logged in, interceptor disabled'); | |
| return; | |
| } | |
| console.log('[Game Save Interceptor] Active for game:', gameId || 'unknown'); | |
| // ==================== CONFIGURATION ==================== | |
| const BLACKLIST = [ | |
| /^edurocks_/i, | |
| /cache/i, | |
| /^__/, | |
| /analytics/i, | |
| /telemetry/i | |
| ]; | |
| const IDB_WHITELIST = [ | |
| '/idbfs', // Unity PlayerPrefs and save files | |
| 'UnityPlayerPrefs' // Alternative Unity storage | |
| ]; | |
| // ==================== UTILITY FUNCTIONS ==================== | |
| function shouldSyncKey(key) { | |
| for (const pattern of BLACKLIST) { | |
| if (pattern.test(key)) return false; | |
| } | |
| return true; | |
| } | |
| function notifyParent(action, data) { | |
| try { | |
| if (window.parent && window.parent.postMessage) { | |
| window.parent.postMessage({ | |
| type: 'edurocks-save-event', | |
| action: action, | |
| data: data, | |
| gameId: gameId | |
| }, '*'); | |
| } | |
| } catch (e) { | |
| console.warn('[Game Save Interceptor] Failed to notify parent:', e); | |
| } | |
| } | |
| // ==================== LOCALSTORAGE INTERCEPTION ==================== | |
| const originalSetItem = Storage.prototype.setItem; | |
| const originalRemoveItem = Storage.prototype.removeItem; | |
| const originalClear = Storage.prototype.clear; | |
| Storage.prototype.setItem = function(key, value) { | |
| originalSetItem.call(this, key, value); | |
| if (this === window.localStorage && shouldSyncKey(key)) { | |
| console.log('[Game Save Interceptor] localStorage.setItem:', key); | |
| notifyParent('localStorage-set', { key, value }); | |
| } | |
| }; | |
| Storage.prototype.removeItem = function(key) { | |
| originalRemoveItem.call(this, key); | |
| if (this === window.localStorage && shouldSyncKey(key)) { | |
| console.log('[Game Save Interceptor] localStorage.removeItem:', key); | |
| notifyParent('localStorage-remove', { key }); | |
| } | |
| }; | |
| Storage.prototype.clear = function() { | |
| if (this === window.localStorage) { | |
| const keys = []; | |
| for (let i = 0; i < this.length; i++) { | |
| const key = this.key(i); | |
| if (shouldSyncKey(key)) { | |
| keys.push(key); | |
| } | |
| } | |
| originalClear.call(this); | |
| console.log('[Game Save Interceptor] localStorage.clear()'); | |
| notifyParent('localStorage-clear', { keys }); | |
| } else { | |
| originalClear.call(this); | |
| } | |
| }; | |
| // ==================== INDEXEDDB INTERCEPTION ==================== | |
| if (window.indexedDB) { | |
| const originalOpen = window.indexedDB.open.bind(window.indexedDB); | |
| const trackedDatabases = new Map(); | |
| window.indexedDB.open = function(name, version) { | |
| console.log('[Game Save Interceptor] IndexedDB.open:', name, 'version:', version); | |
| const request = originalOpen.call(window.indexedDB, name, version); | |
| request.addEventListener('success', function(event) { | |
| const db = event.target.result; | |
| // Only track whitelisted databases (Unity save data) | |
| if (IDB_WHITELIST.includes(name)) { | |
| trackedDatabases.set(name, db); | |
| console.log('[Game Save Interceptor] Tracking Unity database:', name); | |
| notifyParent('indexeddb-open', { dbName: name }); | |
| // Set up periodic snapshots | |
| setInterval(() => snapshotDatabase(db, name), 5000); | |
| } else { | |
| console.log('[Game Save Interceptor] Ignoring non-whitelisted database:', name); | |
| } | |
| }); | |
| return request; | |
| }; | |
| // Snapshot IndexedDB data | |
| async function snapshotDatabase(db, dbName) { | |
| if (!db || db.objectStoreNames.length === 0) return; | |
| try { | |
| const objectStoreNames = Array.from(db.objectStoreNames); | |
| for (const storeName of objectStoreNames) { | |
| // Unity stores PlayerPrefs and save files in FILE_DATA | |
| if (storeName !== 'FILE_DATA') { | |
| continue; | |
| } | |
| const transaction = db.transaction(storeName, 'readonly'); | |
| const objectStore = transaction.objectStore(storeName); | |
| const getAllRequest = objectStore.getAll(); | |
| const getAllKeysRequest = objectStore.getAllKeys(); | |
| Promise.all([ | |
| new Promise(resolve => { | |
| getAllRequest.onsuccess = () => resolve(getAllRequest.result); | |
| getAllRequest.onerror = () => resolve([]); | |
| }), | |
| new Promise(resolve => { | |
| getAllKeysRequest.onsuccess = () => resolve(getAllKeysRequest.result); | |
| getAllKeysRequest.onerror = () => resolve([]); | |
| }) | |
| ]).then(([values, keys]) => { | |
| if (values && values.length > 0) { | |
| const records = values.map((value, index) => ({ | |
| key: keys[index], | |
| value: value | |
| })); | |
| console.log('[Game Save Interceptor] Snapshotted', records.length, 'Unity save records'); | |
| notifyParent('indexeddb-snapshot', { | |
| dbName, | |
| storeName, | |
| records: records | |
| }); | |
| } | |
| }).catch(err => { | |
| console.warn('[Game Save Interceptor] Snapshot error:', err); | |
| }); | |
| } | |
| } catch (error) { | |
| console.error('[Game Save Interceptor] Error snapshotting database:', error); | |
| } | |
| } | |
| console.log('[Game Save Interceptor] IndexedDB intercepted'); | |
| } | |
| // ==================== UNITY SPECIFIC HOOKS ==================== | |
| // Detect Unity WebGL and hook into its PlayerPrefs system if possible | |
| function hookUnityPlayerPrefs() { | |
| // Unity's PlayerPrefs are stored in IndexedDB under /idbfs/PlayerPrefs | |
| // We're already intercepting IndexedDB operations, so this is just for logging | |
| // Try to detect Unity | |
| if (window.unityInstance || window.gameInstance || document.querySelector('canvas[data-engine="unity"]')) { | |
| console.log('[Game Save Interceptor] Unity WebGL detected'); | |
| notifyParent('unity-detected', { gameName }); | |
| } | |
| // Check for Unity in window | |
| const checkUnity = setInterval(() => { | |
| if (window.unityInstance || window.gameInstance) { | |
| console.log('[Game Save Interceptor] Unity instance found'); | |
| notifyParent('unity-ready', {}); | |
| clearInterval(checkUnity); | |
| } | |
| }, 1000); | |
| // Stop checking after 30 seconds | |
| setTimeout(() => clearInterval(checkUnity), 30000); | |
| } | |
| // Wait for page to load before hooking Unity | |
| if (document.readyState === 'loading') { | |
| document.addEventListener('DOMContentLoaded', hookUnityPlayerPrefs); | |
| } else { | |
| hookUnityPlayerPrefs(); | |
| } | |
| console.log('[Game Save Interceptor] Ready - localStorage and IndexedDB intercepted'); | |
| })(); | |