Skip to content

Commit

Permalink
Merge pull request #4 from PRO-2684/dev
Browse files Browse the repository at this point in the history
v0.3.2 from dev
  • Loading branch information
PRO-2684 authored May 15, 2024
2 parents 0bd3c0c + e2918bb commit 68a62a1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ This table shows supported parameters for each mode:
| `acts` ||||||||
| `regex` ||||||||
| `replace` ||||||||
| `ua` ||||||||
| ~~`ua`~~ ||||||||
| `headers` ||||||||
| `lambda` ||||||||
| `continue` ||||||||

Expand Down Expand Up @@ -281,10 +282,13 @@ If you'd like to learn more about the syntax of the "replacement string", please
| Param | Type | Default |
| --- | --- | --- |
| `ua` | `string` | `undefined` |
| ~~`ua`~~ | `string` | `undefined` |
| `headers` | `object` | `{}` |
| `continue` | `Boolean` | `true` |

Under Redirect mode, pURLfy will call constructor parameter `fetch` to get the redirected URL, by firing a `HEAD` request using `ua` to the matched URL and return the `Location` header or the updated `response.url`. If `continue` is not set to `false`, the new URL will be purified again.
Under Redirect mode, pURLfy will call constructor parameter `fetch` to get the redirected URL, by firing a `HEAD` request using `headers` as the headers to the matched URL and return the `Location` header or the updated `response.url`. If `continue` is not set to `false`, the new URL will be purified again.

Note: `ua` parameter will be deprecated in the future, and you should use `headers` to set the `User-Agent` header.

#### 🟠 Visit Mode `visit`

Expand All @@ -293,11 +297,14 @@ Under Redirect mode, pURLfy will call constructor parameter `fetch` to get the r
| Param | Type | Default |
| --- | --- | --- |
| `ua` | `string` | `undefined` |
| ~~`ua`~~ | `string` | `undefined` |
| `headers` | `object` | `{}` |
| `acts` | `string[]` | `["regex:<url_pattern>"]` |
| `continue` | `Boolean` | `true` |

