本地存储中的保存不起作用
Saving in local storage not working
我正在构建一个现有的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()
谢谢黄嘌呤!
相关文章:
- 保存HTML文件后,Javascript不起作用
- jqGrid-保存内联编辑不起作用
- Javascript:保存和恢复样式状态在IE中不起作用
- 将查询选择器保存在要追加的变量中不起作用
- 为什么我的保存时间功能不起作用?PHP + AJAX JS.
- 解析之前保存()云代码函数不起作用
- 本地存储:记录和保存,不起作用
- 尝试将 PHP $_POST 变量保存到 javascript 中,但它不起作用
- 保存图像 src 的功能不起作用
- 在Javascript中保存和加载不起作用
- PhoneGap - 请告知保存功能不起作用的原因
- 加载保存文本区域不起作用,如图所示
- 角度将变量保存到带有 ajax 调用的 laravel 会话不起作用
- 预防保存弹出窗口的默认在 chrome 中工作,但在火狐中不起作用
- 如何在选择选项后立即从选择菜单中将值保存到本地存储?它不起作用
- 保存 URI 不起作用
- Angular ui路由器在服务中保存到状态名称不起作用
- 在使用具有隔离作用域的指令时保存模型值不起作用
- 编辑和保存不起作用
- 本地存储中的保存不起作用