extension: Rename message properties

Make them short and clear:

* `uniqueMessageSuffix` -> `ID`
* `type` -> `to`
This commit is contained in:
relrelb 2021-09-24 09:06:31 +03:00 committed by relrelb
parent 28b34cbe25
commit 340837649e
2 changed files with 14 additions and 19 deletions

View File

@ -24,7 +24,8 @@ const pendingMessages: ({
resolve(value: unknown): void;
reject(reason?: unknown): void;
} | null)[] = [];
const uniqueMessageSuffix = Math.floor(Math.random() * 100000000000);
const ID = Math.floor(Math.random() * 100000000000);
/**
* Send a message to the main world, where Ruffle runs.
@ -33,7 +34,7 @@ const uniqueMessageSuffix = Math.floor(Math.random() * 100000000000);
*/
function sendMessageToPage(data: unknown): Promise<unknown> {
const message = {
type: `FROM_RUFFLE${uniqueMessageSuffix}`,
to: `ruffle_page${ID}`,
index: pendingMessages.length,
data,
};
@ -136,11 +137,7 @@ function isXMLDocument(): boolean {
injectScriptRaw(
'(function(){class RuffleMimeType{constructor(a,b,c){this.type=a,this.description=b,this.suffixes=c}}class RuffleMimeTypeArray{constructor(a){this.__mimetypes=[],this.__named_mimetypes={};for(let b of a)this.install(b)}install(a){let b=this.__mimetypes.length;this.__mimetypes.push(a),this.__named_mimetypes[a.type]=a,this[a.type]=a,this[b]=a}item(a){return this.__mimetypes[a]}namedItem(a){return this.__named_mimetypes[a]}get length(){return this.__mimetypes.length}}class RufflePlugin extends RuffleMimeTypeArray{constructor(a,b,c,d){super(d),this.name=a,this.description=b,this.filename=c}install(a){a.enabledPlugin||(a.enabledPlugin=this),super.install(a)}}class RufflePluginArray{constructor(a){this.__plugins=[],this.__named_plugins={};for(let b of a)this.install(b)}install(a){let b=this.__plugins.length;this.__plugins.push(a),this.__named_plugins[a.name]=a,this[a.name]=a,this[b]=a}item(a){return this.__plugins[a]}namedItem(a){return this.__named_plugins[a]}get length(){return this.__plugins.length}}const FLASH_PLUGIN=new RufflePlugin("Shockwave Flash","Shockwave Flash 32.0 r0","ruffle.js",[new RuffleMimeType("application/futuresplash","Shockwave Flash","spl"),new RuffleMimeType("application/x-shockwave-flash","Shockwave Flash","swf"),new RuffleMimeType("application/x-shockwave-flash2-preview","Shockwave Flash","swf"),new RuffleMimeType("application/vnd.adobe.flash-movie","Shockwave Flash","swf")]);function install_plugin(a){navigator.plugins.install||Object.defineProperty(navigator,"plugins",{value:new RufflePluginArray(navigator.plugins),writable:!1}),navigator.plugins.install(a),0<a.length&&!navigator.mimeTypes.install&&Object.defineProperty(navigator,"mimeTypes",{value:new RuffleMimeTypeArray(navigator.mimeTypes),writable:!1});for(var b=0;b<a.length;b+=1)navigator.mimeTypes.install(a[b])}install_plugin(FLASH_PLUGIN);})();'
);
injectScriptURL(
utils.runtime.getURL(
`dist/ruffle.js?uniqueMessageSuffix=${uniqueMessageSuffix}`
)
);
injectScriptURL(utils.runtime.getURL(`dist/ruffle.js?id=${ID}`));
window.addEventListener("message", (event) => {
// We only accept messages from ourselves.
@ -148,8 +145,8 @@ function isXMLDocument(): boolean {
return;
}
const { type, index, data } = event.data;
if (type === `TO_RUFFLE${uniqueMessageSuffix}`) {
const { to, index, data } = event.data;
if (to === `ruffle_content${ID}`) {
const request = pendingMessages[index];
if (request) {
pendingMessages[index] = null;

View File

@ -6,34 +6,32 @@ window.RufflePlayer = PublicAPI.negotiate(
new SourceAPI("extension")
);
let uniqueMessageSuffix: string | null = null;
let ID: string | null = null;
if (
document.currentScript !== undefined &&
document.currentScript !== null &&
"src" in document.currentScript &&
document.currentScript.src !== ""
) {
// Default to the directory where this script resides.
try {
uniqueMessageSuffix = new URL(
document.currentScript.src
).searchParams.get("uniqueMessageSuffix");
ID = new URL(document.currentScript.src).searchParams.get("id");
} catch (_) {
// uniqueMessageSuffix remains null.
// ID remains null.
}
}
if (uniqueMessageSuffix) {
if (ID) {
window.addEventListener("message", (event) => {
// We only accept messages from ourselves.
if (event.source !== window) {
return;
}
const { type, index, data } = event.data;
if (type === `FROM_RUFFLE${uniqueMessageSuffix}`) {
const { to, index, data } = event.data;
if (to === `ruffle_page${ID}`) {
// Ping back.
const message = {
type: `TO_RUFFLE${uniqueMessageSuffix}`,
to: `ruffle_content${ID}`,
index,
data,
};