Skip to content
Merged
8 changes: 8 additions & 0 deletions custom-nodes/js/javascript_examples.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ import { api } from "../../scripts/api.js";

## Detect an interrupted workflow

<Note>
**Deprecated:** The API hijacking pattern shown below is deprecated and subject to change at any point in the near future. Use the official [extension hooks](/custom-nodes/js/javascript_hooks) and API event listeners where available.
</Note>

A simple example of hijacking the api:

```Javascript
Expand All @@ -134,6 +138,10 @@ import { api } from "../../scripts/api.js";

## Catch clicks on your node

<Note>
**Deprecated:** The node method hijacking pattern shown below is deprecated and subject to change at any point in the near future. Use the official [extension hooks](/custom-nodes/js/javascript_hooks) where available.
</Note>

`node` has a mouseDown method you can hijack.
This time we're careful to pass on any return value.

Expand Down
7 changes: 7 additions & 0 deletions custom-nodes/js/javascript_hooks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ might modify yours!), in which case care should be taken to ensure interoperabil
And play nicely - isolate your changes wherever possible.</Tip>

A very common idiom in `beforeRegisterNodeDef` is to 'hijack' an existing method:

<Note>
**Deprecated:** The prototype hijacking pattern shown below is deprecated and subject to change at any point in the near future. For context menus, use the official [Context Menu API](/custom-nodes/js/context-menu-migration) instead. For other use cases, prefer using the official [extension hooks](/custom-nodes/js/javascript_hooks) where available.
</Note>

```Javascript
async beforeRegisterNodeDef(nodeType, nodeData, app) {
if (nodeType.comfyClass=="MyNodeClass") {
Expand All @@ -70,6 +75,8 @@ or even make calling it conditional.
When hijacking a method in this way, you will want to look at the core comfy code (breakpoints are your friend) to check
and conform with the method signature.

<Warning>This approach is fragile and may break with future ComfyUI updates. Use official APIs whenever possible.</Warning>

#### nodeCreated()

```Javascript
Expand Down
10 changes: 9 additions & 1 deletion custom-nodes/js/javascript_objects_and_hijacking.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ which can be found at `doc/index.html`.
The `app` object (always accessible by `import { app } from "../../scripts/app.js";`) represents the Comfy application running in the browser,
and contains a number of useful properties and functions, some of which are listed below.

<Note>
**Deprecated:** Hijacking/monkey-patching functions on `app` or prototypes is deprecated and subject to change at any point in the near future. Use the official [extension hooks](/custom-nodes/js/javascript_hooks) and [Context Menu API](/custom-nodes/js/context-menu-migration) instead.
</Note>

<Warning>Hijacking functions on `app` is not recommended, as Comfy is under constant development, and core behavior may change.</Warning>

### Properties
Expand Down Expand Up @@ -81,7 +85,11 @@ Group nodes, primitive nodes, notes, and redirect nodes have different propertie

A `ComfyNode` object represents a node in the current workflow. It has a number of important properties
that you may wish to make use of, a very large number of functions that you may wish to use, or hijack to
modify behavior.
modify behavior.

<Note>
**Deprecated:** Hijacking prototype methods on `ComfyNode` or `LGraphNode` is deprecated and subject to change at any point in the near future. Use the official [extension hooks](/custom-nodes/js/javascript_hooks) where available, such as `getNodeMenuItems` for context menus. See the [Context Menu Migration Guide](/custom-nodes/js/context-menu-migration) for examples.
</Note>

To get a more complete sense of the node object, you may find it helpful to insert the following
code into your extension and place a breakpoint on the `console.log` command. When you then create a new node
Expand Down
8 changes: 8 additions & 0 deletions zh-CN/custom-nodes/js/javascript_examples.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ import { api } from "../../scripts/api.js";

## 检测工作流被中断

<Note>
**已弃用:** 下面展示的 API 劫持模式已被弃用,可能在不久的将来随时更改。请尽可能使用官方的 [扩展钩子](/zh-CN/custom-nodes/js/javascript_hooks) 和 API 事件监听器。
</Note>

这是一个劫持 api 的简单例子:

```Javascript
Expand All @@ -131,6 +135,10 @@ import { api } from "../../scripts/api.js";

## 捕获节点点击

<Note>
**已弃用:** 下面展示的节点方法劫持模式已被弃用,可能在不久的将来随时更改。请尽可能使用官方的 [扩展钩子](/zh-CN/custom-nodes/js/javascript_hooks)。
</Note>

`node` 有一个 mouseDown 方法可以被劫持。
这次我们注意传递任何返回值。

Expand Down
7 changes: 7 additions & 0 deletions zh-CN/custom-nodes/js/javascript_hooks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ async beforeRegisterNodeDef(nodeType, nodeData, app)
<Tip>由于其他扩展也可能会修改节点,建议尽量减少假设,并尽量隔离你的更改,友好共存。</Tip>

在 `beforeRegisterNodeDef` 中非常常见的做法是"劫持"已有方法:

<Note>
**已弃用:** 下面展示的原型劫持模式已被弃用,可能在不久的将来随时更改。对于右键菜单,请改用官方的 [右键菜单 API](/zh-CN/custom-nodes/js/context-menu-migration)。对于其他用例,请尽可能使用官方的 [扩展钩子](/zh-CN/custom-nodes/js/javascript_hooks)。
</Note>

```Javascript
async beforeRegisterNodeDef(nodeType, nodeData, app) {
if (nodeType.comfyClass=="MyNodeClass") {
Expand All @@ -51,6 +56,8 @@ async beforeRegisterNodeDef(nodeType, nodeData, app) {

以这种方式劫持方法时,建议查看核心 comfy 代码(断点调试很有用),以确保方法签名一致。

<Warning>这种方法比较脆弱,可能会在未来的 ComfyUI 更新中失效。请尽可能使用官方 API。</Warning>

#### nodeCreated()

```Javascript
Expand Down
8 changes: 8 additions & 0 deletions zh-CN/custom-nodes/js/javascript_objects_and_hijacking.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Comfy 的许多功能都由 LiteGraph 提供,因此如果你要开发更复杂

`app` 对象(始终可通过 `import { app } from "../../scripts/app.js";` 获取)代表在浏览器中运行的 Comfy 应用,包含许多有用的属性和函数,部分如下所示。

<Note>
**已弃用:** 劫持/猴子补丁 `app` 上的函数或原型方法已被弃用,可能在不久的将来随时更改。请改用官方的 [扩展钩子](/zh-CN/custom-nodes/js/javascript_hooks) 和 [右键菜单 API](/zh-CN/custom-nodes/js/context-menu-migration)。
</Note>

<Warning>不建议劫持 `app` 上的函数,因为 Comfy 正在持续开发,核心行为可能会变化。</Warning>

### 属性
Expand Down Expand Up @@ -74,6 +78,10 @@ ComfyNode_object_for_my_node.inputs.forEach(input => {

`ComfyNode` 对象代表当前工作流中的一个节点。它有许多重要属性和大量可用或可劫持的函数,用于修改行为。

<Note>
**已弃用:** 劫持 `ComfyNode` 或 `LGraphNode` 上的原型方法已被弃用,可能在不久的将来随时更改。请尽可能使用官方的 [扩展钩子](/zh-CN/custom-nodes/js/javascript_hooks),例如使用 `getNodeMenuItems` 处理右键菜单。参见 [右键菜单迁移指南](/zh-CN/custom-nodes/js/context-menu-migration) 获取示例。
</Note>

为了更全面地了解节点对象,你可以在扩展中插入如下代码,并在 `console.log` 处打断点。创建新节点时即可用调试器查看节点。

```Javascript
Expand Down