-
-
Notifications
You must be signed in to change notification settings - Fork 87
Optimize settings injection with MutationObserver #225
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
base: main
Are you sure you want to change the base?
Changes from all commits
3be14f6
1f718f9
ec189a8
f629367
596b4f5
0df9571
7efead6
3db7b1c
105b37a
2b3e58f
35a3225
5b0f853
f159978
7d52416
a4b7af8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,32 +26,55 @@ const themesync = async () => { | |
| }; | ||
|
|
||
| // Settings injection | ||
| setInterval(() => { | ||
| const versionInfo = document.querySelector('[class*="sidebar"] [class*="compactInfo"]'); | ||
| if (!versionInfo || document.getElementById('openasar-ver')) return; | ||
|
|
||
| const oaVersionInfo = versionInfo.cloneNode(true); | ||
| const oaVersion = oaVersionInfo.children[0]; | ||
| oaVersion.id = 'openasar-ver'; | ||
| oaVersion.textContent = 'OpenAsar (<hash>)'; | ||
| oaVersion.onclick = () => DiscordNative.ipc.send('DISCORD_UPDATED_QUOTES', 'o'); | ||
|
|
||
| oaVersionInfo.textContent = ''; | ||
| oaVersionInfo.appendChild(oaVersion); | ||
| versionInfo.parentElement.parentElement.lastElementChild.insertAdjacentElement('beforebegin', oaVersionInfo); | ||
|
|
||
| if (document.getElementById('openasar-item')) return; | ||
| let advanced = document.querySelector('[data-list-item-id="settings-sidebar___advanced_sidebar_item"]'); | ||
| if (!advanced) advanced = document.querySelector('[class*="sidebar"] [class*="nav"] > [class*="section"]:nth-child(3) > :last-child'); | ||
| if (!advanced) advanced = [...document.querySelectorAll('[class*="item"]')].find(x => x.textContent === 'Advanced'); | ||
|
|
||
| const oaSetting = advanced.cloneNode(true); | ||
| oaSetting.querySelector('[class*="text"]').textContent = 'OpenAsar'; | ||
| oaSetting.id = 'openasar-item'; | ||
| oaSetting.onclick = oaVersion.onclick; | ||
|
|
||
| advanced.insertAdjacentElement('afterend', oaSetting); | ||
| }, 800); | ||
| const injectOpenAsar = () => { | ||
| if (document.getElementById('openasar-ver') && document.getElementById('openasar-item')) return; | ||
|
|
||
| const sidebar = document.querySelector('[data-list-id="settings-sidebar"]'); | ||
| if (!sidebar) return; | ||
|
|
||
| // Inject version info inside clickable div | ||
| if (!document.getElementById('openasar-ver')) { | ||
| const links = sidebar.querySelector('div[class*="links"]'); | ||
|
|
||
| if (links && links.parentElement) { | ||
| const container = links.parentElement; | ||
| const clickableDiv = container.firstElementChild; | ||
|
|
||
| if (clickableDiv) { | ||
| const oaVersion = document.createElement('div'); | ||
| oaVersion.id = 'openasar-ver'; | ||
| oaVersion.className = 'text-xxs/normal_cf4812'; | ||
| oaVersion.textContent = 'OpenAsar (<hash>)'; | ||
| oaVersion.onclick = () => window.open('https://openasar.dev', '_blank'); | ||
| oaVersion.style.color = 'var(--text-muted)'; | ||
|
|
||
| clickableDiv.appendChild(oaVersion); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Inject menu item after Advanced | ||
| if (!document.getElementById('openasar-item')) { | ||
| let advanced = sidebar.querySelector('[data-list-item-id*="advanced"]') | ||
| || sidebar.querySelector('[data-settings-sidebar-item="advanced_panel"]')?.querySelector('[class*="item"]') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having these be || instead of ifs is overcomplicated and serves no benefit? |
||
| || (() => { | ||
| const sections = sidebar.querySelectorAll('[class*="section"]'); | ||
| return sections[2]?.querySelectorAll('[class*="item"]')[sections[2]?.querySelectorAll('[class*="item"]').length - 1]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't duplicate querySelector calls, it could change between calls to cause a race condition and is also not great for performance |
||
| })(); | ||
|
|
||
| if (advanced) { | ||
| const item = advanced.cloneNode(true); | ||
| item.id = 'openasar-item'; | ||
| item.querySelector('[class*="text"]').textContent = 'OpenAsar'; | ||
| item.onclick = () => DiscordNative.ipc.send('DISCORD_UPDATED_QUOTES', 'o'); | ||
| advanced.insertAdjacentElement('afterend', item); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const observer = new MutationObserver(() => injectOpenAsar()); | ||
| observer.observe(document.body, { childList: true, subtree: true }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am pretty sure this will be slower than the interval because it will run on every body change which is much more frequent than 800ms |
||
| setTimeout(injectOpenAsar, 500); | ||
|
|
||
| const injCSS = x => { | ||
| const el = document.createElement('style'); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hard-coded class names break easily which is why we need to clone existing elements.