-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Moved part of the database to indexedDB - Speeded up page loading and extension performance - Fixed bugs
- Loading branch information
Showing
13 changed files
with
304 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
"use strict"; | ||
|
||
let db,mode; | ||
|
||
function getDB(){ | ||
if(mode!==undefined)return mode; | ||
const request=indexedDB.open("WPS"); | ||
return new Promise((resolve,reject)=>{ | ||
request.onerror=(e)=>{ | ||
console.warn(e.target.error); | ||
mode=false; | ||
resolve(false); | ||
}; | ||
request.onsuccess=(e)=>{ | ||
db=e.target.result; | ||
mode=true; | ||
resolve(true); | ||
}; | ||
request.onupgradeneeded=(e)=>{ | ||
db=e.target.result; | ||
db.createObjectStore("changes",{keyPath:"uniq"}); | ||
}; | ||
}); | ||
} | ||
|
||
async function getChanges(index){ | ||
if(await getDB()){ | ||
const tx=db.transaction("changes","readonly"); | ||
const store=tx.objectStore("changes"); | ||
const request=store.get(index); | ||
return new Promise((resolve,reject)=>{ | ||
tx.onerror=(e)=>{ | ||
console.error(e.target.error); | ||
}; | ||
request.onsuccess=(e)=>{ | ||
resolve(e.target.result); | ||
}; | ||
}); | ||
}else{ | ||
return await browser.runtime.sendMessage({ | ||
"getChanges":true, | ||
"index":index | ||
}); | ||
} | ||
} | ||
|
||
async function getAllChanges(){ | ||
if(await getDB()){ | ||
const tx=db.transaction("changes","readonly"); | ||
const store=tx.objectStore("changes"); | ||
const request=store.getAll(); | ||
return new Promise((resolve,reject)=>{ | ||
tx.onerror=(e)=>{ | ||
console.error(e.target.error); | ||
}; | ||
request.onsuccess=(e)=>{ | ||
resolve(e.target.result); | ||
}; | ||
}); | ||
}else{ | ||
return await browser.runtime.sendMessage({ | ||
"getAllChanges":true | ||
}); | ||
} | ||
} | ||
|
||
async function setChanges(data){ | ||
if(await getDB()){ | ||
const tx=db.transaction("changes","readwrite"); | ||
const store=tx.objectStore("changes"); | ||
store.put(data); | ||
return new Promise((resolve,reject)=>{ | ||
tx.onerror=(e)=>{ | ||
console.error(e.target.error); | ||
}; | ||
tx.oncomplete=(e)=>{ | ||
resolve(true); | ||
}; | ||
}); | ||
}else{ | ||
return await browser.runtime.sendMessage({ | ||
"setChanges":true, | ||
"record":data | ||
}); | ||
} | ||
} | ||
|
||
async function deleteChanges(index){ | ||
if(await getDB()){ | ||
const tx=db.transaction("changes","readwrite"); | ||
const store=tx.objectStore("changes"); | ||
store.delete(index); | ||
return new Promise((resolve,reject)=>{ | ||
tx.onerror=(e)=>{ | ||
console.error(e.target.error); | ||
}; | ||
tx.oncomplete=(e)=>{ | ||
resolve(true); | ||
}; | ||
}); | ||
}else{ | ||
return await browser.runtime.sendMessage({ | ||
"deleteChanges":true, | ||
"index":index | ||
}); | ||
} | ||
} | ||
|
||
async function clearChanges(){ | ||
if(await getDB()){ | ||
const tx=db.transaction("changes","readwrite"); | ||
const store=tx.objectStore("changes"); | ||
store.clear(); | ||
return new Promise((resolve,reject)=>{ | ||
tx.onerror=(e)=>{ | ||
console.error(e.target.error); | ||
resolve(false); | ||
}; | ||
tx.oncomplete=(e)=>{ | ||
resolve(true); | ||
}; | ||
}); | ||
}else{ | ||
return await browser.runtime.sendMessage({ | ||
"clearChanges":true | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.