简体   繁体   English

如何在HTML 5 Web worker中访问jQuery

[英]How to access jQuery in HTML 5 web worker

I am unable to to access jQuery inside an HTML5 web worker . 我无法在HTML5 Web工作者中访问jQuery。 Is there a way I can do that? 有没有办法可以做到这一点?

tl;dr: include this script before jQuery tl; dr:在jQuery之前包含这个脚本

JQuery initally accesses lots of document properties to check for browser features. JQuery最初访问许多document属性以检查浏览器功能。 document is not defined in Worker and you actually cannot create Document instance in web workers at this moment. document未在Worker中定义,此时您实际上无法在Web worker中创建Document实例。 JQuery isn't prepared for that - as you could see in comments on your question, nobody seems to understand what would you do with JQuery without DOM. JQuery没有为此做好准备 - 正如你在问题评论中看到的那样,似乎没有人理解你如何在没有DOM的情况下使用JQuery。

Since, as I said, JQuery needs document to initialise, I created a dummy fake document object. 因为,正如我所说,JQuery需要document初始化,我创建了一个假的伪文档对象。 This object acts as real document during JQuery tests: 在JQuery测试期间,此对象充当真实文档:

var document = self.document = {parentNode: null, nodeType: 9, toString: function() {return "FakeDocument"}};
var window = self.window = self;
var fakeElement = Object.create(document);
fakeElement.nodeType = 1;
fakeElement.toString=function() {return "FakeElement"};
fakeElement.parentNode = fakeElement.firstChild = fakeElement.lastChild = fakeElement;
fakeElement.ownerDocument = document;

document.head = document.body = fakeElement;
document.ownerDocument = document.documentElement = document;
document.getElementById = document.createElement = function() {return fakeElement;};
document.createDocumentFragment = function() {return this;};
document.getElementsByTagName = document.getElementsByClassName = function() {return [fakeElement];};
document.getAttribute = document.setAttribute = document.removeChild = 
  document.addEventListener = document.removeEventListener = 
  function() {return null;};
document.cloneNode = document.appendChild = function() {return this;};
document.appendChild = function(child) {return child;};

Beware that this fake document is only meant to allow jQuery to load, it won't allow any real DOM operations. 请注意,这个假文档只是为了允许加载jQuery,它不允许任何真正的DOM操作。

Example usage: 用法示例:

importScripts("workerFakeDOM.js");
importScripts('jquery-2.1.4.min.js');
console.log("JQuery version:", $.fn.jquery);

Test script 测试脚本

Allows you to try various versions of JQuery with my script. 允许您使用我的脚本尝试各种版本的JQuery。

JSfiddle 的jsfiddle

Check that you're using http:// , my domain doesn't work with https:// . 检查您是否使用http:// ,我的域无法使用https://

Download as script 下载为脚本

The answer of Tomáš Zato was correct, but no longer works with newer jQuery version. TomášZato的答案是正确的,但不再适用于较新的jQuery版本。 I added some more for jQuery 3.1.1: 我为jQuery 3.1.1添加了一些:

var document = self.document = { parentNode: null, nodeType: 9, toString: function () { return "FakeDocument" } };
var window = self.window = self;
var fakeElement = Object.create(document);
fakeElement.nodeType = 1;
fakeElement.toString = function () { return "FakeElement" };
fakeElement.parentNode = fakeElement.firstChild = fakeElement.lastChild = fakeElement;
fakeElement.ownerDocument = document;

document.head = document.body = fakeElement;
document.ownerDocument = document.documentElement = document;
document.getElementById = document.createElement = function () { return fakeElement; };
document.createDocumentFragment = function () { return this; };
document.getElementsByTagName = document.getElementsByClassName = function () { return [fakeElement]; };
document.getAttribute = document.setAttribute = document.removeChild =
    document.addEventListener = document.removeEventListener =
    function () { return null; };
document.cloneNode = document.appendChild = function () { return this; };
document.appendChild = function (child) { return child; };
document.childNodes = [];
document.implementation = {
    createHTMLDocument: function () { return document; }
}

Has anyone tried the jQuery - No DOM Edition? 有没有人尝试过jQuery - 没有DOM版? https://github.com/kpozin/jquery-nodom . https://github.com/kpozin/jquery-nodom

This is a small subset of the jQuery library intended for use in Web Worker contexts, where most of the browser API does not exist. 这是jQuery库的一小部分,旨在用于Web Worker上下文,其中大多数浏览器API不存在。

This is achieved primarily by modifying jQuery build instructions (Makefile) to only include the non-DOM modules. 这主要是通过修改jQuery构建指令(Makefile)以仅包含非DOM模块来实现的。

This might help resolve the issue as this build has no DOM dependency, which is a hurdle when importing jQuery in webworker. 这可能有助于解决问题,因为此构建没有DOM依赖,这在webworker中导入jQuery时是一个障碍。 Will try to provide some sample code soon for this 将尝试尽快提供一些示例代码

There is a nice plugin to use Web Worker with jQuery by one of jQuery's own leaders. 有一个很好的插件可以让jQuery自己的领导者使用Web Worker和jQuery。 Check out his plugin on GitHub . 看看他在GitHub上的插件

Does using: 使用:

importScripts("jQueryWhatever.js");
$.blahblahblah();

Not work as expected? 不按预期工作? I suspect it will load jQuery without trouble, but most of the calls associated with good-ole $ will not work as expected as there is no DOM access inside a WebWorker. 我怀疑它会毫无困难地加载jQuery,但是与good-ole $相关的大多数调用都无法正常工作,因为WebWorker中没有DOM访问。

As an alternative, you can use Cheerio . 作为替代方案,您可以使用Cheerio

Fast, flexible & lean implementation of core jQuery designed specifically for the server. 快速,灵活和精简的核心jQuery实现,专为服务器而设计。

You will simply need to browserify the module, and loading the resulting file into your web worker. 你只需要简单地browserify模块,并加载生成的文件到您的网络工作者。 Then, you will be able to parse your string document and query it like jQuery does: 然后,您将能够解析您的字符串文档并像jQuery一样查询它:

$ = cheerio.load('<h2 class="title">Hello world</h2>')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM