Skip to content

Commit

Permalink
fix: db error
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon406 committed Dec 30, 2024
1 parent 5cde0d2 commit 65345b6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "__MSG_appName__",
"description": "__MSG_appDesc__",
"default_locale": "zh_CN",
"version": "0.11.0",
"version": "0.11.1",
"icons": {
"48": "assets/result48.png",
"128": "assets/icon_128.png"
Expand Down
54 changes: 37 additions & 17 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ function initDatabase() {
}

function saveToIndexedDB(q, data) {
if (!db) {
console.log('Database not initialized')
return
}
const transaction = db.transaction(['dictionary'], 'readwrite')
const objectStore = transaction.objectStore('dictionary')
const request = objectStore.add({ q: q.toLowerCase(), data: data })
Expand All @@ -47,6 +51,11 @@ function saveToIndexedDB(q, data) {

function getFromIndexedDB(q) {
return new Promise((resolve, reject) => {
if (!db) {
console.log('Database not initialized')
resolve(null)
return
}
const transaction = db.transaction(['dictionary'], 'readonly')
const objectStore = transaction.objectStore('dictionary')
const request = objectStore.get(q.toLowerCase())
Expand Down Expand Up @@ -133,6 +142,33 @@ function load_idioms() {
.then(do_load_idioms)
}

function handleMessage(request,sendResponse){
return fetch(
`https://cn.bing.com/dict/clientsearch?mkt=zh-CN&setLang=zh&form=BDVEHC&ClientVer=BDDTV3.5.1.4320&q=${encodeURIComponent(request.q)}`,
)
.then((response) => response.text())
.then((html) => {
const minimizeHtml = html
.replace(/<script [\s\S]+?<\/script>/g, '')
.replace(/<head[\s\S]+?<\/head>/g, '')
// 删除无用跳转数据
.replace(
/<span id="anchor1">[\s\S]+?<span id="dictionaryvoiceid"><\/span>/g,
'</div></div></div>',
)
// console.log("minimizeHtml",minimizeHtml)
if (db) {
saveToIndexedDB(request.q, minimizeHtml)
}else {
console.log('waiting for DB...')
setTimeout(function () {
saveToIndexedDB(request.q, minimizeHtml)
}, 3000)
}
sendResponse(minimizeHtml)
})
}

function initialize_extension() {
initDatabase()
chrome.runtime.onMessage.addListener(
Expand All @@ -145,23 +181,7 @@ function initialize_extension() {
sendResponse(cachedData)
} else {
console.log('fetch bing: ', request.q)
return fetch(
`https://cn.bing.com/dict/clientsearch?mkt=zh-CN&setLang=zh&form=BDVEHC&ClientVer=BDDTV3.5.1.4320&q=${encodeURIComponent(request.q)}`,
)
.then((response) => response.text())
.then((html) => {
const minimizeHtml = html
.replace(/<script [\s\S]+?<\/script>/g, '')
.replace(/<head[\s\S]+?<\/head>/g, '')
// 删除无用跳转数据
.replace(
/<span id="anchor1">[\s\S]+?<span id="dictionaryvoiceid"><\/span>/g,
'</div></div></div>',
)
// console.log("minimizeHtml",minimizeHtml)
saveToIndexedDB(request.q, minimizeHtml)
sendResponse(minimizeHtml)
})
return handleMessage(request, sendResponse)
}
})
.catch((error) => {
Expand Down
1 change: 1 addition & 0 deletions test2.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<body>
<div>to look at haven't have haven</div>
<div>haven’t boogy man</div>
<div> enthusiasm</div>
<div>I don’t blame her
I don’t blame you
I don’t give a monkey’s
Expand Down

0 comments on commit 65345b6

Please sign in to comment.