本地存储中的保存不起作用

Saving in local storage not working

本文关键字:保存 不起作用 存储      更新时间:2023-09-26

我正在构建一个现有的chrome扩展,我试图保持一致的风格。我需要添加一个新功能,我使用下面的脚本从弹出窗口中保存用户的选择,然后根据保存的选择设置一个新的弹出窗口。

userchoices.js:

require.scopes["userchoices"] = (function() {
var exports = {};
var userChoices = exports.userChoices = {
  userchoices: {},
  updateChoice: function(){
    self = this;
    chrome.storage.local.get('userchoices', function(items){
      if(!items.userchoices){
        chrome.storage.local.set({userchoices: self.userchoices});
        return;
      }
      self.userchoices = items.userchoices;
    });
  },
  getChoice: function(url){
    if(this.userchoices[url]){
      return this.userchoices[url][choice];
    } else {
      return {};
    }
  },
  setChoice: function(url, newChoice){
    if(!this.userchoices[url]){
      this.userchoices[url] = {};
    }
    this.userchoices[url][choice] = newChoice;
    chrome.storage.local.set({userchoices: this.userchoices});
  },
  removeChoice: function(url){
    if(!this.userchoices[url]){
      return;
    } else {
      delete this.userchoices[url]
    }
    chrome.storage.local.set({userchoices: this.userchoices});
  }
}
return exports; 
})(); 

background.js:

var userChoices= require("userchoices").userChoices;
chrome.windows.onCreated.addListener(function(){
  CookieBlockList.updateDomains();
  BlockedDomainList.updateDomains();
  FakeCookieStore.updateCookies();
  userChoices.updateChoice();
});
function refreshIconAndContextMenu(tab)
{
  // The tab could have been closed by the time this function is called
  if(!tab)
    return;
  var choice = userChoices.getChoice(tab.url);
  if(choice) {
    if (choice == "one"){
        chrome.browserAction.setPopup({tabId: tab.id, popup: "skin/popupDontCare.html"});
    } else if(choice=="two"){
        chrome.browserAction.setPopup({tabId: tab.id, popup: "skin/popupSortofCare.html"});
    } else if(choice=="three") {
        chrome.browserAction.setPopup({tabId: tab.id, popup: "skin/popupCare.html"});
    } else if(choice=="four") {
        chrome.browserAction.setPopup({tabId: tab.id, popup: "skin/popupReallyCare.html"});
    } else {
        chrome.browserAction.setPopup({tabId: tab.id, popup: "skin/popup.html"});
    }}
}
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
  if(changeInfo.status == "loading")
    refreshIconAndContextMenu(tab);
});
// Update icon if a tab is replaced or loaded from cache
chrome.tabs.onReplaced.addListener(function(addedTabId, removedTabId){
  chrome.tabs.get(addedTabId, function(tab){
    refreshIconAndContextMenu(tab);
  });
});

popup.js:

var userChoices = require("userchoices").userChoices;
function init()
{
  console.log("Initializing popup.js");
  // Attach event listeners
  $("#Dont_Care_btn").click(doNothing);
  $("#Sort_of_Care_btn").click(doBadger);
  $("#Care_btn").click(giveSecrecyBadger);
  $("#Really_Care_btn").click(giveAdvice);
  $("#Nuance_btn").click(addNuance);
}
function doNothing() {
  $("#startingQuestion").hide();
  $("#DontCareResponse").show();
  $("#siteControls").hide();
  userChoices.setChoice(tab.url, "one");
  refreshIconAndContextMenu(tab);
 }
function doBadger() {
  $("#startingQuestion").hide();
  $("#SortofCareResponse").show();
  $("#siteControls").hide();
  $("#blockedResourcesContainer").hide();
  $("#Nuance_btn").show();
  userChoices.setChoice(tab.url, "two");
  refreshIconAndContextMenu(tab);
 }
function giveSecrecyBadger() {
  $("#startingQuestion").hide();
  $("#siteControls").hide();
  $("#CareResponse").show();
  $("#siteControls").hide();
  $("#blockedResourcesContainer").hide();
  $("#Nuance_btn").show();
  userChoices.setChoice(tab.url, "three");
  refreshIconAndContextMenu(tab);
}
function giveAdvice() {
    $("#startingQuestion").hide();
    $("#siteControls").hide();
    $("#ReallyCareResponse").show();
    userChoices.setChoice(tab.url, "four");
    refreshIconAndContextMenu(tab);
}

当前没有设置弹出窗口,我甚至不确定选择是否成功保存。有人发现问题了吗?

哈哈!在尝试创建一个最小示例的过程中,我发现了问题所在。原来问题是现在已弃用的chrome.tabs.getSelected方法,而它应该是chrome.tabs.query()

谢谢黄嘌呤!