Skip to content

(待定)通用处理修正 - 引入 toEncodedUrl 避免 URL 错误解析问题 、修正 prettyUrl 不完整#1236

Draft
cyfung1031 wants to merge 3 commits intoscriptscat:release/v1.3from
cyfung1031:fix-link-decoding003
Draft

(待定)通用处理修正 - 引入 toEncodedUrl 避免 URL 错误解析问题 、修正 prettyUrl 不完整#1236
cyfung1031 wants to merge 3 commits intoscriptscat:release/v1.3from
cyfung1031:fix-link-decoding003

Conversation

@cyfung1031
Copy link
Collaborator

@cyfung1031 cyfung1031 commented Feb 12, 2026

修复安装链接解析失败(关联 #1235)并优化 prettyUrl

本 PR 重点解决从 GreasyFork、SleazyFork 及自定义来源获取的安装链接在不同编码场景下解析失败、乱码、403 或 Punycode 显示异常的问题,同时完善 URL 规范化与安装页体验。


一、核心问题

  • 部分平台错误混用 encodeURI / encodeURIComponent
  • &url= 参数被浏览器或扩展多层编码,导致解析错位
  • fetch 请求出现 403 Forbidden
  • Punycode 域名未正确显示
  • prettyUrl 实现不完整,异常 URL 处理不足

二、核心改动

1️⃣ DeclarativeNetRequest 重定向锚点机制(关键修复)

service_worker/script.ts 新增精确 redirect 规则:

  • 使用 regexSubstitution
  • 注入唯一锚点 <,\1,> 标记原始捕获 URL
  • 重定向至 ?url=<,\1,>

目的:

  • 明确区分 raw / encodeURI / encodeURIComponent 三种编码层级
  • 避免 &url= 参数被错误拆分或二次解码
  • 保证安装页可精确恢复真实脚本地址

2️⃣ 新增健壮 URL 工具(url-utils.ts)

新增 url-utils.ts + 完整 Vitest 覆盖测试。

toEncodedURL(input: string)

  • 基于 new URL() 做输入清洗与校验

  • host 强制 Punycode 编码(支持 CJK / emoji / IDN)

  • 分离编码 path / search / hash,避免双重编码

  • 修复常见异常:

    • 反斜杠
    • 连续斜杠
    • 非法 % 编码
    • 编码混乱 URL

prettyUrl(url, base?)

  • Punycode → 可读域名
  • 使用 decodeURI 安全解码路径
  • URLSearchParams 重建查询字符串
  • 保留 & / = 语义
  • graceful fallback 处理畸形 URL

示例:

http://xn--6qq79v.com/你好
→ http://你好.com/你好

✅ 测试覆盖

  • CJK 域名(点看.com → xn--3pxu8k.com)
  • emoji / RTL / 组合字符路径
  • 各类空格(U+0020 / U+00A0 / U+3000)
  • 双重编码防御
  • 非法 URL fallback
  • 真实 GreasyFork / ScriptCat / GitHub 链接样本

3️⃣ 安装页国际化与体验优化

优化:

  • 加载中 / 失败提示更清晰
  • 等待状态更直观

三、整体效果

  • 显著提升 GreasyFork 一键安装成功率
  • 兼容更多“非常规编码”安装链接
  • 正确支持 Punycode 与国际化域名
  • 安装页提示更友好
  • URL 解析更健壮、可测试、可维护
  • prettyUrl 行为更稳定一致

Close #1235

keywords: Download Progress, Fetch, Script Installation Links, Encoding, Punycode, RFC 3492, regexSubstitution, chrome.declarativeNetRequest.Rule

Testing


Case 1

Case 2




@cyfung1031 cyfung1031 linked an issue Feb 12, 2026 that may be closed by this pull request
@cyfung1031
Copy link
Collaborator Author

要調一下UI

@cyfung1031 cyfung1031 marked this pull request as draft February 12, 2026 14:23
@cyfung1031 cyfung1031 marked this pull request as ready for review February 12, 2026 23:58
@cyfung1031 cyfung1031 marked this pull request as draft February 13, 2026 01:29
@cyfung1031 cyfung1031 marked this pull request as ready for review February 13, 2026 02:18
@cyfung1031 cyfung1031 marked this pull request as draft February 13, 2026 02:41
@cyfung1031 cyfung1031 marked this pull request as ready for review February 13, 2026 03:40
@cyfung1031 cyfung1031 marked this pull request as draft February 13, 2026 03:53
@cyfung1031 cyfung1031 marked this pull request as ready for review February 13, 2026 04:09
@cyfung1031 cyfung1031 added the hotfix 需要尽快更新到扩展商店 label Feb 13, 2026
@cyfung1031 cyfung1031 changed the title [v1.3] 修正安装链结没有正确解析问题 [v1.3] 修正安装链结没有正确解析问题 + 优化 prettyUrl Feb 13, 2026
@CodFrm
Copy link
Member

CodFrm commented Feb 13, 2026

fetch时应该用原本的url参数去处理,可读性的url简单处理即可

这又一大堆的代码,还专门特殊处理,看得头疼

@CodFrm CodFrm closed this Feb 13, 2026
@cyfung1031
Copy link
Collaborator Author

fetch时应该用原本的url参数去处理,可读性的url简单处理即可

这又一大堆的代码,还专门特殊处理,看得头疼

...
不是 fetch 问题。。。
为什么 close了呢
哪你自己修 bug 吧

一大堆代码是之前写的prettyUrl 不太好
prettyUrl 不改的话没问题


我注释都写得很清楚
用 regexSubsitute 的话,有解码错误的问题
不能用 new URL / new URLSearchParams 去搞

你自己搞。我不理了

@CodFrm
Copy link
Member

CodFrm commented Feb 13, 2026

看得头大,每次修bug,结果变成大改动了,代码也看不下去

@CodFrm CodFrm reopened this Feb 13, 2026
@cyfung1031
Copy link
Collaborator Author

我把 prettyUrl 拆出來

@cyfung1031 cyfung1031 marked this pull request as draft February 13, 2026 13:55
(cherry picked from commit ec65ce1)
try {
// 截取 "xn--" 前缀后进行解码
const punycodePart = s.slice(4);
return punycodeDecode(punycodePart);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

虽然我已经不会在这 BUG FIX PR 搞prettyUrl.
但原本 72 行就搞定的东西,你偏要引用一个 压缩了也有 3KB 的老东西

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那这点可以再考虑一下,只是我更偏向使用现成的工具库

@CodFrm
Copy link
Member

CodFrm commented Feb 13, 2026

Case 1
https://update.greasyfork.org/scripts/550295/100%解锁CSDN文库vip文章阅读限制.user.js

本来就是一个不正确的url(使用curl判断),没必要还做特殊处理

@CodFrm
Copy link
Member

CodFrm commented Feb 13, 2026

@CodFrm CodFrm force-pushed the fix-link-decoding003 branch from 2302906 to fc7b701 Compare February 13, 2026 14:35
@cyfung1031 cyfung1031 changed the title [v1.3] 修正安装链结没有正确解析问题 + 优化 prettyUrl [v1.3] (待定)通用处理修正 - 引入 toEncodedUrl 避免 URL 错误解析问题 、修正 prettyUrl 不完整 Feb 13, 2026
@cyfung1031
Copy link
Collaborator Author

我开了一个 #1238
我看到你有在这提交 commit. 我就不动这个了
你喜欢吧

@cyfung1031 cyfung1031 changed the title [v1.3] (待定)通用处理修正 - 引入 toEncodedUrl 避免 URL 错误解析问题 、修正 prettyUrl 不完整 (待定)通用处理修正 - 引入 toEncodedUrl 避免 URL 错误解析问题 、修正 prettyUrl 不完整 Feb 13, 2026
@CodFrm CodFrm mentioned this pull request Feb 13, 2026
@cyfung1031 cyfung1031 added enhancement New feature or request and removed hotfix 需要尽快更新到扩展商店 labels Feb 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] GreasyFork Install - Fetch error 403

2 participants