如何让Worker()针对跨域工作? [英] How do I make Worker() work against cross-domain?

查看:1403
本文介绍了如何让Worker()针对跨域工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在攻击BeSpin,试图让它在我的CDN上工作,我通过转换为使用JSONP,超越了XHR跨主题请求。我遇到的下一个问题是新的Worker(js_file),其中js_file位于不同的域中。

I've been hacking away at BeSpin trying to make it work on my CDN, and I got past the XHR cross domain request for the theme.less by converting to use JSONP. The next problem I've been at is the new Worker(js_file) where js_file is on a different domain.

如何/我可以为工作人员启用跨域()?

How do/Can I enable cross-domain for a Worker()?

我可以直接向工人提供源代码吗? (即构建一个超大的JavaScript文件,其中嵌入了另一个文件)[这不太理想,但应该可以工作]。

Can I give the Worker the source code directly? (i.e. build a super large JavaScript file with another file embedded in it) [this is less than ideal, but it should work].

推荐答案

试试这个:


  • 使用工人的代码创建一个函数

  • 获取字符串表示函数(.toString),删除第一行和最后一行。现在你有一个包含工人代码的字符串

  • 创建一个新的BlobBuilder(window.MozBlobBuilder || window.WebKitBlobBuilder || window.BlobBuilder)

  • 将工作字符串附加到它

  • 使用窗口的URL调用getBlob()来获取blob

  • (window.URL || window.webkitURL)使用createObjectURL创建一个对象网址

  • 为工作者使用该网址

  • create a function with the worker's code
  • get the string representation of the function (.toString), remove the first and last line. Now you have a string with the worker's code
  • create a new BlobBuilder ( window.MozBlobBuilder || window.WebKitBlobBuilder || window.BlobBuilder)
  • append the worker string to it
  • call getBlob() to get a blob
  • using the window's URL (window.URL || window.webkitURL) create an object url using createObjectURL
  • use that url for the worker

这是代码

function getUrlForWorker(workerFunction) {
    var BlobBuilder = window.MozBlobBuilder || window.WebKitBlobBuilder || window.BlobBuilder,
    URL = window.URL || window.webkitURL,
        mainString = workerFunction.toString(),
        bodyString     = mainString.substring( mainString.indexOf("{")+1, mainString.lastIndexOf("}") ),
        bb = new BlobBuilder()

    bb.append(bodyString)

    return URL.createObjectURL(bb.getBlob())
}

这篇关于如何让Worker()针对跨域工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