Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

当 userscript 中仅存在 tweak_proto_view 回调时,debugger 无法正常使用 #298

Closed
GalaxySnail opened this issue Feb 6, 2025 · 4 comments

Comments

@GalaxySnail
Copy link

GalaxySnail commented Feb 6, 2025

是否可以稳定复现

复现步骤

使用如下用户脚本,没有任何额外内容:

tweak_proto_view(view => {
    console.log("!!!", view)
    debugger
})

然后在任意一个B站视频页打开浏览器控制台,并刷新。

预期行为

debugger 能正常暂停运行

实际行为

debugger 会跳转到 sources 页面进入调试,但不到1秒后该页面会消失。此时 console.log 打印的对象也无法点击展开,阅读代码后推测可能是此时执行用户脚本的 web worker 已被销毁。

禁用 pakku 后 bug 是否存在

不存在

是否与其他插件有关

无关

B 站播放器版本号

4.9.20-0773c501

pakku 调试信息

如果 pakku 完全无法工作,请完整地复制蓝框里的所有文字,并在反馈 bug 时附上。

[Debug Info]

Version: 2024.12.2

Channel: chrome

User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36

Incognito: false

Has Session Storage: true

localStorage: {}

Views:

  • chrome-extension://jklfcpboamajpiikgkbjcnnnnooefbhh/page/userscript_editor.html

  • chrome-extension://jklfcpboamajpiikgkbjcnnnnooefbhh/page/troubleshooting.html

Permissions: {"origins":["*://*.bilibili.com/*","https://www.bilibili.com/robots.txt?pakku_sandbox"],"permissions":["contextMenus","notifications","storage","scripting","declarativeNetRequestWithHostAccess"]}

Content Scripts: [{"allFrames":true,"css":["generated/injected.css"],"excludeMatches":["https://message.bilibili.com/*","https://www.bilibili.com/robots.txt?pakku_sandbox"],"id":"pakku-ajax","js":["generated/xhr_hook.js"],"matchOriginAsFallback":false,"matches":["*://*.bilibili.com/*"],"persistAcrossSessions":true,"runAt":"document_start","world":"MAIN"}]

State: {"GLOBAL_SWITCH":true,"STATS_1484333765":{"combined_cosine_distance":0,"combined_edit_distance":0,"combined_identical":0,"combined_pinyin_distance":0,"deleted_blacklist":0,"deleted_blacklist_each":{},"deleted_dispval":0,"download_time_ms":17,"ignored_script":0,"ignored_type":0,"ignored_whitelist":0,"modified_enlarge":0,"modified_scroll":0,"modified_shrink":0,"num_max_combo":1,"num_max_dispval":0,"num_onscreen_danmu":297,"num_taolu_matched":0,"num_total_danmu":297,"num_userscript":1,"parse_time_ms":123,"type":"done","userscript_time_ms":0},"STATS_1484333913":{"badge":"↓","message":"正在下载弹幕(0/0/0)","type":"message"},"_INITIALIZED":true}

Tabs:

由于打开的标签页过多,已编辑,与问题无关

DNR Rules: []

Config: {"_LAST_UPDATE_TIME":11738832702083,"_CONFIG_VER":4,"ADVANCED_USER":true,"THRESHOLD":-1,"MAX_DIST":3,"MAX_COSINE":85,"TRIM_PINYIN":true,"TRIM_ENDING":true,"TRIM_SPACE":true,"TRIM_WIDTH":true,"FORCELIST":[],"WHITELIST":[],"CROSS_MODE":false,"PROC_TYPE7":false,"PROC_TYPE4":true,"PROC_POOL1":false,"DANMU_MARK":"suffix","MARK_THRESHOLD":1,"DANMU_SUBSCRIPT":true,"ENLARGE":true,"SHRINK_THRESHOLD":0,"DROP_THRESHOLD":0,"MODE_ELEVATION":true,"REPRESENTATIVE_PERCENT":0,"TOOLTIP":true,"TOOLTIP_KEYBINDING":true,"AUTO_DISABLE_DANMU":false,"AUTO_DANMU_LIST":false,"FLUCTLIGHT":true,"BREAK_UPDATE":false,"TAKEOVER_AIJUDGE":true,"SCROLL_THRESHOLD":1200,"USERSCRIPT":"tweak_proto_view(view => {\n console.log(\"!!!\", view)\n debugger\n})","POPUP_BADGE":"percent","COMBINE_THREADS":1,"READ_PLAYER_BLACKLIST":true}

Wasm Validate: `true (60625 bytes)

Worker is Simulated: false

Exception: error log is working ("Error: error log is working\n at chrome-extension://jklfcpboamajpiikgkbjcnnnnooefbhh/generated/troubleshooting.js:1:9323")

Exception in Promise: async error log is working ("Error: async error log is working\n at chrome-extension://jklfcpboamajpiikgkbjcnnnnooefbhh/generated/troubleshooting.js:1:9280\n at chrome-extension://jklfcpboamajpiikgkbjcnnnnooefbhh/generated/troubleshooting.js:1:9320")

补充说明

如果在脚本中增加任意一个空的 tweak_before_pakku 或 tweak_after_pakku 都可以让 debugger 能正确暂停执行:

tweak_proto_view(view => {
    console.log("!!!", view)
    debugger
})

tweak_before_pakku(view => {})

此时观察 pakku 的日志能看到 pakku 应该是在阻塞等待 web worker 返回消息。


另外似乎用户脚本执行完成后,web worker 会被 terminate 掉,从而导致使用 console.log 打印在控制台的对象都无法点击展开,这个体验很不好(尤其是 debugger 又无效,我折腾了好几小时)。我目前只发现了三种绕过的方式:

  1. 在用户脚本中引发一个异常(例如 null["raise an error"]),可以让打印出的对象可以展开
  2. 如上所述,添加一个 tweak_before_pakku 或 tweak_after_pakku 回调,并使用 debugger 暂停执行
  3. 在用户脚本中覆盖 onmessage 函数,阻止 web worker 调用 postMessage 返回消息,可以让 worker 不被终止从而可以点击展开打印的对象

但感觉这样也太麻烦了,关键是这都会阻止 pakku 正常运行。和真正的油猴脚本相比体验差太多了,不知道有没有什么办法改进。


以及我还有一个疑问,这三种不同的回调目前运行的顺序有保证吗?通过覆盖 onmessage 的尝试,我发现似乎目前的顺序是 tweak_proto_view、tweak_before_pakku、tweak_after_pakku,一个返回后才会执行下一个。这确实是我在 #178 (comment) 中想要的行为,但不知道这个顺序是否有保证,或者能否一定程度上自定义它们运行的先后顺序?我看到昨天最新的提交里支持在回调中运行 async 函数,不太清楚对此是否会有影响。

@xmcp
Copy link
Owner

xmcp commented Feb 6, 2025

2025.2.1版本改成异步执行后解决了此问题,现在用户脚本的worker会在onmessage全都返回后再销毁。

@xmcp xmcp closed this as completed Feb 6, 2025
@GalaxySnail
Copy link
Author

这是否意味着新版改为异步之后,tweak_proto_view 与 tweak_{before,after}_pakku 将会是并发执行的,没有确切的先后顺序?

@xmcp
Copy link
Owner

xmcp commented Feb 6, 2025

本来也没有确切的先后顺序。你观察到的现象只是在一般网速和处理速度下的常见情况,对调用顺序没有保证。如果脚本需要保证顺序就去await,详见 Release Note 里的例子。

@GalaxySnail
Copy link
Author

刚看到了 release note,我明白了,非常感谢!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants