53 lines
2.1 KiB
JavaScript
53 lines
2.1 KiB
JavaScript
/*
|
|
urls.js — where things live, inside Discord vs local dev.
|
|
|
|
Inside Discord the Activity runs on https://<appId>.discordsays.com and may only talk to that
|
|
origin; Discord's URL mappings forward path prefixes (most specific wins):
|
|
/ → our server (tv-activity.bug.tools): /api/config, /api/token, /ws, static files
|
|
/tv → api-tv.bug.tools (bug-tv API)
|
|
/img → image.tmdb.org
|
|
So inside Discord bug-tv URLs become same-origin "/tv/v1/…". The bug-tv prefix cannot be "/api"
|
|
because "/api/token" must reach our server (see docs/protocol.md "Deviations").
|
|
patchUrlMappings() (called in main.js) only rewrites *absolute* URLs of the mapped hosts; the
|
|
relative ones built here already hit the right prefix. The legacy "/.proxy/" prefix is not needed.
|
|
|
|
Dev (no frame_id): bug-tv base = ?api=<url> (default http://127.0.0.1:8789), ws on this origin.
|
|
*/
|
|
|
|
const params = new URLSearchParams(typeof location !== 'undefined' ? location.search : '');
|
|
|
|
export const inDiscord =
|
|
typeof location !== 'undefined' && (params.has('frame_id') || /\.discordsays\.com$/.test(location.hostname));
|
|
|
|
export const TV_PREFIX = '/tv';
|
|
export const IMG_PREFIX = '/img';
|
|
|
|
export function param(name) {
|
|
return params.get(name);
|
|
}
|
|
|
|
export function apiBase() {
|
|
if (inDiscord) return TV_PREFIX;
|
|
return (params.get('api') || 'http://127.0.0.1:8789').replace(/\/+$/, '');
|
|
}
|
|
|
|
/** bug-tv API-relative URL ("/v1/p/…") → fetchable URL. Absolute URLs pass through. */
|
|
export function resolveUrl(u) {
|
|
const s = String(u || '');
|
|
if (/^[a-z][a-z0-9+.-]*:/i.test(s)) return s;
|
|
return apiBase() + (s.startsWith('/') ? s : `/${s}`);
|
|
}
|
|
|
|
export function wsUrl() {
|
|
return `${location.protocol === 'https:' ? 'wss:' : 'ws:'}//${location.host}/ws`;
|
|
}
|
|
|
|
/** TMDB image: a path ("/abc.jpg") or a full image.tmdb.org URL → loadable URL. */
|
|
export function imgUrl(p, size = 'w780') {
|
|
if (!p) return '';
|
|
let u = String(p);
|
|
if (u.startsWith('/')) u = `https://image.tmdb.org/t/p/${size}${u}`;
|
|
if (inDiscord) u = u.replace(/^https?:\/\/image\.tmdb\.org/, IMG_PREFIX);
|
|
return u;
|
|
}
|