Skip to content

Commit 1621c78

Browse files
committed
web: Display loading screen while resolving the SWF URL
Previously, when entering a URL in the extension player, it had been resolved before creating a RufflePlayer instance and displaying a loading screen. As resolving can include testing for server availability (to decide whether HTTPS or HTTP is used), this could create a wait time without the loading screen. This has been fixed. The URL is now resolved while the loading screen is displayed. Additionally, the timeouts have been increased (they were low because of the lack of a loading animation). A bug that caused the document title to not be set correctly if the SWF URL was the main page of a domain has also been fixed.
1 parent 3adab91 commit 1621c78

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

web/packages/core/src/ruffle-player.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,10 +949,13 @@ export class RufflePlayer extends HTMLElement {
949949
*
950950
* The options will be defaulted by the [[config]] field, which itself
951951
* is defaulted by a global `window.RufflePlayer.config`.
952+
* @param urlResolveHook An optional function resolving the URL. This is used by the
953+
* extension player to support URLs without an explicit protocol.
952954
*/
953955
async load(
954956
options: string | URLLoadOptions | DataLoadOptions,
955957
isPolyfillElement: boolean = false,
958+
urlResolveHook: ((url: string) => Promise<string>) | null = null,
956959
): Promise<void> {
957960
options = this.checkOptions(options);
958961

@@ -997,6 +1000,9 @@ export class RufflePlayer extends HTMLElement {
9971000
await this.ensureFreshInstance();
9981001

9991002
if ("url" in options) {
1003+
if (urlResolveHook !== null) {
1004+
options.url = await urlResolveHook(options.url);
1005+
}
10001006
console.log(`Loading SWF file ${options.url}`);
10011007
this.swfUrl = new URL(options.url, document.baseURI);
10021008

web/packages/extension/src/player.ts

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,15 @@ function unload() {
9999
}
100100
}
101101

102-
function load(options: string | DataLoadOptions | URLLoadOptions) {
102+
function load(
103+
options: string | DataLoadOptions | URLLoadOptions,
104+
urlResolveHook: ((url: string) => Promise<string>) | null = null,
105+
) {
103106
unload();
104107
player = ruffle.createPlayer();
105108
player.id = "player";
106109
playerContainer.append(player);
107-
player.load(options, false);
110+
player.load(options, false, urlResolveHook);
108111
player.addEventListener("loadedmetadata", () => {
109112
if (player.metadata) {
110113
for (const [key, value] of Object.entries(player.metadata)) {
@@ -236,22 +239,32 @@ window.addEventListener("load", () => {
236239
});
237240

238241
async function loadSwf(swfUrl: string) {
239-
const url = await resolveSwfUrl(swfUrl);
240-
if (url !== null) {
241-
swfUrl = url.toString();
242-
const pathname = url.pathname;
243-
document.title = pathname.substring(pathname.lastIndexOf("/") + 1);
244-
}
242+
document.title = swfUrl
243+
.split("/")
244+
.filter((item) => item !== "")
245+
.slice(-1)[0]!;
246+
247+
const resolveUrlHook = async (swfUrl: string) => {
248+
const url = await resolveSwfUrl(swfUrl);
249+
if (url !== null) {
250+
return url.toString();
251+
} else {
252+
return swfUrl;
253+
}
254+
};
245255

246256
const options = await utils.getExplicitOptions();
247257
localFileName.textContent = document.title;
248258
localFileInput.value = "";
249-
load({
250-
...options,
251-
url: swfUrl,
252-
base: swfUrl.substring(0, swfUrl.lastIndexOf("/") + 1),
253-
...baseExtensionConfig,
254-
});
259+
load(
260+
{
261+
...options,
262+
url: swfUrl,
263+
base: swfUrl.substring(0, swfUrl.lastIndexOf("/") + 1),
264+
...baseExtensionConfig,
265+
},
266+
resolveUrlHook,
267+
);
255268
}
256269

257270
/**
@@ -287,11 +300,10 @@ async function resolveSwfUrl(enteredUrl: string): Promise<URL | null> {
287300
}
288301

289302
try {
290-
// TODO: Make the loading animation appear before waiting for the server response
291303
// Only use http if it works but https doesn't
292304
if (
293-
(await serverAvailable("https://" + enteredUrl, 200)) ||
294-
!(await serverAvailable("http://" + enteredUrl, 100))
305+
(await serverAvailable("https://" + enteredUrl, 600)) ||
306+
!(await serverAvailable("http://" + enteredUrl, 300))
295307
) {
296308
return new URL("https://" + enteredUrl);
297309
} else {

0 commit comments

Comments
 (0)