mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 06:52:40 -05:00
Merge pull request #3712 from LLK/scratch-link-safari-helper-compat
feat(extensions): support Scratch Link 2.0 browser extension
This commit is contained in:
commit
dc23254461
1 changed files with 28 additions and 1 deletions
|
@ -399,6 +399,8 @@ class Runtime extends EventEmitter {
|
|||
* @type {?string}
|
||||
*/
|
||||
this.origin = null;
|
||||
|
||||
this._initScratchLink();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1437,6 +1439,27 @@ class Runtime extends EventEmitter {
|
|||
(result, categoryInfo) => result.concat(categoryInfo.blocks.map(blockInfo => blockInfo.json)), []);
|
||||
}
|
||||
|
||||
/**
|
||||
* One-time initialization for Scratch Link support.
|
||||
*/
|
||||
_initScratchLink () {
|
||||
/* global globalThis */
|
||||
// Check if we're actually in a browser
|
||||
if (globalThis.document && document.getElementById) {
|
||||
// Create a script tag for the Scratch Link browser extension, unless one already exists
|
||||
const scriptElement = document.getElementById('scratch-link-extension-script');
|
||||
if (!scriptElement) {
|
||||
const script = document.createElement('script');
|
||||
script.id = 'scratch-link-extension-script';
|
||||
document.body.appendChild(script);
|
||||
|
||||
// Tell the browser extension to inject its script.
|
||||
// If the extension isn't present or isn't active, this will do nothing.
|
||||
globalThis.postMessage('inject-scratch-link-script', globalThis.origin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a scratch link socket.
|
||||
* @param {string} type Either BLE or BT
|
||||
|
@ -1462,7 +1485,11 @@ class Runtime extends EventEmitter {
|
|||
* @returns {ScratchLinkSocket} The new scratch link socket (a WebSocket object)
|
||||
*/
|
||||
_defaultScratchLinkSocketFactory (type) {
|
||||
return new ScratchLinkWebSocket(type);
|
||||
const Scratch = self.Scratch;
|
||||
const ScratchLinkSafariSocket = Scratch && Scratch.ScratchLinkSafariSocket;
|
||||
// detect this every time in case the user turns on the extension after loading the page
|
||||
const useSafariSocket = ScratchLinkSafariSocket && ScratchLinkSafariSocket.isSafariHelperCompatible();
|
||||
return useSafariSocket ? new ScratchLinkSafariSocket(type) : new ScratchLinkWebSocket(type);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue