diff --git a/CHANGELOG.md b/CHANGELOG.md index 021dad0..8381994 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,9 @@ ## [1.0.1](https://github.com/brckd/fancade-plus/compare/v1.0.0...v1.0.1) (2024-04-13) - ### Bug Fixes -* update dependenices ([fa0ee49](https://github.com/brckd/fancade-plus/commit/fa0ee491910c0705e032a168b528d23d3cb84dfa)) +- update dependenices + ([fa0ee49](https://github.com/brckd/fancade-plus/commit/fa0ee491910c0705e032a168b528d23d3cb84dfa)) # 1.0.0 (2024-04-13) diff --git a/README.md b/README.md index e20c1d6..ceb6726 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,17 @@ A userscript to make [Fancade Web](https://play.fancade.com) more accessible on mobile. ## Features + - Install Fancade Web like a native app -- Always go full screen and play instantly on the installed app +- Instantly immerse into full-screen game play +- Physical keyboard support +- Increased feature parity ## Using -Install the [latest userscript](https://github.com/brckd/fancade-plus/releases/latest/download/fancade-plus.user.js) in a browser that supports doing so. +Install the +[latest userscript](https://github.com/brckd/fancade-plus/releases/latest/download/fancade-plus.user.js) +in a browser that supports doing so. - [Cromite](https://github.com/uazo/cromite) and Bromite [support userscripts natively](https://github.com/bromite/bromite/wiki/UserScripts). diff --git a/src/fancade-plus.user.ts b/src/fancade-plus.user.ts index b51140b..4095847 100644 --- a/src/fancade-plus.user.ts +++ b/src/fancade-plus.user.ts @@ -1,13 +1,10 @@ -import { getManifestData, createDataURI } from "./manifest"; +import { createManifestLink } from "./manifest"; +import { updateLocation, sanitizePath, setFullscreen, setInstantStart } from "./location"; -try { - const url = new URL(document.location.href); - const manifestData = getManifestData(document.head, url); +document.head.append(createManifestLink(document)); - const manifestLink = document.createElement("link"); - manifestLink.rel = "manifest"; - manifestLink.href = createDataURI(JSON.stringify(manifestData), "application/json"); - document.head.append(manifestLink); -} catch (err) { - alert(err); -} +const url = new URL(document.location.href); +sanitizePath(url); +setInstantStart(url); +setFullscreen(url); +updateLocation(document.location, url); diff --git a/src/global.d.ts b/src/global.d.ts new file mode 100644 index 0000000..0d72983 --- /dev/null +++ b/src/global.d.ts @@ -0,0 +1,3 @@ +declare let stretchMode: boolean; +declare let resizeCanvas: (informC?: boolean) => void; +declare let getStartInstantly: () => boolean; diff --git a/src/location.ts b/src/location.ts new file mode 100644 index 0000000..e4ce1ee --- /dev/null +++ b/src/location.ts @@ -0,0 +1,21 @@ +export function updateLocation(location: Location, url: URL) { + if (url.href != location.href) location.replace(url); +} + +export function sanitizePath(url: URL) { + if (!url.pathname.endsWith("/")) url.pathname += "/"; +} + +export function setSearchParam(url: URL, param: string, value: string | number, enable: boolean) { + if (enable) url.searchParams.set(param, value.toString()); + else url.searchParams.delete(param); +} + +export function setFullscreen(url: URL, enable = true) { + setSearchParam(url, "max_w", 9999, enable); + setSearchParam(url, "max_h", 9999, enable); +} + +export function setInstantStart(url: URL, enable = true) { + setSearchParam(url, "istart", 1, enable); +} diff --git a/src/manifest.ts b/src/manifest.ts index 77dc70d..864e12e 100644 --- a/src/manifest.ts +++ b/src/manifest.ts @@ -1,23 +1,24 @@ import { JSONSchemaForWebApplicationManifestFiles as Manifest } from "@schemastore/web-manifest"; -function getMetaContent(element: HTMLElement, name: string) { - return element.querySelector(`meta[name~="${name}"], meta[property~="${name}"]`) - ?.content; +export function createManifestLink(document: Document, data?: Manifest) { + if (!data) { + const url = new URL(document.location.href); + data = getManifestData(document.head, url); + } + const link = document.createElement("link"); + link.rel = "manifest"; + link.href = createDataURI(JSON.stringify(data), "application/json"); + return link; } export function getManifestData(head: HTMLHeadElement, url: URL) { const root = new URL("/", url); - const start = new URL(url); - if (!start.pathname.endsWith("/")) start.pathname += "/"; - start.searchParams.set("istart", "1"); - start.searchParams.set("max_w", "9999"); - start.searchParams.set("max_h", "9999"); const color = "#00c9ff"; const manifest: Manifest = { name: head.querySelector("title")?.text, description: getMetaContent(head, "description"), - start_url: start.href, + start_url: url.href, scope: root.href, display: "fullscreen", theme_color: color, @@ -33,6 +34,11 @@ export function getManifestData(head: HTMLHeadElement, url: URL) { return manifest; } +export function getMetaContent(element: HTMLElement, name: string) { + return element.querySelector(`meta[name~="${name}"], meta[property~="${name}"]`) + ?.content; +} + export function createDataURI(data: string, mediatype?: string, base64?: false): string; export function createDataURI(data: Buffer, mediatype: string, base64: true): string; export function createDataURI(data: string | Buffer, mediatype = "", base64 = false) {