网络工作者抛出'未捕获错误:DATA_CLONE_ERR:DOM异常25'

时间:2022-02-12 20:29:21

So I'm creating a web worker:

所以我正在创建一个Web worker:

var arrayit = function(obj) {
  return Array.prototype.slice.call(obj);
};
work = arrayit(images);
console.log(work);
//work = images.push.apply( images, array );
// Method : "load+scroll"
var worker = new Worker('jail_worker.js');
worker.postMessage(work)
worker.onmessage = function(event) {
  console.log("Worker said:" + event.data);
};

Here's what images is:

这是图像是什么:

$.jail.initialStack = this;
// Store the selector into 'triggerEl' data for the images selected
this.data('triggerEl', (options.selector) ? $(options.selector) : $window);
var images = this;

I think my problem has something to do with this:

我认为我的问题与此有关:

http://dev.w3.org/html5/spec/Overview.html#safe-passing-of-structured-data

http://dev.w3.org/html5/spec/Overview.html#safe-passing-of-structured-data

How can I get around this? as you can see, I tried slicing the host object into a real array, but that didn't work.

我怎么能绕过这个?正如你所看到的,我尝试将主机对象切成一个真正的数组,但这不起作用。

Here's a link to the file I'm hacking on:

这是我正在攻击的文件的链接:

https://github.com/jtmkrueger/JAIL

https://github.com/jtmkrueger/JAIL

UPDATE--------------------------------------------------

UPDATE ------------------------------------------------- -

This is what I had to do based on the accepted answer from @davin:

这是我根据@davin接受的答案所做的事情:

var arrayit = function(obj) {
  return Array.prototype.slice.call(obj);
};
imgArray = arrayit(images);
work = _.map(images, function(i){ return i.attributes[0].ownerElement.outerHTML; });

var worker = new Worker('jail_worker.js');
worker.postMessage(work)
worker.onmessage = function(event) {
  console.log("Worker said:" + event.data);
};

NOTE: I used underscore.js to assure compatibility.

注意:我使用underscore.js来确保兼容性。

2 个解决方案

#1


22  

The original exception was most likely thrown because you tried passing a host object to the web worker (most likely a dom element). Your subsequent attempts don't throw the same error. Remember two key points: there isn't shared memory between the different threads, and the web workers can't manipulate the DOM.

最初的异常很可能是因为您尝试将主机对象传递给Web worker(很可能是dom元素)。您的后续尝试不会抛出相同的错误。请记住两个关键点:不同线程之间没有共享内存,Web工作者无法操作DOM。

postMessage supports passing structured data to threads, and will internally serialise (or in some other way copy the value of the data recursively) the data. Serialising DOM elements often results in circular reference errors, so your best bet is to map the object you want serialised and extract relevant data to be rebuilt in the web worker.

postMessage支持将结构化数据传递给线程,并将内部序列化(或以其他方式递归地复制数据的值)数据。序列化DOM元素通常会导致循环引用错误,因此最好的办法是映射要序列化的对象,并提取要在Web worker中重建的相关数据。

#2


0  

Uncaught DataCloneError: An object could not be cloned was reproduced when tried save to indexeddb function as object's key. Need double recheck that saved object is serializable

未捕获的DataCloneError:当尝试保存到indexeddb函数作为对象的键时,无法克隆的对象被重现。需要双重检查,保存的对象是可序列化的

#1


22  

The original exception was most likely thrown because you tried passing a host object to the web worker (most likely a dom element). Your subsequent attempts don't throw the same error. Remember two key points: there isn't shared memory between the different threads, and the web workers can't manipulate the DOM.

最初的异常很可能是因为您尝试将主机对象传递给Web worker(很可能是dom元素)。您的后续尝试不会抛出相同的错误。请记住两个关键点:不同线程之间没有共享内存,Web工作者无法操作DOM。

postMessage supports passing structured data to threads, and will internally serialise (or in some other way copy the value of the data recursively) the data. Serialising DOM elements often results in circular reference errors, so your best bet is to map the object you want serialised and extract relevant data to be rebuilt in the web worker.

postMessage支持将结构化数据传递给线程,并将内部序列化(或以其他方式递归地复制数据的值)数据。序列化DOM元素通常会导致循环引用错误,因此最好的办法是映射要序列化的对象,并提取要在Web worker中重建的相关数据。

#2


0  

Uncaught DataCloneError: An object could not be cloned was reproduced when tried save to indexeddb function as object's key. Need double recheck that saved object is serializable

未捕获的DataCloneError:当尝试保存到indexeddb函数作为对象的键时,无法克隆的对象被重现。需要双重检查,保存的对象是可序列化的