簡體   English   中英

Chrome 擴展存儲刪除不起作用

[英]Chrome extension storage remove not working

我正在構建 chrome 擴展程序,但 chrome.storage.sync.remove 有問題

假設這是我的 chrome.storage 中的內容:

我的鉻存儲

這包含我要刪除的項目 (removedItems[])

移除物品[]

這是我的代碼:

chrome.storage.sync.get(null, function(data) {
    coasterList = data;
    console.log('FFFFFFFF :',coasterList.data);
    chrome.storage.sync.remove(removedItems[0].CoasterName, function(data) {
      chrome.storage.sync.get(null, function(data) {
        var coasterListFINAL = data;
        console.log('FINAL LIST :',coasterListFINAL.data);
        //console.log(removedItems[0].CoasterName);
      });
    });
  });

當我這樣做時什么都沒有發生:

chrome.storage.sync.remove(removedItems[0].CoasterName, function(data) {...}

我究竟做錯了什么? (我沒有錯誤,但我要刪除的密鑰仍然在這里)

您不能刪除存儲在 chrome.storage 中的單個數組項。 您必須將整個數組替換為您之前刪除該項目的新數組。

您的代碼:

    chrome.storage.sync.set({'CoasterList':coasterListClean}, function() {
        console.log("SAVED");
    });

/*
if you try to retrieve the newly set storage var "CoasterList" right after setting it this way
you will probably get the old value 'cause you are reading something that is not changed yet
ALL CHROME.STORAGE APIS ARE ASYNCHRONOUS!!!
*/
    chrome.storage.sync.get(null, function(data) {
        console.log(data.coasterList);
    });

----------------------------------
/*
if you want to retrieve the new value of CoasterList you have
to get it inside the storage.sync.set callback function.
if you are under MV3 rules you can also use the "promise returned" syntax
THIS SHOUL WORK
*/
    chrome.storage.sync.set({'CoasterList':coasterListClean}, function() {
        console.log("SAVED");
        chrome.storage.sync.get(null, function(data) {
            console.log(data.coasterList);
        });
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM