Rewrite examples to use browser.* and promises (#138)

This commit is contained in:
wbamberg
2016-11-15 10:12:55 -08:00
committed by GitHub
parent 664239dac0
commit c5d69d15d6
33 changed files with 290 additions and 244 deletions

View File

@@ -14,23 +14,24 @@ var addBtn = document.querySelector('.add');
addBtn.addEventListener('click', addNote);
clearBtn.addEventListener('click', clearAll);
/* generic error handler */
function onError(error) {
console.log(error);
}
/* display previously-saved stored notes on startup */
initialize();
function initialize() {
chrome.storage.local.get(null,function(results) {
if(chrome.runtime.lastError) {
console.log(chrome.runtime.lastError);
} else {
var noteKeys = Object.keys(results);
for(i = 0; i < noteKeys.length; i++) {
var curKey = noteKeys[i];
var curValue = results[curKey];
displayNote(curKey,curValue);
}
var gettingAllStorageItems = browser.storage.local.get(null);
gettingAllStorageItems.then((results) => {
var noteKeys = Object.keys(results);
for(noteKey of noteKeys) {
var curValue = results[noteKey];
displayNote(noteKey,curValue);
}
});
}, onError);
}
/* Add a note to the display, and storage */
@@ -38,26 +39,24 @@ function initialize() {
function addNote() {
var noteTitle = inputTitle.value;
var noteBody = inputBody.value;
chrome.storage.local.get(noteTitle, function(result) {
var gettingItem = browser.storage.local.get(noteTitle);
gettingItem.then((result) => {
var objTest = Object.keys(result);
if(objTest.length < 1 && noteTitle !== '' && noteBody !== '') {
inputTitle.value = '';
inputBody.value = '';
storeNote(noteTitle,noteBody);
}
})
}, onError);
}
/* function to store a new note in storage */
function storeNote(title, body) {
chrome.storage.local.set({ [title] : body }, function() {
if(chrome.runtime.lastError) {
console.log(chrome.runtime.lastError);
} else {
displayNote(title,body);
}
});
var storingNote = browser.storage.local.set({ [title] : body });
storingNote.then(() => {
displayNote(title,body);
}, onError);
}
/* function to display a note in the note box */
@@ -92,7 +91,7 @@ function displayNote(title, body) {
deleteBtn.addEventListener('click',function(e){
evtTgt = e.target;
evtTgt.parentNode.parentNode.parentNode.removeChild(evtTgt.parentNode.parentNode);
chrome.storage.local.remove(title);
browser.storage.local.remove(title);
})
/* create note edit box */
@@ -155,15 +154,17 @@ function displayNote(title, body) {
/* function to update notes */
function updateNote(delNote,newTitle,newBody) {
chrome.storage.local.set({ [newTitle] : newBody }, function() {
var storingNote = browser.storage.local.set({ [newTitle] : newBody });
storingNote.then(() => {
if(delNote !== newTitle) {
chrome.storage.local.remove(delNote, function() {
var removingNote = browser.storage.local.remove(delNote);
removingNote.then(() => {
displayNote(newTitle, newBody);
});
}, onError);
} else {
displayNote(newTitle, newBody);
}
});
}, onError);
}
/* Clear all notes from the display/storage */
@@ -172,5 +173,5 @@ function clearAll() {
while (noteContainer.firstChild) {
noteContainer.removeChild(noteContainer.firstChild);
}
chrome.storage.local.clear();
}
browser.storage.local.clear();
}