Itsnotlupus' React Tools

Observe, inspect and perhaps modify a React tree through the React DevTools Hook

Този скрипт не може да бъде инсталиран директно. Това е библиотека за други скриптове и може да бъде използвана с мета-директива // @require https://update.greasyfork.org/scripts/473998/1246974/Itsnotlupus%27%20React%20Tools.js

Автор
itsnotlupus
Версия
1.1
Създаден
27.08.2023
Обновен
07.09.2023
Лиценз
MIT

A scrappy little library that plugs into React and tries to get useful bits user scripts can leverage.

Sample usage:

const stuff = new ReactTools();

stuff.observe(root => {
  console.log("A react tree was updated, and here's the fiber tree for it:", root);
});

// If the site you're targeting uses Redux and wasn't written by psychopaths, this will usually work.
// (The tree will need to get rendered first. You can use observe() to wait for it.)

const reduxStore = stuff.getProp("store");

findNodesWithProp() and updateNodeProps() are a bit trickier to use.
You'll want to install the React Dev Tools to see what props are actually available on each node.
See https://greasyfork.org/en/scripts/444804-crunchyroll-watchlist-userscript/code for an example use in tweakPlayer()
That crunchyroll script no longer works, but the code shows how to make this stuff work:
Mutating react props from outside of React is almost futile, since they get overwritten almost immediately.
But "almost" is the operative word there. You can effectively control React component props by:

  • having a React observer
  • that finds the node(s) you care about on every iteration with findNodesWithProp()
  • and reset the props you need with updateNodeProps()

Important Notes:

This library doesn't use the React DevTools Hook correctly.
There are almost certainly better way to use the hook APIs to do the things it does.
And yet it works (for me.)