diff --git a/js/background.js b/js/background.js index 2144bc3..19c07ec 100644 --- a/js/background.js +++ b/js/background.js @@ -5,24 +5,43 @@ browser.sidebars.add({ isBottom: true, }) -getFromStorage(); +getFromStorage().then(createSidebarItems); async function getFromStorage() { //get from storage let sidebaritems = await browser.storage.local.get("sidebaritems"); - if (sidebaritems.sidebaritems == undefined) { - sidebaritems.sidebaritems = []; - } + console.log(sidebaritems); + return sidebaritems; +} + +async function createSidebarItems(sidebaritems) { //create sidebar items for (let i = 0; i < sidebaritems.sidebaritems.length; i++) { - browser.sidebars.add({ - title: sidebaritems.sidebaritems[i].title, - iconUrl: sidebaritems.sidebaritems[i].iconUrl, - webviewUrl: sidebaritems.sidebaritems[i].webviewUrl, - }); + browser.sidebars.add({ + title: sidebaritems.sidebaritems[i].title, + iconUrl: sidebaritems.sidebaritems[i].iconUrl, + webviewUrl: sidebaritems.sidebaritems[i].webviewUrl, + }); } } +async function removeSidebarItems(itemId) +{ + const item = await browser.sidebars.get(itemId); + + //remove from storage + let storagearray = await getFromStorage(); + let storageitems = storagearray.sidebaritems; + + for (let i = 0; i < storageitems.length; i++) { + if (storageitems[i].webviewUrl == item.webviewUrl) { + storageitems.splice(i, 1); + } + } + + browser.storage.local.set(storagearray); +} + browser.sidebars.onRemove.addListener((itemId) => { - console.log(itemId); + removeSidebarItems(itemId); })