Under Visit mode, pURLfy will visit the URL with `ua`, and if the URL has not beed redirected, it will call the [processors](#-processors) specified in `acts` in order (`<url_pattern>` is `https?:\/\/.(?:www\.)?[-a-zA-Z0-9@%._\+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:%_\+.~#?!&\/\/=]*)`). The initial input to `acts` is of type `string`, i.e. the text returned by visiting the URL. If the URL has been redirected, the redirected URL will be returned. If `continue` is not set to `false`, the new URL will be purified again.
Under Visit mode, pURLfy will visit the URL with `headers` as the headers, and if the URL has not beed redirected, it will call the [processors](#-processors) specified in `acts` in order (`<url_pattern>` is `https?:\/\/.(?:www\.)?[-a-zA-Z0-9@%._\+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:%_\+.~#?!&\/\/=]*)`). The initial input to `acts` is of type `string`, i.e. the text returned by visiting the URL. If the URL has been redirected, the redirected URL will be returned. If `continue` is not set to `false`, the new URL will be purified again.

Note: `ua` parameter will be deprecated in the future, and you should use `headers` to set the `User-Agent` header.

#### 🔵 Lambda Mode `lambda`

Expand Down
17 changes: 12 additions & 5 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ new Purlfy({
| `acts` ||||||||
| `regex` ||||||||
| `replace` ||||||||
| `ua` ||||||||
| ~~`ua`~~ ||||||||
| `headers` ||||||||
| `lambda` ||||||||
| `continue` ||||||||

Expand Down Expand Up @@ -282,10 +283,13 @@ new Purlfy({
| 参数 | 类型 | 默认值 |
| --- | --- | --- |
| `ua` | `string` | `undefined` |
| ~~`ua`~~ | `string` | `undefined` |
| `headers` | `object` | `{}` |
| `continue` | `Boolean` | `true` |

重定向模式下,pURLfy 会调用构造时的参数 `fetch` 使用 `ua` 发送 `HEAD` 请求并返回 `Location` 标头或更新的 `response.url` 作为重定向后的 URL。若 `continue` 未被设置为 `false`,则再次净化新的 URL。
重定向模式下,pURLfy 会调用构造时的参数 `fetch` 使用 `headers` 头发送 `HEAD` 请求并返回 `Location` 标头或更新的 `response.url` 作为重定向后的 URL。若 `continue` 未被设置为 `false`,则再次净化新的 URL。

注意:`ua` 参数将被废弃,现在请使用 `headers` 参数来设置 `User-Agent` 请求头。

#### 🟠 访问模式 `visit`

Expand All @@ -294,11 +298,14 @@ new Purlfy({
| 参数 | 类型 | 默认值 |
| --- | --- | --- |
| `ua` | `string` | `undefined` |
| ~~`ua`~~ | `string` | `undefined` |
| `headers` | `object` | `{}` |
| `acts` | `string[]` | `["regex:<url_pattern>"]` |
| `continue` | `Boolean` | `true` |

在访问模式下,pURLfy 会使用 `ua` 访问 URL,若网址未被重定向,则按序调用 `acts` 中指定的 [处理器](#-处理器) 来获取页面中的链接 (`<url_pattern>``https?:\/\/.(?:www\.)?[-a-zA-Z0-9@%._\+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:%_\+.~#?!&\/\/=]*)`)。`acts` 的首个输入为 `string`,即访问当前 URL 的返回文本。若网址已重定向,则返回重定向后网址。若 `continue` 未被设置为 `false`,则再次净化新的 URL。
在访问模式下,pURLfy 会使用 `headers` 头访问 URL,若网址未被重定向,则按序调用 `acts` 中指定的 [处理器](#-处理器) 来获取页面中的链接 (`<url_pattern>``https?:\/\/.(?:www\.)?[-a-zA-Z0-9@%._\+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:%_\+.~#?!&\/\/=]*)`)。`acts` 的首个输入为 `string`,即访问当前 URL 的返回文本。若网址已重定向,则返回重定向后网址。若 `continue` 未被设置为 `false`,则再次净化新的 URL。

注意:`ua` 参数将被废弃,现在请使用 `headers` 参数来设置 `User-Agent` 请求头。

#### 🔵 匿名函数模式 `lambda`

Expand Down
20 changes: 9 additions & 11 deletions purlfy.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Purlfy extends EventTarget {
// Static properties
static get version() {
return "0.3.1";
return "0.3.2";
};
static #AsyncFunction = async function () { }.constructor;
static #zeroStatistics = {
Expand Down Expand Up @@ -108,9 +108,9 @@ class Purlfy extends EventTarget {
case "regex":
return Array.isArray(rule.regex) && Array.isArray(rule.replace) && Purlfy.#udfOrType(rule.continue, "boolean") && rule.regex.length === rule.replace.length;
case "redirect":
return this.fetchEnabled && Purlfy.#udfOrType(rule.ua, "string") && Purlfy.#udfOrType(rule.continue, "boolean");
return this.fetchEnabled && Purlfy.#udfOrType(rule.ua, "string") && Purlfy.#udfOrType(rule.headers, "object") && Purlfy.#udfOrType(rule.continue, "boolean");
case "visit":
return this.fetchEnabled && Purlfy.#udfOrType(rule.ua, "string") && (rule.acts === undefined || Array.isArray(rule.acts)) && Purlfy.#udfOrType(rule.continue, "boolean");
return this.fetchEnabled && Purlfy.#udfOrType(rule.ua, "string") && Purlfy.#udfOrType(rule.headers, "object") && (rule.acts === undefined || Array.isArray(rule.acts)) && Purlfy.#udfOrType(rule.continue, "boolean");
case "lambda":
return this.lambdaEnabled && (typeof rule.lambda === "string" || rule.lambda instanceof Purlfy.#AsyncFunction) && Purlfy.#udfOrType(rule.continue, "boolean");
default:
Expand Down Expand Up @@ -256,12 +256,11 @@ class Purlfy extends EventTarget {
}
const options = {
method: "HEAD",
redirect: "manual"
redirect: "manual",
headers: rule.headers ?? {}
};
if (rule.ua) {
options.headers = {
"User-Agent": rule.ua
};
options.headers["User-Agent"] = rule.ua;
}
let dest = null;
try {
Expand Down Expand Up @@ -296,12 +295,11 @@ class Purlfy extends EventTarget {
}
const options = {
method: "GET",
redirect: "follow" // TODO: "manual"
redirect: "manual",
headers: rule.headers ?? {}
};
if (rule.ua) {
options.headers = {
"User-Agent": rule.ua
};
options.headers["User-Agent"] = rule.ua;
}
let r, html = null;
try {
Expand Down

0 comments on commit 68a62a1

Please sign in to comment.