Skip to content

Commit e7dda1a

Browse files
committed
Refactor: use only mutation observer to parse link and set favicon
1 parent 9e623e5 commit e7dda1a

1 file changed

Lines changed: 26 additions & 28 deletions

File tree

index.ts

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -181,23 +181,35 @@ const main = async () => {
181181
extLinkEl.insertAdjacentElement('afterbegin', fav);
182182
};
183183

184-
// Favicons observer
185184
const extLinksObserverConfig = { childList: true, subtree: true };
186185
const extLinksObserver = new MutationObserver((mutationsList, observer) => {
187-
for (let i = 0; i < mutationsList.length; i++) {
188-
const addedNode = mutationsList[i].addedNodes[0];
189-
if (
190-
addedNode &&
191-
addedNode.childNodes.length &&
192-
addedNode instanceof Element
193-
) {
194-
const extLinkList =
195-
addedNode.querySelectorAll('.external-link');
196-
if (extLinkList.length) {
186+
for (const element of mutationsList) {
187+
const addedNode = element.addedNodes[0] as Element;
188+
if (addedNode?.childNodes.length) {
189+
if (addedNode.querySelector('.external-link')) {
197190
extLinksObserver.disconnect();
198-
for (let i = 0; i < extLinkList.length; i++) {
199-
setFavicon(extLinkList[i] as HTMLAnchorElement);
200-
}
191+
192+
(async (addedNode) => {
193+
const blockId = addedNode
194+
.querySelectorAll('.block-content')[0]
195+
.getAttribute('blockid');
196+
if (blockId) {
197+
try {
198+
await parseBlockForLink(blockId);
199+
setTimeout(() => {
200+
addedNode
201+
.querySelectorAll('.external-link')
202+
.forEach((extLink) => {
203+
const extLinkElement =
204+
extLink as HTMLAnchorElement;
205+
setFavicon(extLinkElement);
206+
});
207+
}, 250);
208+
} catch (error) {
209+
console.error('Error in async task:', error);
210+
}
211+
}
212+
})(addedNode);
201213

202214
extLinksObserver.observe(
203215
appContainer,
@@ -224,20 +236,6 @@ const main = async () => {
224236
extLinkList.forEach((extLink) => setFavicon(extLink));
225237
}
226238
);
227-
228-
const blockSet = new Set<string>();
229-
logseq.DB.onChanged(async (e) => {
230-
if (e.txMeta?.outlinerOp !== 'insert-blocks') {
231-
e.blocks[0]?.uuid && blockSet.add(e.blocks[0]?.uuid);
232-
doc.querySelectorAll('.external-link')?.forEach((extLink) =>
233-
setFavicon(extLink as HTMLAnchorElement)
234-
);
235-
return;
236-
}
237-
238-
await blockSet.forEach((uuid) => parseBlockForLink(uuid));
239-
blockSet.clear();
240-
});
241239
};
242240

243241
logseq.ready(main).catch(console.error);

0 commit comments

Comments
 (0)