# clipboard（剪貼板）

{% hint style="info" %}
提示：推薦使用 Clipboard Viewer（[Win](https://freeclipboardviewer.com/) / [Mac](https://langui.net/clipboard-viewer/)） 工具進行開發除錯，讓開發過程更順利。
{% endhint %}

```javascript
await eagle.clipboard.writeText('Example string');

console.log(await eagle.clipboard.readText());
```

***

### 方法 <a href="#z1a5y" id="z1a5y"></a>

## clear() <a href="#tkp0d" id="tkp0d"></a>

清除剪貼板內容。

```javascript
eagle.clipboard.writeText('Example string');
eagle.clipboard.clear();
console.log(eagle.clipboard.readText());	// undefined
```

***

## has(format) <a href="#p4ult" id="p4ult"></a>

當前剪貼板內容是否包含指定的 format

* `format` string - 指定格式
* 返回 boolean - 是否包含指定格式

```javascript
console.log(eagle.clipboard.has('public/utf8-plain-text'));	// false

const buffer = Buffer.from('writeBuffer', 'utf8');
eagle.clipboard.writeBuffer('public/utf8-plain-text', buffer);

console.log(eagle.clipboard.has('public/utf8-plain-text'));	// true
```

***

## writeText(text) <a href="#eear5" id="eear5"></a>

將 `text` 作為純文字寫入剪貼板。

* `text` string - 欲寫入文字

```javascript
eagle.clipboard.writeText('Example string');
console.log(eagle.clipboard.readText());	// 'Example string'
```

***

## readText() <a href="#ytddd" id="ytddd"></a>

取得目前剪貼板的純文字內容。

* 返回 string

```javascript
console.log(await eagle.clipboard.readText());
```

***

## writeBuffer(format, buffer) <a href="#ol666" id="ol666"></a>

將 `buffer` 作為 `format` 類型寫入剪貼板。

* `format` string - 剪貼板格式
* `buffer` Buffer - 欲寫入內容之 Buffer 格式

```javascript
const buffer = Buffer.from('writeBuffer', 'utf8');
eagle.clipboard.writeBuffer('public/utf8-plain-text', buffer);
```

***

## readBuffer(format) <a href="#gadle" id="gadle"></a>

從剪貼板中讀取 `format` 類型的內容。

* 返回 Buffer

```javascript
const buffer = Buffer.from('this is binary', 'utf8');
eagle.clipboard.writeBuffer('public/utf8-plain-text', buffer);

const out = eagle.clipboard.readBuffer('public/utf8-plain-text');

console.log(buffer.equals(out));	// true
```

***

## writeImage(image) <a href="#cwuzf" id="cwuzf"></a>

將 `image` 寫入剪貼板。

* `image` [NativeImage](https://www.electronjs.org/zh/docs/latest/api/native-image) - 欲寫入剪貼板圖片

```javascript
let img = nativeImage.createFromPath('path_to_img_file');
eagle.clipboard.writeImage(img);
```

***

## readImage() <a href="#hfggy" id="hfggy"></a>

從剪貼板中讀取圖片格式內容。

* 返回 [NativeImage](https://www.electronjs.org/zh/docs/latest/api/native-image)

```javascript
let input = nativeImage.createFromPath('path_to_img_file');
eagle.clipboard.writeImage(input);

let output = eagle.clipboard.readImage();
```

***

## writeHTML(markup) <a href="#naujl" id="naujl"></a>

將 `markup` 作為 HTML 格式寫入剪貼板。

* `markup` string

```javascript
eagle.clipboard.writeHTML('<b>Hi</b>');
console.log(eagle.clipboard.readHTML());	// <b>Hi</b>
```

***

## readHTML() <a href="#btaqx" id="btaqx"></a>

從剪貼板中讀取 HTML 格式內容。

* 返回 string

```javascript
eagle.clipboard.writeHTML('<b>Hi</b>');
console.log(eagle.clipboard.readHTML());	// <b>Hi</b>
```

***

## copyFiles(paths) <a href="#t8sny" id="t8sny"></a>

將指定檔案複製到剪貼板，支援檔案管理器粘貼。

* `paths` strings\[] - 欲複製到剪貼板的檔案。

```javascript
eagle.clipboard.copyFiles([
    'path_to_file',
    'path_to_file2'
]);
```

***


---

# 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/clipboard.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.
