-
Notifications
You must be signed in to change notification settings - Fork 324
[兼容TM] 以 Navigation API 实现 TM 的 window.onurlchange #1315
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
Open
cyfung1031
wants to merge
8
commits into
release/v1.4
Choose a base branch
from
develop/onurlchange
base: release/v1.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+86
−4
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a35d8b8
以 Navigation API 实现 TM 的 window.onurlchange
cyfung1031 8070269
整理档案架构
cyfung1031 7a5ba76
增加 seq判断 避免重复触发
cyfung1031 6359474
Update src/app/service/content/gm_api/navigation_handle.ts
CodFrm 77a84d9
Merge branch 'release/v1.4' into develop/onurlchange
cyfung1031 c36ee37
修改代码
cyfung1031 cc72cc4
Update navigation_handle.ts
cyfung1031 e26af0a
修改代码
cyfung1031 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| export class UrlChangeEvent extends Event { | ||
| readonly url: string; | ||
| constructor(type: string, url: string) { | ||
| super(type); | ||
| this.url = url; | ||
| } | ||
| } | ||
|
|
||
| let attached = false; | ||
|
|
||
| const getPropGetter = (obj: any, key: string) => { | ||
| // 避免直接 obj[key] 读取。或会被 hack | ||
| let t = obj; | ||
| let pd: PropertyDescriptor | undefined; | ||
| while (t) { | ||
| pd = Object.getOwnPropertyDescriptor(t, key); | ||
| if (pd) break; | ||
| t = Object.getPrototypeOf(t); | ||
| } | ||
| if (pd) { | ||
| return pd?.get?.bind(obj); | ||
| } | ||
| }; | ||
|
|
||
| // Chrome 102+, Firefox 147+ | ||
| // https://developer.chrome.com/docs/web-platform/navigation-api | ||
| // https://developer.mozilla.org/en-US/docs/Web/API/Navigation_API#browser_compatibility | ||
| export const attachNavigateHandler = (win: Window & { navigation: EventTarget }) => { | ||
| if (attached) return; | ||
| attached = true; | ||
| // 以 location.href 判断避免 replaceState/pushState 重复执行重复触发 | ||
| const loc = win.location; | ||
| const getUrl = getPropGetter(loc, "href"); | ||
| const dispatch = win.dispatchEvent.bind(win); | ||
| let lastUrl = getUrl?.(); | ||
cyfung1031 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let callSeq = 0; | ||
| const handler = async (ev: Event): Promise<void> => { | ||
| callSeq = callSeq > 512 ? 1 : callSeq + 1; | ||
| const seq = callSeq; | ||
| let newUrl = getUrl?.(); // 取得当前 location.href | ||
| const destUrl = (ev as any).destination?.url; | ||
| if (destUrl !== newUrl && newUrl === lastUrl) { | ||
| // 某些情况,location.href 未更新就触发了 | ||
| // 用 postMessage 推迟到下一个 macrotask 阶段 | ||
| await new Promise((resolve) => { | ||
| self.addEventListener("message", resolve, { once: true }); | ||
| self.postMessage({ [`${Math.random()}`]: {} }, "*"); // 传一个 dummy message | ||
| }); | ||
| if (seq !== callSeq) return; // 等待时,或许已经触发了其他 navigate | ||
| newUrl = getUrl?.(); // 再次取得当前 location.href | ||
| } | ||
| if (newUrl === lastUrl) return; | ||
| lastUrl = newUrl; | ||
| const urlChangeEv = new UrlChangeEvent("urlchange", (destUrl || newUrl) as string); | ||
| dispatch(urlChangeEv); | ||
| }; | ||
| win.navigation?.addEventListener("navigate", handler, false); | ||
| }; | ||
cyfung1031 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.