> For the complete documentation index, see [llms.txt](https://developer.eagle.cool/plugin-api/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.eagle.cool/plugin-api/zh-tw/get-started/plugin-types/service.md).

# 背景服務

背景服務插件與[視窗插件](/plugin-api/zh-tw/get-started/plugin-types/window.md)開發方式類似，主要區別在於程式碼的執行時機。背景服務插件會在軟體啓動時自動啓動，而視窗插件只會在使用者點擊時執行。要新增一個背景服務插件，只需要在 `manifest.json` 中的 `main` 欄位新增 `"serviceMode": true`，如下所示：

```json
{
    "main":
    {
        "serviceMode": true,    // 主要差異
        "url": "index.html",
        "width": 640,
        "height": 480
    }
}
```

背景服務插件也能彈出視窗，你可以在這個視窗顯示當前背景任務的進度、狀態，以便使用者可以清楚地瞭解插件的當前狀態。

最終程式碼如下：

{% tabs %}
{% tab title="manifest.json" %}

```json
{
    "id": "LBCZEHP8BBO94",
    "version": "1.0.0",
    "name": "Service Plugin",
    "logo": "/logo.png",
    "keywords": [],
    "main":
    {
        "serviceMode": true,
        "url": "index.html",
        "width": 640,
        "height": 480
    }
}
```

{% endtab %}

{% tab title="index.html" %}

```html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
    <script type="text/javascript" src="js/plugin.js"></script>
</head>

<body>
    <div>Background services can be without UI, but you can still display the status of background services here.</div>
</body>
</html>
```

{% endtab %}

{% tab title="plugin.js" %}

```javascript
console.log(`Plugins will be created automatically, no need for users to execute them.`);

eagle.onPluginCreate((plugin) => {
    console.log('eagle.onPluginCreate');
    console.log(plugin);
});

eagle.onPluginShow(() => {
    console.log('eagle.onPluginShow');
});

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

{% endtab %}
{% endtabs %}

{% hint style="info" %}
**完整範例程式碼：**

<https://github.com/eagle-app/eagle-plugin-examples/tree/main/Service>
{% endhint %}

{% hint style="info" %}
注：你可以查看這篇文章，[瞭解 `manifest.json`的所有設定方式](/plugin-api/zh-tw/tutorial/manifest.md)。
{% endhint %}

{% hint style="warning" %}
**注意：** 如果插件執行過程必須依賴相對的資源庫路徑，你可能需要透過[`onLibraryChanged(callback)`](/plugin-api/zh-tw/api/event.md#g3tny)，在資源庫切換時，做出對應的調整，避免程式執行過程發生錯誤。
{% endhint %}
