mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 06:52:40 -05:00
feat(extensions): support Scratch Link 2.0 browser extension
Right now this is only needed for Safari
This commit is contained in:
parent
22f89ed69a
commit
2bdea9106d
1 changed files with 28 additions and 1 deletions
|
@ -399,6 +399,8 @@ class Runtime extends EventEmitter {
|
||||||
* @type {?string}
|
* @type {?string}
|
||||||
*/
|
*/
|
||||||
this.origin = null;
|
this.origin = null;
|
||||||
|
|
||||||
|
this._initScratchLink();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1437,6 +1439,27 @@ class Runtime extends EventEmitter {
|
||||||
(result, categoryInfo) => result.concat(categoryInfo.blocks.map(blockInfo => blockInfo.json)), []);
|
(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.
|
* Get a scratch link socket.
|
||||||
* @param {string} type Either BLE or BT
|
* @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)
|
* @returns {ScratchLinkSocket} The new scratch link socket (a WebSocket object)
|
||||||
*/
|
*/
|
||||||
_defaultScratchLinkSocketFactory (type) {
|
_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