Skip to content

Commit

Permalink
add IndexedDB to BASE
Browse files Browse the repository at this point in the history
  • Loading branch information
AlterTobi committed Aug 23, 2023
1 parent b69f980 commit 08fac6a
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 13 deletions.
22 changes: 14 additions & 8 deletions wfes-AddTranslationButtons.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @name Add Translation Buttons
// @version 2.0.0
// @version 2.1.0
// @description Adds a button to translate the text associated with a wayspot
// @author AlterTobi

Expand Down Expand Up @@ -40,7 +40,7 @@

function init() {
window.wfes.f.addCSS(myCSSId, myStyle);
window.wfes.f.localGet(storageName, "Deepl").then(e => {
window.wfes.f.localGetIDBcompat(storageName, "Deepl").then(e => {
currentEngine = e;
});
}
Expand Down Expand Up @@ -74,7 +74,7 @@

select.addEventListener("change", function() {
currentEngine = select.value;
window.wfes.f.localSave(storageName, currentEngine);
window.wfes.f.localSaveIDBcompat(storageName, currentEngine);
link.href = engines[currentEngine].url + encodeURIComponent(text);
link.target = engines[currentEngine].target;
});
Expand Down Expand Up @@ -157,11 +157,17 @@
translateButton.remove();
}

init();
window.addEventListener("WFESReviewPageNewLoaded", addTranslationButtonsNew);
window.addEventListener("WFESReviewPageEditLoaded", addTranslationButtonsEdit);
window.addEventListener("WFESReviewPagePhotoLoaded", addTranslationButtonsPhoto);
window.addEventListener("WFESReviewDecisionSent", removeButton);

if (window.wfes.f.hasMinVersion("1.9.0")) {
init();
window.addEventListener("WFESReviewPageNewLoaded", addTranslationButtonsNew);
window.addEventListener("WFESReviewPageEditLoaded", addTranslationButtonsEdit);
window.addEventListener("WFESReviewPagePhotoLoaded", addTranslationButtonsPhoto);
window.addEventListener("WFESReviewDecisionSent", removeButton);
} else {
console.warn(GM_info.script.name, "Need at least wfes-Base version 1.9.0. Please upgrade.");
}

/* we are done :-) */
console.log("Script loaded:", GM_info.script.name, "v" + GM_info.script.version);
})();
82 changes: 77 additions & 5 deletions wfes-Base.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @name Base
// @version 1.8.0
// @version 1.9.0
// @description basic functionality for WFES
// @author AlterTobi
// @run-at document-start
Expand All @@ -11,9 +11,13 @@

/* WFES data structures */
const PREFIX = "/api/v1/vault/";
const loginPath = "/login";
// session storage
const sStoreReview = "wfes_Reviews";
const sStoreNominationsDetails = "wfes_nominationDetails";
const loginPath = "/login";
// indexedDB
const idbName = "wfes-data";
const idbLocalStorageCompat = "localStorage";

const myCssId = "notifyAreaCSS";
const myStyle = `
Expand Down Expand Up @@ -89,7 +93,6 @@
});
}


function checkWfVersion(v) {
if (wfes.version !== v) {
// console.log("WF version changed from", wfes.version, "to", v);
Expand Down Expand Up @@ -150,6 +153,77 @@
checkUID(1);
});

/* =========== IndexedDB ============================= */
const getIDBInstance = version => new Promise((resolve, reject) => {

if (!window.indexedDB) {
reject("This browser doesn't support IndexedDB!");
return;
}

const openRequest = window.indexedDB.open(idbName, version);
openRequest.onsuccess = event => {
const db = event.target.result;
resolve(db);
};
openRequest.onerror = (event) => {
console.error("Error using IndexedDB", event.target.errorCode);
reject(event.target.errorCode);
};
openRequest.onupgradeneeded = event => {
console.log(GM_info.script.name, "Upgrading database...");
const db = event.target.result;
// const ver = db.version;
if (!db.objectStoreNames.contains(idbLocalStorageCompat)) {
db.createObjectStore(idbLocalStorageCompat, { keyPath: "index" });
}
};
});

// save data in "localstorage"
window.wfes.f.localSaveIDBcompat = (name, content) => new Promise((resolve, reject) => {
getUserId().then((userId) => {
const json = JSON.stringify(content);
const index = name+"_"+userId;
const data = {index: index, data:json};
getIDBInstance().then(db => {
const tx = db.transaction([idbLocalStorageCompat], "readwrite");
tx.oncomplete = event => { db.close(); resolve(); };
tx.onerror = reject;
const objectStore = tx.objectStore(idbLocalStorageCompat);
objectStore.put(data);
tx.commit();
})
.catch(reject);
});
});

// get data from "localstorage"
window.wfes.f.localGetIDBcompat = (name, content = "") => new Promise((resolve, reject) => {
getUserId().then((userId) => {
const index = name+"_"+userId;
getIDBInstance().then(db => {
const tx = db.transaction([idbLocalStorageCompat], "readonly");
tx.oncomplete = ( event ) => {db.close();};
tx.onerror = ( event ) => {console.log("transaction error:", event);};
const objectStore = tx.objectStore(idbLocalStorageCompat);
const request = objectStore.get(index);
request.onsuccess = () => {
if (request.result) {
resolve(request.result.data);
} else {
resolve(content);
}
};
request.onerror = (event) => {
console.log("error retrieving data:", event);
resolve(undefined);
};
});
});
});
/* =========== /IndexedDB ============================ */

/* ================ overwrite XHR ================ */
const openOrig = window.XMLHttpRequest.prototype.open, sendOrig = window.XMLHttpRequest.prototype.send;

Expand Down Expand Up @@ -317,7 +391,6 @@

window.XMLHttpRequest.prototype.open = openReplacement;
window.XMLHttpRequest.prototype.send = sendReplacement;

/* ================ /overwrite XHR ================ */

/* ================ showcase ====================== */
Expand Down Expand Up @@ -558,7 +631,6 @@
window.wfes.g.isMobile = function() {
return _isMobile;
};

/* ================ /getter ======================= */

/* ================ setter ======================== */
Expand Down

0 comments on commit 08fac6a

Please sign in to comment.