Platform Adapter
One set of calls (gameplay start and stop, ads, sharing, the player’s name, saving) that works on Bash.tv, itch.io, CrazyGames, Poki or a plain website. Game code never touches a portal’s SDK directly, and a missing or failing SDK never breaks the game.
Files: platform-adapter.js (the component, no dependencies) and platform-adapter.test.mjs (7 tests).
Copy platform-adapter.js into your game, for example to src/components/, and import it as an ES module.
Usage
import { createPlatform } from "./platform-adapter.js";
const platform = await createPlatform({ hooks: { mute: () => audio.mute("master", true), unmute: () => audio.mute("master", false), pause, resume } });
platform.loadingDone();
platform.gameplayStart(); // level start, unpause, revive
platform.gameplayStop(); // menus, level end, pause
await platform.midgameAd(); // between levels; resolves straight away where there are no ads
if (await platform.rewardedAd()) player.extraLife(); // true only if the player watched it
await platform.share({ text: `I scored ${score}!` });
console.log(platform.name, platform.supports("ads"));
API
-
createPlatform({ name, hooks, storage, global }) → Promise of platformname forces one: “bash”“crazygames” “poki” “itch” “web”; default detects platform.name / platform.supports(feature) / platform.featuresfeatures: ads, rewarded, share, user, cloudSave, gameplayEventsplatform.loadingStart() / loadingDone() / gameplayStart() / gameplayStop() / happyTime()each safe to call anywhere; repeats are droppedplatform.midgameAd() → Promise<void> / platform.rewardedAd() → Promise<boolean>mute and pause hooks run around the adplatform.share({ text, url, files }) → Promise<{ shared }>Bash.tv share sheet, the browser share sheet, or copying textplatform.getUser() → Promise<{ id, name } | null>platform.save(key, value) / platform.load(key)portal storage where there is one, else localStorageplatform.errorsSDK calls that failed (they are caught and logged here, never thrown)
Detection: Bash.tv if window.bash exists; CrazyGames if window.CrazyGames.SDK exists; Poki if window.PokiSDK exists; itch.io if the page is on itch.zone or itch.io; else web.
Each portal’s SDK script still has to be added to the page as that portal’s docs say. Check the portal’s current docs before release; their rules change.
Engines
| Engine | Notes |
|---|---|
| Vanilla, Phaser, PixiJS, Three.js | yes. Call the hooks from your engine’s pause and audio code. |
Tests
Node tests with fake SDKs: detection order, gameplay events without repeats, ads with mute and pause hooks, rewarded results, ad errors and missing SDKs, failing SDK calls caught, share fallbacks, users, storage.
Run them with node --test toolkit/components/platform-adapter/ (Node 20 or newer, nothing to install).