Expose Itch object to HTML games using contextBridge #2507
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #2703
Basically, the "Itch" global wasn't getting exposed to my installed HTML5 game code when launched from the Itch app because of Electron's context isolation (it sounds like the existing code that modifies "global" used to work due to a bug in context isolation).
This change uses Electron's contextBridge.exposeInMainWorld function to expose the "Itch" global that is created in the (isolated) preload script.
I tested with two different HTML integration samples (which threw an "Itch is undefined" error before) to ensure that the "Itch" global (with API key) was exposed with this fix.
I observed this issue originally on Windows 10, and I only tested this change on Windows 10. I don't currently have access to any other operating systems.
EDIT: The following paragraph is about a different (but related) issue, that appears to be an Electron bug.
Having said that, neither of the samples appear to have API requests that actually work. The Itch documentation indicates that the "same origin policy" is disabled for HTML5 games launched from the app, but I saw CORS errors in the console. EDIT: The origin issue looks like it was a bug in Electron that was recently fixed (the Itch App code correctly sets
webPreferences.webSecurity
to false): electron/electron#23664