Skip to content

Commit cdb802e

Browse files
committed
Refactor: GDApi is now loaded only when the control pannel is opened
1 parent 420a068 commit cdb802e

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

Loader/gdmodext/js/injected.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
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
*/
2019
let 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
*/
2539
function 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
8493
if(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);

Loader/gdmodext/js/menu.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,22 @@ chrome.tabs.query({active: true, currentWindow: false}, function(tabs) {
3434
chrome.runtime.onMessage.addListener(function(event) {
3535
if(typeof event["id"] !== "undefined") {
3636
if(event["id"] === "pong") {
37-
document.getElementById("connecting").innerHTML = "Connected!";
38-
document.title = "GDmod Scene Selector"
39-
chrome.tabs.sendMessage(tabs[0].id, {message: "listScenes"});
37+
console.log("pong")
4038
} else if(event["id"] === "listScenes") {
4139
const scenes = document.getElementById("scenes");
4240
scenes.innerHTML = "";
4341
for(let sceneName of event.payload) {
4442
scenes.appendChild(createCard(sceneName));
4543
}
44+
} else if(event["id"] === "installedAPI") {
45+
// This means the game is connected and ready
46+
document.title = "GDmod Scene Selector";
47+
document.getElementById("connecting").innerHTML = "Connected!";
48+
chrome.tabs.sendMessage(tabs[0].id, {message: "listScenes"}); // Get list of scenes (will be moved)
4649
}
4750
}
4851
});
4952

50-
chrome.tabs.sendMessage(tabs[0].id, {message: "ping"});
53+
// Now that we opened the mod loader install the API into the game (the API is not crucial and is therefore only loaded when needed)
54+
chrome.tabs.sendMessage(tabs[0].id, {message: "installAPI"});
5155
});

0 commit comments

Comments
 (0)