Itsnotlupus' React Tools

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

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.greasyfork.org/scripts/473998/1246974/Itsnotlupus%27%20React%20Tools.js

作者
itsnotlupus
版本
1.1
建立日期
2023-08-27
更新日期
2023-09-07
授權條款
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.)