indie-wiki-buddy icon indicating copy to clipboard operation
indie-wiki-buddy copied to clipboard

Perf: Rewrite content scripts to run at the start of document/page loading

Open Razboy20 opened this issue 6 months ago • 2 comments

In using Indie Wiki Buddy, I noticed that it only reorganizes/filters search results after the document has loaded (i.e. run_at = document_end), and I thought it might be nice to have it run at the start, rather than the end.

This PR rewrites the content scripts to follow with the paradigm of MutationObservers, and 'streaming' new anchor elements that have been added to the page to be processed and filtered.

In doing so, this PR also cleans up some of the main content script logic, so hopefully although a lot of code was changed, the new structure of the code should make it easier for newcomers to understand what it's doing, and I hope also easier to review 😅.

Features/fixes that this PR provides:

  • Immediate filtering of search results
  • (Reordering) If the first result is a fandom link, swaps it with (if one exists) a lower down indie wiki
  • Reordering now works on all search engines! The only issue I've noticed is that Brave hydrates it's DOM, so the descriptions become a little wonky and are getting reset)
  • Only ever execute on changed elements
  • Search engines (such as bing) which do not reload on search changes still work, and the extension is able to detect when a new search is made and re-initialize it's internal url cache. This should also make the extension more stable in the long-run.

Current issues:

  • Ecosia still needs to run at document_end—there is some race condition where sometimes Ecosia's JS will crash based on DOM modifications made by the extension.
  • The aforementioned reordering not working perfectly on Brave. Not sure whether to just turn it off on Brave, or let it stay with a notice. I'll leave that up to you :)

Showcase:

Before: https://github.com/user-attachments/assets/583b05d8-afbd-4bbd-bfe5-69eec712b323

After: https://github.com/user-attachments/assets/0b8b56f2-3212-4a7b-9e64-354075d5a51f

Razboy20 avatar Aug 08 '24 02:08 Razboy20