webworker 中的 importScripts,忽略引用异常

importScripts in webworker, ignore reference exceptions

我正在尝试在包含文档引用的 WebWorker 中导入一个 javascript 文件,因此失败了。是否可以抑制引用异常?我只需要 javascript 文件中的一些函数,这些函数不应使用文档对象。或者我是否需要从源代码重建 js 库,排除我不需要的所有内容。

干杯, 丹尼斯

我在 中在 Web Worker 中做了类似于加载 Angular 的事情。我的解决方案是在全局范围内创建对象的虚拟版本,具有足够的属性和函数,因此 Angular 在尝试访问它们时不会抛出异常,然后使用 [= 加载 Angular 11=].

下面的代码适用于 Angular,它不仅需要 document,还需要 windowhistory

// In the web worker

// Angular needs a global window object
self.window = self;

// Skeleton properties to get Angular to load and bootstrap.
self.history = {};
self.document = {
  readyState: 'complete',
  querySelector: function() {},
  createElement: function() {
    return {
      pathname: '',
      setAttribute: function() {}
    }
  }
};

// Load Angular: must be on same domain as this script
self.importScripts('angular.js');