News for hackers. Stuff that matters.
Re:Show HN: I made a CLI tool to create web extensions with ... by grimgrin on Tuesday April 30, 2024 @01:49PM (#40213830)
The best luck I’ve had with SPA apps and a goal of manipulating the DOM is by using mutation observers. Worth exploring if you haven’t yet

https://developer.mozilla.org/en-US/docs/Web/API/MutationObs...

You can hack on the behavior via userscripts, my preferred way to alter websites, though I write extensions too (greasemoney is good tho)

Re:Show HN: I made a CLI tool to create web extensions with ... by rsoto on Tuesday April 30, 2024 @01:56PM (#40213954)
IMO MutationObserver's API is a bit difficult to grasp. For simpler cases for getting a callback when an element is created, I use spect[1] or sentinel[2].

1: https://github.com/dy/spect 2: https://github.com/kubetail-org/sentineljs

Re:Show HN: I made a CLI tool to create web extensions with ... by sfink on Tuesday April 30, 2024 @06:16PM (#40217095)
You can wimp out and just use MutationObserver as a way to get a callback whenever anything changes, ignoring all of the mutation records. Then in the callback, look up all of the elements you care about. The API is pretty simple then. It may be less efficient, but usually when you're mucking about with existing web pages, you're not all that performance sensitive. (And MO batches up updates reasonably well, so it's not like you're running the callback once per change.)

Occasionally it might even be faster, since you're not iterating through the mutation records and testing for stuff you care about.

Re:Show HN: I made a CLI tool to create web extensions with ... by grimgrin on Tuesday April 30, 2024 @08:02PM (#40217984)
yeah that's exactly how I've done it -- observe the highest order container I care about (document.body lazy mode)