Greasy Fork is available in English.

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
创建于
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.)