带有Tesseract的Ionic 4离线获取“未捕获的DOMException:无法在'WorkerGlobalScope'上执行'importScripts'”

时间:2019-04-13 17:11:38

标签: android cordova ionic-framework ionic4 tesseract.js

我正在尝试在Tesseract应用中以离线模式使用Ionic 4。为了做到这一点,我将代码建立在this example上,尽管它是用Ionic 3完成的,以及Tesseract GitHub关于offline mode的解释。

首先,我将Tesseract文件放在src\assets\lib目录中,如下所示(文件的tesseract-前缀已由我添加):

Assets directory

接下来,我创建了一个服务,该服务基本上创建了Tesseract脱机模式实例,如上面提到的链接所示:

  const path = this.webview.convertFileSrc(this.file.applicationDirectory + 'www/assets/lib/');

  this.tesseract = await Tesseract.create({
    langPath: path + 'tesseract-', 
    corePath: path + 'tesseract-index.js',
    workerPath: path + 'tesseract-worker.js',
  });

有关代码的一些注释:

  • this.fileFile的{​​{1}}。
  • 调用'@ionic-native/file/ngx'是为了避免在尝试直接加载Javascript文件时遇到的convertFileSrc错误。
  • 如果我使用unable to load resource登录this.file.listDir的内容,则可以看到this.file.applicationDirectory + 'www/assets/lib/'文件。

现在,当我将其部署到Tesseract并尝试调用此代码所在的函数时,根据Chrome调试器,我得到以下错误:

importScripts error

FWIW,这是我的环境:

Android emulator (Pixel 2 API 28)

我想念什么?访问Ionic: ionic (Ionic CLI) : 4.12.0 (C:\Users\guillem.vicens\AppData\Roaming\nvm\v10.15.3\node_modules\ionic) Ionic Framework : @ionic/angular 4.1.2 @angular-devkit/build-angular : 0.13.6 @angular-devkit/schematics : 7.2.4 @angular/cli : 7.3.6 @ionic/angular-toolkit : 1.4.1 Cordova: cordova (Cordova CLI) : not installed Cordova Platforms : android 8.0.0 Cordova Plugins : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 3.1.2, (and 6 other plugins) System: Android SDK Tools : 26.1.1 (C:\Users\myUser\AppData\Local\Android\Sdk) NodeJS : v10.15.3 (C:\Program Files\nodejs\node.exe) npm : 6.4.1 OS : Windows 10 文件夹的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

我终于设法通过使用未计算的URL库解决了我的问题。

我注意到tesseract-tesseract-js文件是从以下URL加载的:

http://localhost/assets/lib/tesseract-tesseract.js

但是没有其他人被加载。这使我认为问题出在某种程度上与Tesseract.js的内部使用有关,这些内部路径与Webview安全策略相冲突。

更改代码以执行以下操作即可:

  this.tesseract = await Tesseract.create({
    langPath: 'http://localhost/assets/lib/tesseract-', 
    corePath: 'http://localhost/assets/lib/tesseract-index.js',
    workerPath: 'http://localhost/assets/lib/tesseract-worker.js',
  });

我将不得不在真实的手机和iOS上对此进行测试,但这可以回答我的原始问题。