From 56dd49aaed6c49402dcc88e6005594a2e4d3ef2c Mon Sep 17 00:00:00 2001 From: Christopher Willis-Ford <7019101+cwillisf@users.noreply.github.com> Date: Tue, 16 Aug 2022 23:42:55 -0700 Subject: [PATCH] fix(extensions): try harder to avoid Scratch Link init in non-browser environments Prior to this change, the Scratch Link init would fail in JSDOM. That means it fails in the scratch-gui integration tests, so that's no good... --- src/engine/runtime.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 10e2d3b7..d79337ca 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -1444,8 +1444,17 @@ class Runtime extends EventEmitter { */ _initScratchLink () { /* global globalThis */ - // Check if we're actually in a browser - if (globalThis.document && document.getElementById) { + // Check that we're actually in a real browser, not Node.js or JSDOM, and we have a valid-looking origin. + if (globalThis.document && + globalThis.document.getElementById && + globalThis.origin && + globalThis.origin !== 'null' && + globalThis.navigator && + globalThis.navigator.userAgent && + globalThis.navigator.userAgent.includes && + !globalThis.navigator.userAgent.includes('Node.js') && + !globalThis.navigator.userAgent.includes('jsdom') + ) { // Create a script tag for the Scratch Link browser extension, unless one already exists const scriptElement = document.getElementById('scratch-link-extension-script'); if (!scriptElement) {