-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from festoney8/dev
merge dev to main, update init process
- Loading branch information
Showing
5 changed files
with
27 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
# CHANGELOG | ||
|
||
## 2.0.3 | ||
|
||
- 修复:Firefox+ViolentMonkey合用时的规则载入bug | ||
- 优化:脚本初始化流程 | ||
|
||
## 2.0.2 | ||
|
||
- 新增:首页 隐藏 右下角-下载桌面端弹窗 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters