From 04668bcaed6ebdb5094c9be8ceb3e5ee489e7234 Mon Sep 17 00:00:00 2001 From: PressJump <40802367+PressJump@users.noreply.github.com> Date: Thu, 29 Sep 2022 15:36:07 +1000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Remove=20Sidebar=20Items?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- js/background.js | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) 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); })