# event（事件）

## onPluginCreate(callback) <a href="#gylpl" id="gylpl"></a>

插件視窗建立時，Eagle 會主動呼叫這個方法，你可以使用此方法初始化插件需要的模組。

* `callback` Function
  * `plugin` Object - 插件屬性
    * `manifest` Object - 插件 manifest.json 完整設定。
    * `path` String - 插件所在路徑

```javascript
eagle.onPluginCreate((plugin) => {
    console.log(plugin.manifest.name);
    console.log(plugin.manifest.version);
    console.log(plugin.manifest.logo);
    console.log(plugin.path);
});
```

{% hint style="info" %}
提示：如果插件不需要 manifest 資訊就可以執行，那麼你也可以使用 `window.onload` 來進行開發。
{% endhint %}

## onPluginRun(callback) <a href="#gylpl" id="gylpl"></a>

當使用者點擊插件面板的插件時，Eagle 會主動呼叫這個方法。

* `callback` Function

```javascript
eagle.onPluginRun(() => {
    console.log('eagle.onPluginRun');
});
```

## onPluginBeforeExit(callback) <a href="#z1a5y" id="z1a5y"></a>

插件視窗關閉前 Eagle 會主動呼叫這個方法。

* `callback` Function

```javascript
eagle.onPluginBeforeExit(() => {
    console.log("插件將退出");
});

// 阻止視窗關閉
window.onbeforeunload = (event) => {
    return event.returnValue = false;
};
```

{% hint style="info" %}
提示：如果你想要阻止視窗被關閉，你可以註冊 `window.onbeforeunload`方法避免視窗被關閉。
{% endhint %}

## onPluginShow(callback) <a href="#w2vxi" id="w2vxi"></a>

插件視窗顯示時，Eagle 會主動呼叫這個方法。

* `callback` Function

```javascript
eagle.onPluginShow(() => {
    console.log("插件視窗顯示");
});
```

## onPluginHide(callback) <a href="#zgvst" id="zgvst"></a>

插件視窗隱藏時，Eagle 會主動呼叫這個方法。

* `callback` Function

```javascript
eagle.onPluginHide(() => {
    console.log("插件視窗隱藏");
});
```

## onLibraryChanged(callback) <a href="#g3tny" id="g3tny"></a>

當使用者切換資源庫時，Eagle 會主動呼叫這個方法。

* `callback` Function
  * `libraryPath` String - 當前資源庫路徑。

```javascript
eagle.onLibraryChanged((libraryPath) => {
    console.log(`偵測到資源庫切換，新的資源庫路徑: ${libraryPath}`);
});
```

{% hint style="info" %}
提示：如果你需要取得更完整的資源庫資訊，你可以使用 `eagle.library.info()` 方法取得。
{% endhint %}

{% hint style="warning" %}
**注意：** 如果插件執行過程必須依賴相對的資源庫路徑，你可能需要透過註冊此方法，在資源庫切換時，做出對應的調整，避免程式執行過程發生錯誤。
{% endhint %}

## onThemeChanged(callback) <a href="#xlf6z" id="xlf6z"></a>

Eagle 主程式主題配色改變時，Eagle 會主動呼叫這個方法，如果插件支援多種配色主題，你可以使用此方法做出對應的 UI 調整。

* `callback` Function
  * `theme` String - 當前主題配色的名稱，如 `Auto`、`LIGHT`、`LIGHTGRAY`、`GRAY`、`DARK`、`BLUE`、`PURPLE`。

```javascript
eagle.onThemeChanged((theme) => {
    console.log(`配色主題以改為: ${theme}`);
});
```

### &#x20;<a href="#nptwx" id="nptwx"></a>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developer.eagle.cool/plugin-api/zh-tw/api/event.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
