# バックグラウンドサービス

バックグラウンドサービスプラグインの開発方法は[ウィンドウプラグイン](/plugin-api/ja-jp/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>バックグラウンドサービスはUIを持たなくても構いませんが、バックグラウンドサービスの状態をここに表示することができます。</div>
</body>
</html>
```

{% endtab %}

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

```javascript
console.log(`プラグインは自動的に作成されるので、ユーザーが実行する必要はありません。`);

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`のすべての設定方法については、この記事を参照してください。[「manifest.json」の設定方法を詳しく知る](/plugin-api/ja-jp/tutorial/manifest.md)。
{% endhint %}

{% hint style="warning" %}
\*\*注意：\*\*プラグインの実行プロセスで相対的なリソースライブラリパスに依存しなければならない場合、[`onLibraryChanged(callback)`](/plugin-api/ja-jp/api/event.md#g3tny)を介してライブラリが切り替わるたびに適切な調整を行い、プログラムの実行過程でエラーが発生しないようにする必要があります。
{% endhint %}


---

# 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/ja-jp/get-started/plugin-types/service.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.
