Discussions » Development

Bug: Script does not run when site nav bar is used

§
Posted: 23.08.2025

My script here at GF (Redditors In Feed) runs on the Reddit home page, and works fine as long as you manually reload the page to get there. However, when the site's nav bar is used to get there, the script doesn't run at all.

This seems like it must have come up before with other scripts. Is this a common problem for scripts, and is there a likely solution?

§
Posted: 25.08.2025

I fixed this by using a MutationObserver on the title element, as in the following simplified example. It was necessary to put the entire script (excluding the boilerplate userscript template) in a function called runScript and change the @match to match with anything on the site's domain, rather than only the root path.

if (window.location.pathname == "/") {
  runScript();
}

// Run the script when using the nav bar (AJAX fake navigation) to get to the feed.
new MutationObserver(() => {
  if (document.title == "Website Homepage")) {
    runScript();
  }
}).observe(document.querySelector('title'), { childList: true });

Post reply

Sign in to post a reply.

长期地址
遇到问题?请前往 GitHub 提 Issues。