Skip to content

API 文档

本文档详细介绍 AI-SideChat 的内部 API。

Storage API

getFavorites()

获取所有收藏数据。

javascript
async function getFavorites()

返回值Promise<Array>

示例

javascript
const favorites = await getFavorites()
console.log(favorites.length) // 10

saveFavorite(data)

保存一条收藏。

javascript
async function saveFavorite(data)

参数

参数类型说明
dataObject收藏数据对象

返回值Promise<void>

示例

javascript
await saveFavorite({
  id: Date.now(),
  question: 'How to use Python?',
  answer: 'Python is...',
  // ... 其他字段
})

removeFavorite(id)

删除指定收藏。

javascript
async function removeFavorite(id)

参数

参数类型说明
idnumber收藏 ID

返回值Promise<void>

updateFavorite(id, updates)

更新收藏数据。

javascript
async function updateFavorite(id, updates)

参数

参数类型说明
idnumber收藏 ID
updatesObject要更新的字段

示例

javascript
await updateFavorite(123456, {
  tags: ['Python', 'Tutorial']
})

Hash API

simpleHash(text)

计算文本的哈希值。

javascript
function simpleHash(text)

参数

参数类型说明
textstring要计算哈希的文本

返回值string - 哈希值

示例

javascript
const hash = simpleHash('Hello World')
console.log(hash) // "123456789"

Platform API

detectPlatform()

检测当前 AI 平台。

javascript
function detectPlatform()

返回值

typescript
{
  name: 'Gemini' | 'ChatGPT' | null,
  color: string,
  selectors: {
    userBubble: string,
    answer: string,
    convTitle: string
  }
}

示例

javascript
const platform = detectPlatform()
if (platform?.name === 'Gemini') {
  console.log('当前在 Gemini 平台')
}

Jump API

performJump(questionHash, bubbleIndex)

执行跳转定位。

javascript
function performJump(questionHash, bubbleIndex)

参数

参数类型说明
questionHashstring问题哈希值
bubbleIndexnumber问题索引

返回值boolean - 是否定位成功

strategyA_IndexWithHash(hash, index)

策略 A:索引精准命中 + Hash 校验。

javascript
function strategyA_IndexWithHash(hash, index)

strategyB_HashSearch(hash)

策略 B:Hash 全局搜索。

javascript
function strategyB_HashSearch(hash)

strategyC_IndexFallback(index)

策略 C:索引强制兜底。

javascript
function strategyC_IndexFallback(index)

UI API

highlightElement(element)

高亮显示元素。

javascript
function highlightElement(element)

参数

参数类型说明
elementHTMLElement要高亮的元素

效果

  • 添加高亮样式
  • 2 秒后自动移除

scrollToElement(element)

滚动到指定元素。

javascript
function scrollToElement(element)

参数

参数类型说明
elementHTMLElement目标元素

Message API

消息类型

PERFORM_JUMP

执行跳转定位。

javascript
{
  type: 'PERFORM_JUMP',
  data: {
    questionHash: string,
    bubbleIndex: number
  }
}

UPDATE_FAVORITES

更新收藏列表。

javascript
{
  type: 'UPDATE_FAVORITES'
}

OPEN_TAB

打开新标签页。

javascript
{
  type: 'OPEN_TAB',
  url: string
}

发送消息

javascript
// 发送到 background
chrome.runtime.sendMessage({
  type: 'OPEN_TAB',
  url: 'https://example.com'
})

// 发送到 content script
chrome.tabs.sendMessage(tabId, {
  type: 'PERFORM_JUMP',
  data: { ... }
})

Event API

storage.onChanged

监听存储变化。

javascript
chrome.storage.onChanged.addListener((changes, namespace) => {
  if (namespace === 'local' && changes.aiClipData) {
    const newValue = changes.aiClipData.newValue
    // 更新 UI
  }
})

Utility API

debounce(func, wait)

防抖函数。

javascript
function debounce(func, wait)

示例

javascript
const debouncedSearch = debounce((keyword) => {
  performSearch(keyword)
}, 300)

input.addEventListener('input', (e) => {
  debouncedSearch(e.target.value)
})

throttle(func, limit)

节流函数。

javascript
function throttle(func, limit)

类型定义

Favorite

typescript
interface Favorite {
  id: number
  questionHash: string
  bubbleIndex: number
  contentHash: string
  site: 'Gemini' | 'ChatGPT'
  siteColor: string
  convTitle: string
  question: string
  answer: string
  answerHtml: string
  url: string
  tags: string[]
  timestamp: number
}

Platform

typescript
interface Platform {
  name: 'Gemini' | 'ChatGPT'
  color: string
  selectors: {
    userBubble: string
    answer: string
    convTitle: string
  }
}

下一步

Released under the MIT License.