# 即時聊天 Javascript API

{% hint style="info" %}
請注意，此功能目前僅在內部測試階段，尚未正式開放。請關注我們的更新日誌以取得最新動態。
{% endhint %}

## 概述 <a href="#javascript-api-methods" id="javascript-api-methods"></a>

您可以使用 JavaScript API 來控制 FIRST LINE 即時聊天，執行各種有用、有趣的操作，充分利用 FIRST LINE 即時聊天功能。例如，根據網站客戶的行為來決定即時聊天的顯示方式。

以下是一些可以幫助您執行的操作範例：

#### **清除使用者使用資料**

當使用者登出您的應用程式時，您可以清除使用者的資料。確保共用電腦和瀏覽器的使用者無法查看彼此的對話。

#### 自動開啟對話視窗

根據使用者的行為來決定即時聊天的顯示方式。例如，當使用者點擊購買按鈕時，可以即時打開聊天視窗。

{% hint style="info" %}
JavaScript API 已內建在 FIRST LINE 即時聊天啟動器中。無需安裝或啟用其他內容，您可以直接開始使用，無需任何特定配置。\
\
請注意，即時聊天 JavaScript API 僅限於透過置入碼方法建立的即時聊天。外部連結的方式並無法適用於所有方法。
{% endhint %}

## 方法 <a href="#javascript-api-methods" id="javascript-api-methods"></a>

{% hint style="warning" %}
請注意，方法需在 JavaScript API 已載入或 JavaScript 能執行的階段呼叫。在某些瀏覽器場景下，可能無法完成動作，例如嘗試在網頁關閉前呼叫方法。
{% endhint %}

### $FirstLine('shutdown')

如果您將 即時聊天與其他產品結合使用，當使用者登出您的應用程式時，可以呼叫 ‘shutdown’ 方法來清除用戶相關資訊。否則，登入裝置的 cookie 和 localStorage 會保留這些聯絡人資料。使用 shutdown 方法將有效清除用戶資料，確保隱私安全。

```javascript
$FirstLine('shutdown')
```

{% hint style="warning" %}
使用此方法後，若需要再次使用即時聊天，必須重新透過置入碼等方式重新建立新的即時聊天。
{% endhint %}

### $FirstLine('open')

此方法將打開即時聊天對話視窗。

```javascript
$FirstLine('open')
```

### $FirstLine('close')

此方法將關閉即時聊天對話視窗。

```javascript
$FirstLine('close')
```

### $FirstLine('show')

此方法將顯示整個即時聊天。

```javascript
$FirstLine('show')
```

### $FirstLine('hide')

此方法將隱藏整個即時聊天。

```javascript
$FirstLine('hide')
```

### $FirstLine('onShow')

當顯示即時聊天對話視窗時，您可以指定觸發事件。此方法需要一個函數作為參數。

```javascript
$FirstLine('onShow', function() { /** Do something... **/ })
```

### $FirstLine('onHide')

當顯示即時聊天對話視窗時，您可以指定觸發事件。此方法需要一個函數作為參數。

```javascript
$FirstLine('onHide', function() { /** Do something... **/ })
```

### $FirstLine('onUnreadCountChange')

當顯示即時聊天對話視窗時，您可以指定觸發事件。此方法需要一個函數作為參數。

```javascript
$FirstLine('onUnreadCountChange', function(unreadCount) {
  // Do something...
}))
```

### $FirstLine('showEnticeMessage')

顯示引導訊息，並傳遞一個字串類型的訊息作為第二個參數。此訊息將取代當前顯示的歡迎語。

```javascript
$FirstLine('showEnticeMessage', 'message content')
```


---

# 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://help.firstline.cc/developer/ji-shi-liao-tian-javascript-api.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.
