代码之家  ›  专栏  ›  技术社区  ›  Andreas Moldskred

监听网络工作者的创建?

  •  3
  • Andreas Moldskred  · 技术社区  · 7 年前

    WebAssembly 工人和筛选出的网址,我应该终止。但是,在阅读完web worker API文档之后,我可以找出创建时在浏览器中调用的任何事件。

    我试着查看DOM中的突变观察者,但我想不出任何方法来监听创建。

    编辑: 我不认识工人。我正在做一个chrome扩展,在这个扩展中,我需要扫描所有页面以查找工作人员及其位置(url)

    1 回复  |  直到 7 年前
        1
  •  1
  •   Enthusiastic Developer    7 年前

    可能会有一些错误,我试图描述这个概念。所以,这个想法是工人可以通知父母他自己还活着。

    // MAIN JS THREAD
    
    const worker = new Worker('url/to/your/worke.js')
    
    worker.onmessage = message => {
      if (message === 'online') {
        // do something 
        return
      }
    
      // here can be your code that handles main response of the worker
    }
    
    // WORKER.js
    
    postMessage('online')
    
    // here you can evaluate some highload code
    // that you didn't want to block maon thread
    
    postMessage('result of highload code')
    推荐文章