From 95de18ce4e1a76013a614ef905a8d236e67e303f Mon Sep 17 00:00:00 2001
From: starknt <1431880400@qq.com>
Date: Tue, 2 Jan 2024 11:15:44 +0800
Subject: [PATCH] feat: mix `bilibili` history
---
src/components/SearchBar/SearchBar.vue | 19 ++++---
.../SearchBar/searchHistoryProvider.ts | 54 ++++++++++++++++---
2 files changed, 57 insertions(+), 16 deletions(-)
diff --git a/src/components/SearchBar/SearchBar.vue b/src/components/SearchBar/SearchBar.vue
index ecb5c90b..a63f2a27 100644
--- a/src/components/SearchBar/SearchBar.vue
+++ b/src/components/SearchBar/SearchBar.vue
@@ -1,4 +1,5 @@
diff --git a/src/components/SearchBar/searchHistoryProvider.ts b/src/components/SearchBar/searchHistoryProvider.ts
index e718b7b8..9153b963 100644
--- a/src/components/SearchBar/searchHistoryProvider.ts
+++ b/src/components/SearchBar/searchHistoryProvider.ts
@@ -1,4 +1,5 @@
const SEARCH_HISTORY_KEY = 'bew_search_history'
+const BILIBILI_HISTORY_KEY = 'search_history:search_history'
const SEARCH_HISTORY_LIMIT = 10
export interface HistoryItem {
@@ -9,23 +10,57 @@ export interface SuggestionItem {
value: string
timestamp: number
}
+export interface StorageEvent {
+ type: 'COLS_RES'
+ id?: string
+ key: string
+ value: string
+}
function historySort(historyItems: HistoryItem[]) {
historyItems.sort((a, b) => b.timestamp - a.timestamp)
return historyItems
}
-export function getSearchHistory(): HistoryItem[] {
- const history = localStorage.getItem(SEARCH_HISTORY_KEY)
+export async function getSearchHistory(): Promise {
+ let history: string | HistoryItem[] | null = localStorage.getItem(SEARCH_HISTORY_KEY)
if (!history) {
localStorage.setItem(SEARCH_HISTORY_KEY, JSON.stringify([]))
- return []
+ history = []
}
- return historySort(JSON.parse(history))
+ else {
+ history = JSON.parse(history)
+ }
+
+ const iframe = Array.from(document.getElementsByTagName('iframe')).find(i => i.src.includes('https://s1.hdslb.com/bfs/seed/jinkela/short/cols/iframe.html'))
+ if (iframe && iframe.contentWindow) {
+ function getStorage() {
+ return new Promise((resolve) => {
+ iframe!.contentWindow!.postMessage({ type: 'COLS_GET', key: BILIBILI_HISTORY_KEY }, iframe!.src)
+ window.addEventListener('message', (e: MessageEvent) => {
+ if (e.origin === 'https://s1.hdslb.com' && e.data && e.data?.type === 'COLS_RES' && e.data?.key === BILIBILI_HISTORY_KEY)
+ resolve(e.data)
+ }, { once: true })
+ })
+ }
+
+ const e = await getStorage()
+ if (e.value && Array.isArray(history)) {
+ try {
+ const h = JSON.parse(e.value)
+ history.push(...Array.isArray(h) ? h : [])
+ }
+ catch {
+ // ignore
+ }
+ }
+ }
+
+ return historySort(history as HistoryItem[])
}
-export function addSearchHistory(historyItem: HistoryItem) {
- let history = getSearchHistory()
+export async function addSearchHistory(historyItem: HistoryItem) {
+ let history = await getSearchHistory()
let hasSameValue = false
history.forEach((item) => {
@@ -47,12 +82,15 @@ export function addSearchHistory(historyItem: HistoryItem) {
localStorage.setItem(SEARCH_HISTORY_KEY, JSON.stringify(history))
}
-export function removeSearchHistory(value: string) {
- let history = getSearchHistory()
+export async function removeSearchHistory(value: string) {
+ let history = await getSearchHistory()
history = history.filter(item => item.value !== value)
localStorage.setItem(SEARCH_HISTORY_KEY, JSON.stringify(history))
}
export function clearSearchHistory() {
localStorage.setItem(SEARCH_HISTORY_KEY, JSON.stringify([]))
+ const iframe = Array.from(document.getElementsByTagName('iframe')).find(i => i.src.includes('https://s1.hdslb.com/bfs/seed/jinkela/short/cols/iframe.html'))
+ if (iframe && iframe.contentWindow)
+ iframe.contentWindow.postMessage({ type: 'COLS_CLR', key: 'search_history' }, iframe.src)
}