Skip to content

Commit

Permalink
refactor: 大幅精简 bing dict html
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon406 committed Dec 27, 2024
1 parent a1b3824 commit 15c37cd
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
},
"devDependencies": {
"@types/chrome": "^0.0.268",
"@types/dompurify": "^3.0.5",
"copy-webpack-plugin": "^12.0.2",
"eslint": "^8.57.0",
"eslint-config-airbnb-base": "^15.0.0",
Expand Down
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.10.0",
"version": "0.10.1",
"icons": {
"48": "assets/result48.png",
"128": "assets/icon_128.png"
Expand Down
11 changes: 5 additions & 6 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ function initDatabase() {
function saveToIndexedDB(q, data) {
const transaction = db.transaction(['dictionary'], 'readwrite')
const objectStore = transaction.objectStore('dictionary')
console.log('save to db', data)
const request = objectStore.add({ q: q.toLowerCase(), data: data })

request.onsuccess = function(event) {
console.log('Data saved to IndexedDB:', q)
console.info(`\x1B[31msaved to DB ==> \x1B[34m${q}`)
}

request.onerror = function(event) {
Expand Down Expand Up @@ -142,7 +141,7 @@ function initialize_extension() {
getFromIndexedDB(request.q)
.then(cachedData => {
if (cachedData) {
console.info(`\t==>db cache : ${request.q}`)
console.info(`\tcache: \x1b[43m${request.q}`)
sendResponse(cachedData)
} else {
console.log('fetch bing: ', request.q)
Expand All @@ -152,10 +151,10 @@ function initialize_extension() {
.then(html => {
const minimizeHtml = html
.replace(/<script [\s\S]+?<\/script>/g, '')
.replace(/<style[\s\S]+?<\/style>/g, '')
.replace(/<head[\s\S]+?<\/head>/g, '')
// 删除无用跳转数据
.replace(/<a class="client_sen_[ce]n_word"[^>]+>[^>]+?<\/a>/g, '')
.replace('<span class="client_sen_word"></span>', '')
.replace(/<span id="anchor1">[\s\S]+?<span id="dictionaryvoiceid"><\/span>/g, '</div></div></div>')
// console.log("minimizeHtml",minimizeHtml)
saveToIndexedDB(request.q, minimizeHtml)
sendResponse(minimizeHtml)
})
Expand Down

0 comments on commit 15c37cd

Please sign in to comment.