Skip to content

Commit

Permalink
Merge pull request #5 from festoney8/dev
Browse files Browse the repository at this point in the history
merge dev to main, update init process
  • Loading branch information
festoney8 authored Dec 28, 2023
2 parents 3d61736 + 60179cf commit 23095ea
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 33 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 2.0.3

- 修复:Firefox+ViolentMonkey合用时的规则载入bug
- 优化:脚本初始化流程

## 2.0.2

- 新增:首页 隐藏 右下角-下载桌面端弹窗
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
| Firefox | [链接](https://addons.mozilla.org/firefox/addon/tampermonkey/) | [链接](https://addons.mozilla.org/firefox/addon/violentmonkey/) |
| 测试 | **已测试,推荐** | 支持,未完全测试 |

- [Greasemonkey](https://www.greasespot.net/)[ScriptCat](https://docs.scriptcat.org/) 未进行测试

## 与其他 bilibili 插件的兼容性

### [Bilibili-Evolved](https://github.com/the1812/Bilibili-Evolved) 的兼容性
Expand Down Expand Up @@ -169,3 +171,12 @@
### 导入数据

![](images/how-to-import.png)

## Ref

- [vite-plugin-monkey](https://github.com/lisonge/vite-plugin-monkey)

## Contribution

- main branch 用于发布测试版和稳定版
- dev branch 用于开发
41 changes: 9 additions & 32 deletions src/init.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,32 @@
import { log, debug } from './utils/logger'
import { log } from './utils/logger'

/**
* 初始化项目
* 等待firefox的document.head出现, 用于插入节点, 不对chrome系浏览器进行head处理
* 等待firefox的document.head出现+渲染, 用于插入节点
*/
export const init = async () => {
// firefox可捕捉到空head, 让firefox等待head出现+等待head渲染
// chrome系head永远非空, head渲染完成后仍会出现规则丢失情况, 故此时不处理, 由监听load事件的补丁解决
if (navigator.userAgent.toLowerCase().includes('firefox') && document.head === null) {
await waitForHead()
debug('firefox waitForHead complete')
// 此时head非空, 可输出innerHTML, 但存在尚未渲染的可能, 仍有概率插入失败, 故观测head的child出现
await waitForHeadBuild()
debug('waitForHeadBuild complete')
}
// firefox可捕捉到空head, 让firefox等待head渲染出第一波childNode
// chrome系head永远非空, head渲染完成后仍会出现规则丢失情况, 在此观测纯心理安慰, 最后由监听load事件的补丁解决
await waitForHeadBuild()
log('wait for head complete')
}

/**
* 观测head出现
* run-at document-start下, 向head内插入style时小概率报错 TypeError: document.head is null
* 导致规则载入不全, chrome和firefox均复现,chrome下捕捉不到error, firefox下可捕捉
* 出现概率低, 多见于首次开启某个页面(猜测是无缓存状态)
* @see https://github.com/greasemonkey/greasemonkey/issues/2515
* @returns Promise<void>
*/
const waitForHead = () => {
return new Promise<void>((resolve) => {
const observer = new MutationObserver(() => {
// html元素下出现的第一批childList必定包含head
observer.disconnect()
resolve()
})
observer.observe(document.documentElement, { childList: true })
})
}

/**
* 观测head内子元素, 出现子元素标志着head已构建完成
* 若只观测head出现,head可能为未渲染状态,向head中插入节点仍可能报错,故观测head渲染完成
* 耗时<50ms
* @returns Promise<void>
*/
const waitForHeadBuild = () => {
return new Promise<void>((resolve) => {
const observer = new MutationObserver((mutationsList) => {
for (const mutation of mutationsList) {
if (mutation.type === 'childList' && mutation.target === document.head) {
if (mutation.target === document.head) {
observer.disconnect()
resolve()
}
}
})
observer.observe(document.head, { childList: true })
observer.observe(document, { childList: true, subtree: true })
})
}
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const main = async () => {
try {
await init()
} catch (err) {
error(err)
error('init error, try continue')
}

Expand Down
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default defineConfig({
userscript: {
name: 'bilibili 页面净化大师',
namespace: 'http://tampermonkey.net/',
version: '2.0.2',
version: '2.0.3',
description: '净化 B站/哔哩哔哩 页面内各种元素,去广告,BV号转AV号,提供300+项功能,定制自己的B站页面',
author: 'festoney8',
homepage: 'https://github.com/festoney8/bilibili-cleaner',
Expand Down

0 comments on commit 23095ea

Please sign in to comment.