【问题标题】:importScripts in webworker, ignore reference exceptionswebworker中的importScripts,忽略引用异常
【发布时间】:2015-06-09 15:44:36
【问题描述】:

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

干杯, 丹尼斯

【问题讨论】:

    标签: javascript import web-worker


    【解决方案1】:

    我在https://stackoverflow.com/a/27931746/1319998 中做了类似于在网络工作者中加载Angular 的事情。我的解决方案是在全局范围内创建对象的虚拟版本,具有足够的属性和函数,以便 Angular 在尝试访问它们时不会抛出异常,然后使用 importScripts 加载 Angular。

    下面的代码对我来说适用于 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');
    

    【讨论】:

    • 嗯...有趣。这绝对给了我一些想法。尽管现在的问题是导入在脚本中的 implementation.createDocument 语句上阻塞。可能必须查看骨架属性才能解决此问题。你知道如何解决这个问题吗?
    • @DennisBauszus 不是随便的。我认为您可能需要在问题中提供更多详细信息(/另一个问题)
    • 没问题。我接受这是正确的答案,并将在将来回复此问题。
    猜你喜欢
    • 2022-06-25
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-13
    • 1970-01-01
    • 2022-06-16
    相关资源
    最近更新 更多