Reddit Watcher
Consolidated MutationObserver handler for use by other Reddit userscripts. By itself doesn't do much, but other scripts can use hooks to avoid redundant observers watching the same thing. The main benefit being that overloading MutationObservers could slow down the browser. As such, if using multiple userscripts that use this function, a centralized handler will avoid unnecessary duplication.
Can use with:
Ensure in userscript dashboard that this is loaded before any dependent scripts by moving it above in the sorted list of installed userscripts.
Details
Creates variable named redditWatcher
in window
as simplest way of making itself available to other userscripts.
Within are three watcher objects for redditWatcher.body
, redditWatcher.listing
, and redditWatcher.feed
. Each handles changes to the document body (with subtree), the listing outer container (with subtree), and the post feed respectively. The latter two are written to handle deletions and recreations of the watched element, should it be removed and readded.
Pass function to onUpdate()
to run said function when MutationObserver detects an update on the watched element. Parameters passed will be the watched element and the array of MutationRecords.
Pass function to onChange()
to run said function when element itself is changed. Usually this is triggered if the element is removed and/or readded. For the feed watcher, this is also triggered when the feed itself is changed, as Reddit reuses the same container, but empties/repopulates the content when the page is 'changed'.