ruffle/web/js-src/ruffle-player.js

84 lines
2.6 KiB
JavaScript
Raw Normal View History

import load_ruffle from "./load-ruffle";
2019-08-22 01:02:43 +00:00
import ruffle_shadow_template from "./shadow-template";
2019-08-26 00:11:10 +00:00
export let FLASH_MIMETYPE = "application/x-shockwave-flash";
export let FUTURESPLASH_MIMETYPE = "application/futuresplash";
export let FLASH_ACTIVEX_CLASSID = "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000";
export class RufflePlayer extends HTMLElement {
2019-08-22 01:02:43 +00:00
constructor(...args) {
let self = super(...args);
self.shadow = self.attachShadow({mode: 'closed'});
self.shadow.appendChild(ruffle_shadow_template.content.cloneNode(true));
self.dynamic_styles = self.shadow.getElementById("dynamic_styles");
2019-08-22 01:02:43 +00:00
self.canvas = self.shadow.getElementById("player");
self.instance = null;
self.Ruffle = load_ruffle();
2019-08-22 01:02:43 +00:00
return self;
}
connectedCallback() {
this.update_styles();
}
attributeChangedCallback(name, oldValue, newValue) {
if (name === "width" || name === "height") {
this.update_styles();
}
}
update_styles() {
for (var i = 0; i < this.dynamic_styles.sheet.rules.length; i += 1) {
this.dynamic_styles.sheet.deleteRule(i);
}
if (!isNaN(parseInt(this.attributes.width.value))) {
this.dynamic_styles.sheet.insertRule(":host { width: " + this.attributes.width.value + "px; }");
}
if (!isNaN(parseInt(this.attributes.height.value))) {
this.dynamic_styles.sheet.insertRule(":host { height: " + this.attributes.height.value + "px; }");
}
}
async stream_swf_url(url) {
2019-08-22 01:02:43 +00:00
//TODO: Actually stream files...
try {
let abs_url = new URL(url, window.location.href).toString();
console.log("Loading SWF file " + url);
let response = await fetch(abs_url);
2019-08-22 01:02:43 +00:00
if (response.ok) {
let data = await response.arrayBuffer();
await this.play_swf_data(data);
console.log("Playing " + url);
2019-08-22 01:02:43 +00:00
} else {
console.error("SWF load failed: " + response.status + " " + response.statusText + " for " + url);
}
} catch (err) {
console.error("Serious error occured loading SWF file: " + err);
throw err;
}
2019-08-22 01:02:43 +00:00
}
async play_swf_data(data) {
console.log("Got SWF data");
if (this.instance) {
this.instance.destroy();
this.instance = null;
2019-08-22 01:02:43 +00:00
}
let Ruffle = await this.Ruffle.catch(function (e) {
console.error("Serious error loading Ruffle: " + e);
throw e;
});
this.instance = Ruffle.new(this.canvas, new Uint8Array(data));
2019-08-22 01:02:43 +00:00
}
}