jquery - Chrome Extension Content Script to access chrome.storage -
i've created custom menu appended page using chrome extensions content script. extension works great , menu appear wanted.
i want go little further , add options page on/off switch define var menutoggle either 1 or 0, , var attached if condition:
if menutoggle == 1 // else // console.log("var 0, menu not loaded");
i able make work on extension environment using "localstorage.menutoggle = 1;", understand work instance inside extension , content script not able access var.
i went through official docs chrome.storage api honestly, didn't understand since i'm on stage of programming.
from saw should use chrome.storage.sync.set this, didn't quite how set , either how retrieve data once inside content script. tried bellow, didn't work :
chrome.storage.sync.set({'menutoggle': '1'}, function(){});
question here: need define function after sync.set? possible store data without function after it?
bellow code used localstorage:
var varmenutoggledefault = 1; localstorage.varmenutoggle = 1; $("input:checkbox[name=onoffswitch]").change(function() { if ($(this).is(':checked')) { localstorage.varmenutoggle = varmenutoggledefault; console.log('switch on'); console.log("var is: " + localstorage.varmenutoggle); } else { localstorage.varmenutoggle = "0"; console.log('switch off'); console.log("var is: " + localstorage.varmenutoggle); } if (localstorage.varmenutoggle == 1) { // load menu } else { // console.log("varmenutoggle 0. menu not loaded"); };
what i'm doing wrong/missing?
chrome.storage
functions asynchronous. function after brackets called callback function, , fires when operation complete. don't have have callback function.
getting data bit different. can access data way:
chrome.storage.sync.get("menutoggle", function(data) { var menutoggle = data.menutoggle; }
because functions asynchronous, if want data back, must have callback function. callback function receives object has menutoggle
property, contains value set.
Comments
Post a Comment