From 3f98fdeb0f7ca95890a046b466d43f6d0f345279 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Couton?= Date: Mon, 12 Oct 2020 22:12:08 +0200 Subject: [PATCH 1/2] Fix fetch when running in electron webview --- src/worker-script/browser/index.js | 1 + src/worker-script/index.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/worker-script/browser/index.js b/src/worker-script/browser/index.js index 06fd336..2e177a8 100644 --- a/src/worker-script/browser/index.js +++ b/src/worker-script/browser/index.js @@ -29,4 +29,5 @@ worker.setAdapter({ gunzip, fetch: () => {}, ...cache, + isWebWorker: typeof window === 'undefined' && typeof importScripts === 'function', }); diff --git a/src/worker-script/index.js b/src/worker-script/index.js index 5166ab8..6034cc6 100644 --- a/src/worker-script/index.js +++ b/src/worker-script/index.js @@ -99,7 +99,7 @@ const loadLanguage = async ({ } if (path !== null) { - const resp = await (isWebWorker ? fetch : adapter.fetch)(`${path}/${lang}.traineddata${gzip ? '.gz' : ''}`); + const resp = await (isWebWorker || adapter.isWebWorker ? fetch : adapter.fetch)(`${path}/${lang}.traineddata${gzip ? '.gz' : ''}`); data = await resp.arrayBuffer(); } else { data = await adapter.readCache(`${langPath}/${lang}.traineddata${gzip ? '.gz' : ''}`); From 820c91fa524951902299fb61b4d9732f651e70bc Mon Sep 17 00:00:00 2001 From: Jerome Wu Date: Wed, 14 Oct 2020 09:59:37 +0800 Subject: [PATCH 2/2] Fix electron webview by updating src/utils/getEnvironment.js --- src/utils/getEnvironment.js | 8 ++++---- src/worker-script/browser/index.js | 1 - src/worker-script/index.js | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/utils/getEnvironment.js b/src/utils/getEnvironment.js index 3b4f100..03f5663 100644 --- a/src/utils/getEnvironment.js +++ b/src/utils/getEnvironment.js @@ -3,12 +3,12 @@ const isElectron = require('is-electron'); module.exports = (key) => { const env = {}; - if (isElectron()) { - env.type = 'electron'; + if (typeof WorkerGlobalScope !== 'undefined') { + env.type = 'webworker'; } else if (typeof window === 'object') { env.type = 'browser'; - } else if (typeof importScripts === 'function') { - env.type = 'webworker'; + } else if (isElectron()) { + env.type = 'electron'; } else if (typeof process === 'object' && typeof require === 'function') { env.type = 'node'; } diff --git a/src/worker-script/browser/index.js b/src/worker-script/browser/index.js index 2e177a8..06fd336 100644 --- a/src/worker-script/browser/index.js +++ b/src/worker-script/browser/index.js @@ -29,5 +29,4 @@ worker.setAdapter({ gunzip, fetch: () => {}, ...cache, - isWebWorker: typeof window === 'undefined' && typeof importScripts === 'function', }); diff --git a/src/worker-script/index.js b/src/worker-script/index.js index 6034cc6..5166ab8 100644 --- a/src/worker-script/index.js +++ b/src/worker-script/index.js @@ -99,7 +99,7 @@ const loadLanguage = async ({ } if (path !== null) { - const resp = await (isWebWorker || adapter.isWebWorker ? fetch : adapter.fetch)(`${path}/${lang}.traineddata${gzip ? '.gz' : ''}`); + const resp = await (isWebWorker ? fetch : adapter.fetch)(`${path}/${lang}.traineddata${gzip ? '.gz' : ''}`); data = await resp.arrayBuffer(); } else { data = await adapter.readCache(`${langPath}/${lang}.traineddata${gzip ? '.gz' : ''}`);