Skip to content

Commit

Permalink
Version 2.1.5
Browse files Browse the repository at this point in the history
- Moved part of the database to indexedDB
- Speeded up page loading and extension performance
- Fixed bugs
  • Loading branch information
WaldiPL committed Sep 16, 2022
1 parent f8ff012 commit a1ff0e6
Show file tree
Hide file tree
Showing 13 changed files with 304 additions and 87 deletions.
1 change: 1 addition & 0 deletions src/bg.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<body>
<script src="md5.js"></script>
<script src="favicon.js"></script>
<script src="database.js"></script>
<script src="shared.js"></script>
<script src="bg.js"></script>
</body>
Expand Down
67 changes: 54 additions & 13 deletions src/bg.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,19 @@ function handleInstalled(details) {
"warnBeforeUpdating":true,
};
if(details.reason==="install"){
browser.storage.local.get(['sites','settings']).then(result=>{
const db={};
browser.storage.local.get(['sites','settings','dbVersion']).then(result=>{
const storage={};
if(result.sites===undefined){
Object.assign(db,{sites:[],changes:[],sort:[]});
Object.assign(storage,{sites:[],sort:[]});
}
if(result.settings===undefined){
Object.assign(db,{settings:defaultSettings});
Object.assign(storage,{settings:defaultSettings});
}
browser.storage.local.set(db).then(()=>{
if(result.dbVersion===undefined){
Object.assign(storage,{dbVersion:1});
}
browser.storage.local.set(storage).then(async ()=>{
if(!db)await getDB();
init();
if(!details.temporary){
browser.tabs.create({
Expand Down Expand Up @@ -125,11 +129,13 @@ browser.notifications.onClicked.addListener(e=>{
});

(function(){
browser.storage.local.get(['sites','sort','settings']).then(result=>{
browser.storage.local.get(['sites','sort','settings','dbVersion']).then(result=>{
if(result.sites===undefined||result.settings===undefined){
handleInstalled({reason:"install"});
}else{
}else if(result.dbVersion===1){
init();
}else{
convertDb();
}
if(result.sort===undefined&&result.sites!==undefined){
let sort=[];
Expand All @@ -150,16 +156,15 @@ function init(){
showContext(result.settings.addToContextMenu);
let period=result.settings.period?result.settings.period:60;
if(!result.settings.paused){
browser.alarms.create("webpageScanner",{periodInMinutes:period+1});
browser.alarms.create("webpageScanner2",{delayInMinutes:1});
browser.alarms.create("webpageScanner",{delayInMinutes:1,periodInMinutes:period+1});
}
},err=>{
console.error(err);
});
}

browser.alarms.onAlarm.addListener(alarm=>{
if(alarm.name==="webpageScanner"||alarm.name==="webpageScanner2")scanSites();
if(alarm.name==="webpageScanner")scanSites();
else if(alarm.name==="openSitesDelay")openSitesDelay();
});

Expand All @@ -174,7 +179,7 @@ function run(m,s,r){
if(m.scanSites)scanSites(m.force);
if(m.openSites)openSite();
if(m.addToContextMenu!==undefined)showContext(m.addToContextMenu);
if(m.period)browser.alarms.create("webpageScanner",{periodInMinutes:m.period});
if(m.period)browser.alarms.create("webpageScanner",{delayInMinutes:1,periodInMinutes:m.period});
if(m.openSitesDelay){delayCurrentId=0;delayTime=m.openSitesDelay;delayLinksId=m.linksId;lastWindowId=-1;openSitesDelay(m.openWindow);}
if(m.closeTab){browser.tabs.remove(s.tab.id);}
if(m.scanPagesById){scanPagesById(m.idArray);}
Expand Down Expand Up @@ -275,6 +280,12 @@ function run(m,s,r){
browser.tabs.sendMessage(s.tab.id,m).then(()=>{},err=>{console.warn(err);});
}
if(m.openViewPage){browser.tabs.create({url:`${extURL}view.html?${m.viewId}`});}
if(m.convertDB){convertDb(true);}
if(m.getChanges){return getChanges(m.index);}
if(m.getAllChanges){return getAllChanges();}
if(m.setChanges){return setChanges(m.record);}
if(m.deleteChanges){return deleteChanges(m.index);}
if(m.clearChanges){return clearChanges();}
}

function showContext(e){
Expand Down Expand Up @@ -391,6 +402,7 @@ function showPopup(mode="add",editId){
function editSite(id,url,title,mode,freq,charset,cssSelector,ignoreNumbers,ignoreHrefs,deleteScripts,deleteComments,pageSettings,ignoreStyles,ignoreAllAttributes,saveOnlyPart){
browser.storage.local.get("sites").then(async result=>{
let sites=result.sites;
const uniqId=sites[id].uniq;
let obj={
url,
title,
Expand All @@ -416,8 +428,8 @@ function editSite(id,url,title,mode,freq,charset,cssSelector,ignoreNumbers,ignor
ignoreAllAttributes:sites[id].ignoreAllAttributes,
};
if(old.paritialMode!==obj.paritialMode||old.cssSelector!==obj.cssSelector||old.ignoreNumbers!==ignoreNumbers||old.ignoreHrefs!==ignoreHrefs||old.ignoreStyles!==ignoreStyles||old.ignoreAllAttributes!==ignoreAllAttributes){
const {changes}=await browser.storage.local.get("changes");
const html_data=changes[id].html;
const idb=await getChanges(uniqId);
const html_data=idb.html;
if(cssSelector===false){
Object.assign(obj,length_md5(html_data,ignoreNumbers,ignoreHrefs,ignoreStyles,ignoreAllAttributes));
}else{
Expand Down Expand Up @@ -451,3 +463,32 @@ function editSite(id,url,title,mode,freq,charset,cssSelector,ignoreNumbers,ignor
console.error(err);
});
}

async function convertDb(e){
if(!db)await getDB();
browser.storage.local.get(["sites","changes"]).then(async result=>{
const {sites,changes}=result;
sites.forEach((site,i)=>{
if(!site.uniq)site.uniq=uniqueId();
});
await browser.storage.local.set({sites});
const tx=db.transaction(["changes"],"readwrite");
const store=tx.objectStore("changes");
tx.oncomplete=(e)=>{
browser.storage.local.set({dbVersion:1}).then(()=>{
browser.storage.local.remove("changes");
if(!e)init();
browser.runtime.sendMessage({"listSite":true}).then(()=>{},err=>{console.warn(err);});
},err=>{
console.error(err);
});
};
tx.onerror=(e)=>{
console.error(e.target.error);
};
sites.forEach((site,i)=>{
changes[i].uniq=site.uniq;
store.add(changes[i]);
});
});
}
6 changes: 3 additions & 3 deletions src/changelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ function generateChangelog(){
changelogGenerated=true;

let changelog=`[
{"version":"2.1.2",
"changes":["Added the possibility to select a file as notification sound","Updated appearance and icons to Proton style","Added dark theme for options","Added automatic theme","Minor changes"],
"changesPL":["Dodano możliwość wybrania pliku jako dźwięku powiadomienia","Zaktualizowano wygląd i ikony do stylu Proton","Dodano ciemny motyw opcji","Dodano motyw automatyczny","Drobne zmiany"]
{"version":"2.1.5",
"changes":["Moved part of the database to indexedDB","Speeded up page loading and extension performance","Added the possibility to select a file as notification sound","Updated appearance and icons to Proton style","Added dark theme for options","Added automatic theme","Minor changes","Fixed bugs"],
"changesPL":["Przeniesiono część bazy danych do indexedDB","Przyśpieszono ładowanie stron i działanie rozszerzenia","Dodano możliwość wybrania pliku jako dźwięku powiadomienia","Zaktualizowano wygląd i ikony do stylu Proton","Dodano ciemny motyw opcji","Dodano motyw automatyczny","Drobne zmiany","Naprawiono błędy"]
},
{"version":"2.1.0",
"changes":["Fixed problems with partial scanning","#60","Fixed not working notification","Improved import and export of folders","#63","#64","Added possibility to drag and drop bookmarks, folders, links and web pages","#65","Added German language (thanks to Omnibrain007)","#68","Added possibility to sort pages","#45","Minor changes","#59","Fixed bugs"],
Expand Down
128 changes: 128 additions & 0 deletions src/database.js
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
});
}
}
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "__MSG_extensionName__",
"description": "__MSG_extensionDescription__",
"version": "2.1.2",
"version": "2.1.5",
"default_locale": "en",
"icons": {
"48": "icons/icon.svg",
Expand Down
1 change: 1 addition & 0 deletions src/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ <h3 id="h3share"></h3>
</section>
<div id="alertToolbar" class="none"><span id="scanningAlert"></span><button id="enableAutoScan"></button></div>
<script src="changelog.js"></script>
<script src="database.js"></script>
<script src="options.js"></script>
</body>
</html>
48 changes: 33 additions & 15 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ function restoreOptions(){

function createBackup(e){
e.preventDefault();
browser.storage.local.get().then(result=>{
browser.storage.local.get().then(async result=>{
result.changes=await getAllChanges();
let a=document.createElement("a");
document.body.appendChild(a);
let json=JSON.stringify(result),
Expand Down Expand Up @@ -426,15 +427,32 @@ function handleFileSelect(e){
}

function restoreBackup(){
browser.storage.local.set({sites:uploaded.sites,changes:uploaded.changes,sort:uploaded.sort}).then(()=>{
document.getElementById("restoreAlert").classList.add("none");
document.getElementById("restoreOk").classList.remove("none");
browser.runtime.sendMessage({"listSite":true}).then(()=>{},err=>{console.warn(err);});
},err=>{
document.getElementById("restoreAlert").classList.add("none");
document.getElementById("restoreError").classList.remove("none");
console.error(err);
});
if(uploaded.dbVersion===1){
browser.storage.local.set({sites:uploaded.sites,sort:uploaded.sort,dbVersion:uploaded.dbVersion}).then(async ()=>{
document.getElementById("restoreAlert").classList.add("none");
document.getElementById("restoreOk").classList.remove("none");
await clearChanges();
uploaded.changes.forEach(async e=>{
await setChanges(e);
});
browser.runtime.sendMessage({"listSite":true}).then(()=>{},err=>{console.warn(err);});
},err=>{
document.getElementById("restoreAlert").classList.add("none");
document.getElementById("restoreError").classList.remove("none");
console.error(err);
});
}else{
browser.storage.local.set({sites:uploaded.sites,changes:uploaded.changes,sort:uploaded.sort,dbVersion:0}).then(async ()=>{
document.getElementById("restoreAlert").classList.add("none");
document.getElementById("restoreOk").classList.remove("none");
await clearChanges();
browser.runtime.sendMessage({"listSite":true,"convertDB":true}).then(()=>{},err=>{console.warn(err);});
},err=>{
document.getElementById("restoreAlert").classList.add("none");
document.getElementById("restoreError").classList.remove("none");
console.error(err);
});
}
}

async function importFolder(e){
Expand Down Expand Up @@ -873,13 +891,12 @@ function deleteBrokenConfirm(e){

function deleteSite(j,mode){
let e=mode==="duplicates"?duplicatedSites[j][0]:brokenSites[j][0];
browser.storage.local.get(['sites','changes','sort']).then(result=>{
browser.storage.local.get(['sites','sort']).then(result=>{
let sites=result.sites,
changes=result.changes,
sort=result.sort,
sSort;
sSort,
uniqId=sites[e].uniq;
sites.splice(e,1);
changes.splice(e,1);
if(sort){
sort.forEach((value,i)=>{
const id=parseInt(value[0].substr(4));
Expand All @@ -888,14 +905,15 @@ function deleteSite(j,mode){
});
sort.splice(sSort,1);
}
browser.storage.local.set({sites,changes,sort}).then(()=>{
browser.storage.local.set({sites,sort}).then(()=>{
browser.runtime.sendMessage({
"deletedSite":true,
"id":e,
"listSite":true
}).then(()=>{},err=>{
console.warn(err);
});
deleteChanges(uniqId);
if(mode==="duplicates"){
if(j+1<duplicatedSites.length)
deleteSite(j+1,mode);
Expand Down
Loading

0 comments on commit a1ff0e6

Please sign in to comment.