DIDECT2S/D3-Annotator/sidebar/panel_backup.js
2021-12-03 17:58:48 +01:00

223 lines
6.2 KiB
JavaScript

/*
* Some silly variables for window dressing etc.
*/
var myWindowId;
const contentBox = document.querySelector("#content");
/*
* Database configurations.
*/
let db;
const DBRequest = window.indexedDB.open("cdskivor", 1);
let IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction;
let objectStore;
const note = document.getElementById('notifications');
if(!indexedDB){
alert("No IDB here man.");
};
DBRequest.onerror = function(event){
alert("Fakking DB error man: ");
};
DBRequest.onsuccess = function(event){
db = DBRequest.result;
alert(db.name);
let transaction = db.transaction(["rushAlbumList"], "readwrite");
transaction.oncomplete = function(event){
note.innerHTML += '<li>Transaction completed: database modification finished.</li>';
alert("Transaction SUCCEEDED");
};
transaction.error = function(event){
console.log("Here was errorsz");
};
/*
var objectStore = db.createObjectStore('rushAlbumList', { keyPath: 'albumTitle' });
objectStore.createIndex("year","year", { unique: false });
var transactionStore = transaction.objectStore("rushAlbumList");
var objectStoreRequest = objectStore.add(newItem[0]);
objectStoreRequest.onsuccess = function(event){
objectStore.add(albumTitle: "Rising", year: 1996);
alert("Reqyest successful");
};*/
};
DBRequest.onupgradeneeded = function(event){
alert("Upgrade needed");
let db = event.target.result;
db.onerror = function(event) {
note.innerHTML += '<li>Error loading database.</li>';
}
};
/*
Make the content box editable as soon as the user mouses over the sidebar.
*/
window.addEventListener("mouseover", () => {
contentBox.setAttribute("contenteditable", true);
});
/*
When the user mouses out, save the current contents of the box.
*/
window.addEventListener("mouseout", () => {
contentBox.setAttribute("contenteditable", false);
browser.tabs.query({windowId: myWindowId, active: true}).then((tabs) => {
let contentToStore = {};
contentToStore[tabs[0].url] = contentBox.textContent;
browser.storage.local.set(contentToStore);
});
});
/*
Press S - store text to file
*/
window.addEventListener("keydown", function(event){
if (event.code == "KeyS"){
console.log("User details inserted into DB. ");
addToDatabase();
}
if(event.code =="KeyD"){
getFromDatabase();
alert("Fetching data from DB... ");
}
else{
alert("No letter key pressed man. ");
}
});
function addToDatabase(){
/*var trans = db.transaction('rushAlbumList');
var obs = trans.objectStore('rushAlbumList');
obs.onerror = function(e){
alert("DB insertion error.");
};
obs.onsuccess = function(e){
alert("DB insertion sucessful.");
};
trans.oncomplete = function(e){
alert("Datta inserted.");
};*/
var records = [
{ albumTitle: 'Power windows', year: 1985 },
{ albumTitle: 'Grace under pressure', year: 1984 },
{ albumTitle: 'Signals', year: 1982 },
{ albumTitle: 'Moving pictures', year: 1981 },
{ albumTitle: 'Permanent waves', year: 1980 },
{ albumTitle: 'Hemispheres', year: 1978 },
{ albumTitle: 'A farewell to kings', year: 1977 },
{ albumTitle: '2112', year: 1976 },
{ albumTitle: 'Caress of steel', year: 1975 },
{ albumTitle: 'Fly by night', year: 1975 },
{ albumTitle: 'Rush', year: 1974 }
];
DBRequest.onerror = function(event){
alert("Fakking DB error man: ");
};
DBRequest.onsuccess = function(event){
db = dbRequest.result;
alert(db.name);
let transaction = db.transaction(["rushAlbumList"], "readwrite");
transaction.oncomplete = function(event){
alert("Transaction SUCCEEDED");
};
transaction.onerror = function(event){
console.log("Here was errorsz");
};
/*
var objectStore = db.createObjectStore('rushAlbumList', { keyPath: 'albumTitle' });
objectStore.createIndex("year","year", { unique: false });
var transactionStore = transaction.objectStore("rushAlbumList");
var objectStoreRequest = objectStore.add(newItem[0]);
objectStoreRequest.onsuccess = function(event){
objectStore.add(albumTitle: "Rising", year: 1996);
alert("Reqyest successful");
};*/
};
DBRequest.onupgradeneeded = function(event){
alert("Upgrade needed");
let db = event.target.result;
db.onerror = function(event) {
note.innerHTML += '<li>Error loading database.</li>';
}
};
/*
for(i = 0; i < records.length ; i++){
os.put(records[i]);
alert(records[i].albumTitle);
};
obs.oncomplete = function(e){
alert("Datta inserted.",e);
};
obs.onsuccess = function(e){
alert("DB insertion sucessful.",e);
};
obs.onerror = function(e){
alert("DB insertion error.", e);
};*/
}
function getFromDatabase(){
db.transaction("users").objectStore("users").get("id").onsuccess = function(event){
alert("Fetched from database is: " + event.target.result.name);
}
}
function updateContent() {
browser.tabs.query({windowId: myWindowId, active: true})
.then((tabs) => {
return browser.storage.local.get(tabs[0].url);
})
.then((storedInfo) => {
contentBox.textContent = storedInfo[Object.keys(storedInfo)[0]];
});
}
function insertZango(people){
let docs = [
{ name: 'Frank', age: 20 },
{ name: 'Thomas', age: 33 },
{ name: 'Todd', age: 88 },
];
people.insert(docs).then(() => {
return people.find({
name: { $ne: 'Todd' },
}).forEach(doc => console.log('doc:', doc));
}).catch(error => console.error(error));
}
/*
Update content when a new tab becomes active.
*/
browser.tabs.onActivated.addListener(updateContent);
/*
Update content when a new page is loaded into a tab.
*/
browser.tabs.onUpdated.addListener(updateContent);
/*
When the sidebar loads, get the ID of its window,
and update its content.
*/
browser.windows.getCurrent({populate: true}).then((windowInfo) => {
myWindowId = windowInfo.id;
updateContent();
});