<h1 align="center">
<img width="100" height="100" src="logo.svg" alt=""><br>
jsdom
</h1>
jsdom is a pure-JavaScript implementation of many web standards, notably the WHATWG [DOM](https://dom.spec.whatwg.org/) and [HTML](https://html.spec.whatwg.org/multipage/) Standards, for use with Node.js. In general, the goal of the project is to emulate enough of a subset of a web browser to be useful for testing and scraping real-world web applications.
The latest versions of jsdom require Node.js v18 or newer. (Versions of jsdom below v23 still work with previous Node.js versions, but are unsupported.)
## Basic usage
```js
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
```
To use jsdom, you will primarily use the `JSDOM` constructor, which is a named export of the jsdom main module. Pass the constructor a string. You will get back a `JSDOM` object, which has a number of useful properties, notably `window`:
```js
const dom = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
console.log(dom.window.document.querySelector("p").textContent); // "Hello world"
```
(Note that jsdom will parse the HTML you pass it just like a browser does, including implied `<html>`, `<head>`, and `<body>` tags.)
The resulting object is an instance of the `JSDOM` class, which contains a number of useful properties and methods besides `window`. In general, it can be used to act on the jsdom from the "outside," doing things that are not possible with the normal DOM APIs. For simple cases, where you don't need any of this functionality, we recommend a coding pattern like
```js
const { window } = new JSDOM(`...`);
// or even
const { document } = (new JSDOM(`...`)).window;
```
Full documentation on everything you can do with the `JSDOM` class is below, in the section "`JSDOM` Object API".
## Customizing jsdom
The `JSDOM` constructor accepts a second parameter which can be used to customize your jsdom in the following ways.
### Simple options
```js
const dom = new JSDOM(``, {
url: "https://example.org/",
referrer: "https://example.com/",
contentType: "text/html",
includeNodeLocations: true,
storageQuota: 10000000
});
```
- `url` sets the value returned by `window.location`, `document.URL`, and `document.documentURI`, and affects things like resolution of relative URLs within the document and the same-origin restrictions and referrer used while fetching subresources. It defaults to `"about:blank"`.
- `referrer` just affects the value read from `document.referrer`. It defaults to no referrer (which reflects as the empty string).
- `contentType` affects the value read from `document.contentType`, as well as how the document is parsed: as HTML or as XML. Values that are not a [HTML MIME type](https://mimesniff.spec.whatwg.org/#html-mime-type) or an [XML MIME type](https://mimesniff.spec.whatwg.org/#xml-mime-type) will throw. It defaults to `"text/html"`. If a `charset` parameter is present, it can affect [binary data processing](#encoding-sniffing).
- `includeNodeLocations` preserves the location info produced by the HTML parser, allowing you to retrieve it with the `nodeLocation()` method (described below). It also ensures that line numbers reported in exception stack traces for code running inside `<script>` elements are correct. It defaults to `false` to give the best performance, and cannot be used with an XML content type since our XML parser does not support location info.
- `storageQuota` is the maximum size in code units for the separate storage areas used by `localStorage` and `sessionStorage`. Attempts to store data larger than this limit will cause a `DOMException` to be thrown. By default, it is set to 5,000,000 code units per origin, as inspired by the HTML specification.
Note that both `url` and `referrer` are canonicalized before they're used, so e.g. if you pass in `"https:example.com"`, jsdom will interpret that as if you had given `"https://example.com/"`. If you pass an unparseable URL, the call will throw. (URLs are parsed and serialized according to the [URL Standard](https://url.spec.whatwg.org/).)
### Executing scripts
jsdom's most powerful ability is that it can execute scripts inside the jsdom. These scripts can modify the content of the page and access all the web platform APIs jsdom implements.
However, this is also highly dangerous when dealing with untrusted content. The jsdom sandbox is not foolproof, and code running inside the DOM's `<script>`s can, if it tries hard enough, get access to the Node.js environment, and thus to your machine. As such, the ability to execute scripts embedded in the HTML is disabled by default:
```js
const dom = new JSDOM(`<body>
<div id="content"></div>
<script>document.getElementById("content").append(document.createElement("hr"));</script>
</body>`);
// The script will not be executed, by default:
console.log(dom.window.document.getElementById("content").children.length); // 0
```
To enable executing scripts inside the page, you can use the `runScripts: "dangerously"` option:
```js
const dom = new JSDOM(`<body>
<div id="content"></div>
<script>document.getElementById("content").append(document.createElement("hr"));</script>
</body>`, { runScripts: "dangerously" });
// The script will be executed and modify the DOM:
console.log(dom.window.document.getElementById("content").children.length); // 1
```
Again we emphasize to only use this when feeding jsdom code you know is safe. If you use it on arbitrary user-supplied code, or code from the Internet, you are effectively running untrusted Node.js code, and your machine could be compromised.
If you want to execute _external_ scripts, included via `<script src="">`, you'll also need to ensure that they load them. To do this, add the option `resources: "usable"` [as described below](#loading-subresources). (You'll likely also want to set the `url` option, for the reasons discussed there.)
Event handler attributes, like `<div onclick="">`, are also governed by this setting; they will not function unless `runScripts` is set to `"dangerously"`. (However, event handler _properties_, like `div.onclick = ...`, will function regardless of `runScripts`.)
If you are simply trying to execute script "from the outside", instead of letting `<script>` elements and event handlers attributes run "from the inside", you can use the `runScripts: "outside-only"` option, which enables fresh copies of all the JavaScript spec-provided globals to be installed on `window`. This includes things like `window.Array`, `window.Promise`, etc. It also, notably, includes `window.eval`, which allows running scripts, but with the jsdom `window` as the global:
```js
const dom = new JSDOM(`<body>
<div id="content"></div>
<script>document.getElementById("content").append(document.createElement("hr"));</script>
</body>`, { runScripts: "outside-only" });
// run a script outside of JSDOM:
dom.window.eval('document.getElementById("content").append(document.createElement("p"));');
console.log(dom.window.document.getElementById("content").children.length); // 1
console.log(dom.window.document.getElementsByTagName("hr").length); // 0
console.log(dom.window.document.getElementsByTagName("p").length); // 1
```
This is turned off by default for performance reasons, but is safe to enable.
Note that in the default configuration, without setting `runScripts`, the values of `window.Array`, `window.eval`, etc. will be the same as those provided by the outer Node.js environment. That is, `window.eval === eval` will hold, so `window.eval` will not run scripts in a useful way.
We strongly advise against trying to "execute scripts" by mashing together the jsdom and Node global environments (e.g. by doing `global.window = dom.window`), and then executing scripts or test code inside the Node global environment. Instead, you should treat jsdom like you would a browser, and run all scripts and tests that need access to a DOM inside the jsdom environment, using `window.eval` or `r
没有合适的资源?快使用搜索试试~ 我知道了~
佩奇老师讲的最新的抖音弹幕
共2000个文件
py:989个
js:816个
json:66个
需积分: 0 0 下载量 183 浏览量
更新于2025-01-12
收藏 53.55MB ZIP 举报
佩奇老师讲的最新的弹幕源码,自己测试了就一下可以获取弹幕,笔记写的还是比较全面的。照着操作就可以了,本源码仅供学习,不可以商用,否则后果自负。
收起资源包目录
佩奇老师讲的最新的抖音弹幕 (2000个子文件)
greenlet.h 5KB
index.html 174B
external.html 92B
css-color.min.js 172KB
index.js 165KB
trie.js 142KB
trie.js 142KB
decimal.js 133KB
Document.js 133KB
Element.js 120KB
index.js 114KB
index.js 105KB
index.js 104KB
index.umd.min.js 104KB
index.cjs.min.js 103KB
index.esm.min.js 103KB
HTMLElement.js 97KB
index.js 96KB
color.js 86KB
SVGElement.js 86KB
saxes.js 72KB
nwsapi.js 70KB
ElementInternals.js 69KB
xpath.js 69KB
regexes.js 67KB
HTMLInputElement.js 62KB
properties.js 56KB
index.js 54KB
index.js 54KB
decode-data-html.js 47KB
decode-data-html.js 47KB
CSSOM.js 46KB
cookieJar.js 40KB
HTMLTextAreaElement.js 39KB
websocket.js 36KB
HTMLInputElement-impl.js 36KB
Node-impl.js 34KB
HTMLAnchorElement.js 32KB
Window.js 32KB
XMLHttpRequest-impl.js 32KB
HTMLSelectElement.js 32KB
sbcs-data-generated.js 31KB
cookie.js 31KB
url-state-machine.js 30KB
HTMLObjectElement.js 30KB
SymbolTree.js 29KB
HTMLMediaElement.js 28KB
HTMLImageElement.js 28KB
HTMLBodyElement.js 28KB
Document-impl.js 28KB
encode-html.js 26KB
encode-html.js 26KB
Range-impl.js 26KB
Node.js 26KB
HTMLTableElement.js 26KB
HTMLAreaElement.js 25KB
HTMLTableCellElement.js 23KB
SVGSVGElement.js 23KB
HTMLFrameSetElement.js 23KB
dbcs-codec.js 23KB
decode.js 22KB
HTMLIFrameElement.js 22KB
Range.js 22KB
XMLHttpRequest.js 21KB
css-calc.js 21KB
index.js 20KB
decode.js 19KB
HTMLMarqueeElement.js 18KB
Selection.js 18KB
html.js 17KB
HTMLLinkElement.js 17KB
DOMTokenList.js 17KB
html.js 17KB
NamedNodeMap.js 17KB
URLSearchParams.js 17KB
HTMLButtonElement.js 17KB
index.min.js 17KB
Element-impl.js 17KB
MouseEvent.js 17KB
index.min.js 17KB
sender.js 16KB
receiver.js 16KB
HTMLFrameElement.js 16KB
HTMLOptionsCollection.js 16KB
SVGStringList.js 16KB
websocket-server.js 16KB
HTMLFormElement.js 16KB
parsers.js 15KB
tests.js 15KB
FormData.js 15KB
URL.js 15KB
HTMLScriptElement.js 15KB
CharacterData.js 15KB
WebSocket.js 15KB
FileReader.js 14KB
default-stylesheet.js 14KB
Headers.js 14KB
permessage-deflate.js 14KB
parse.js 14KB
xhr-utils.js 14KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源推荐
资源预览
资源评论
174 浏览量
105 浏览量
176 浏览量
166 浏览量
2021-11-13 上传
5星 · 资源好评率100%
183 浏览量
2021-10-03 上传
2018-08-09 上传
5星 · 资源好评率100%
175 浏览量
109 浏览量
134 浏览量
2024-01-08 上传
174 浏览量
2018-04-27 上传
162 浏览量
5星 · 资源好评率100%
160 浏览量
2021-09-10 上传
188 浏览量
115 浏览量
190 浏览量
2024-10-27 上传
资源评论
py—小飞.
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 3b083教师工作量计算系统_springboot+vue.zip
- 3b081火车订票系统_springboot+vue.zip
- 3b082健身房管理系统_springboot+vue.zip
- 直流电机双闭环(电流环、转速环)调速系统:可以根据给定调节电机转速,同时也可以在负载或电网电压发生变动的时候保持电机转速不变 包含仿真和设计报告 设计报告有详细的主电路和控制电路等的设计过程和参数选
- linux常用命令大全.txt
- 37_工作交接清单(可作为离职、调岗人员使用).xlsx
- 【离职】解除合同备案表.xls
- 【转正】转正考核表(管理层).xls
- 42_外贸公司员工离职流程及工作交接程序.xls
- 48_员工离职交接单.xls
- 离职交接表.xlsx.xls
- 38_离职工作交接单.xls
- 离职手续办理表.xlsx
- 工作交接清单(可作为离职、调岗人员使用).xlsx
- 离职手续完备表模板excel表格.xlsx
- linux常用命令大全.txt
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功