最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Workers, web or service; anyway to access window object? - Stack Overflow

programmeradmin3浏览0评论

I am trying to utilise web workers in a webOS for signage application due to an odd bug where times between executing file downloads using the SCAP API slows down exponentially.

My idea was to try web workers to download the files, hopefully meaning the issue would go away/the app would be a bit snappier.

However, the SCAP API is based on Cordova and its seems Cordova needs to access the Window object, something a Web Worker cannot do it seems (I finally found out after hours of trying!)

My question is, is there anyway at all to get a web worker to work in conjunction with Cordova?

Are there any other types of workers that can access the window object?

Basically, is there any solution to this at all? Or is it 100% impossible and futile to attempt?

I am trying to utilise web workers in a webOS for signage application due to an odd bug where times between executing file downloads using the SCAP API slows down exponentially.

My idea was to try web workers to download the files, hopefully meaning the issue would go away/the app would be a bit snappier.

However, the SCAP API is based on Cordova and its seems Cordova needs to access the Window object, something a Web Worker cannot do it seems (I finally found out after hours of trying!)

My question is, is there anyway at all to get a web worker to work in conjunction with Cordova?

Are there any other types of workers that can access the window object?

Basically, is there any solution to this at all? Or is it 100% impossible and futile to attempt?

Share Improve this question asked Sep 11, 2015 at 16:12 Mr PabloMr Pablo 4,1879 gold badges54 silver badges111 bronze badges 3
  • 1 Workers cannot directly reach a window, ever. you might be able to rewrite the API calls to not use anything that's window-only. you could download in a worker using ajax, and the transfer the blob via postMessage() to window at the last moment to download it to the OS. – dandavis Commented Sep 11, 2015 at 16:33
  • Yea, that was my next idea, to use XHR to download the files. But passing the blob through to the main app doesn't seem like it would make anything better. Unless the worker could write the file itself, is that possible? – Mr Pablo Commented Sep 11, 2015 at 16:40
  • afaik, no environ provides Workers with any call that could be used to save a file; namely window.open(), msSaveBlob(), or a[download].click()... you might be able to create a cordova app that runs in the background and only downloads stuff, when the other app tells it to. – dandavis Commented Sep 11, 2015 at 16:43
Add a ment  | 

1 Answer 1

Reset to default 4

You cannot use a service worker to control any window object.

Service workers run in a worker context (not a browser context); it therefore has no DOM access.

Since things like postMessage() is a window function, and window is part of the DOM, you cannot window.postMessage() from a service worker. And unfortunately, client.postMessage() only works between browser contexts (tabs, windows, etc.) from the same domain origin (and service worker "scope").

发布评论

评论列表(0)

  1. 暂无评论