11/**
2- * This is a file injected into HTML documents by GDMod patcher.
3- * It is for supporting patching games in IFrames bypassing the cross-origin policy.
2+ * This is the file injected into HTML documents by GDMod patcher.
43 * @fileoverview
54 */
65
@@ -12,17 +11,34 @@ const debug = false;
1211/**
1312 * The CDN to fetch the GDAPI files from
1413 */
15- const CDN = "https://cdn.jsdelivr.net/gh/arthuro555/gdmod@8ec6b07e41cedc8c23b5708d8ed468bffbe6fb0c /API/" ;
14+ const CDN = "https://cdn.jsdelivr.net/gh/arthuro555/gdmod/API/" ;
1615
1716/**
1817 * Flag telling if that page got patched already.
1918 */
2019let isPatched = false ;
2120
21+ /**
22+ * Flag telling if that page has loaded the API already.
23+ */
24+ let isAPILoaded = false ;
25+
26+
27+ /**
28+ * Send an object to the Popup
29+ * @param {string } id The identifier of the payload (tells the popup what to do with the data)
30+ * @param {any } payload The message to post to the Popup
31+ */
32+ function postToPopup ( id , payload ) {
33+ window . postMessage ( { forwardTo : "GDMod" , payload : { id : id , payload : payload } } , "*" ) ;
34+ }
35+
2236/**
2337 * Installs the modding API.
2438 */
2539function installGDModAPI ( ) {
40+ if ( isAPILoaded ) return ;
41+
2642 return fetch ( CDN + "includes.json" )
2743 . then ( req => req . json ( ) )
2844 . then ( includes => {
@@ -34,6 +50,8 @@ function installGDModAPI() {
3450 script . src = CDN + include ;
3551 script . onload = function ( ) {
3652 if ( ++ loaded === includes . length ) {
53+ isAPILoaded = true ;
54+ postToPopup ( "installedAPI" , true ) ;
3755 resolve ( ) ;
3856 }
3957 if ( ! debug ) document . body . removeChild ( script ) ; // Cleanup document after loading API.
@@ -70,15 +88,6 @@ function patchSceneCode() {
7088 }
7189}
7290
73- /**
74- * Send an object to the Popup
75- * @param {string } id The identifier of the payload (tells the popup what to do with the data)
76- * @param {any } payload The message to post to the Popup
77- */
78- function postToPopup ( id , payload ) {
79- window . postMessage ( { forwardTo : "GDMod" , payload : { id : id , payload : payload } } , "*" ) ;
80- }
81-
8291
8392// First we verify if the game is a GDevelop game
8493if ( window . gdjs !== undefined ) {
@@ -90,12 +99,13 @@ if(window.gdjs !== undefined) {
9099 * it still executes the original code and we won't have access until the scene switches
91100 * once. This comment is here because we used to patch only on request by the user.
92101 */
93- installGDModAPI ( ) ; // We install the modding API.
94102
95103 window . addEventListener ( "message" , function ( event ) {
96104 if ( typeof event . data [ "message" ] !== "undefined" ) {
97105 if ( event . data [ "message" ] === "ping" ) {
98106 postToPopup ( "pong" , true ) ;
107+ } else if ( event . data [ "message" ] === "installAPI" ) {
108+ installGDModAPI ( ) ;
99109 } else if ( event . data [ "message" ] === "listScenes" ) {
100110 let allScenes = [ ] ;
101111 for ( let scene of gdjs . projectData . layouts ) allScenes . push ( scene . name ) ;
0 commit comments