I am trying to get my proxy chrome extention to keep on/off after closing using chrome.local.storage. This does not seem to work, can anyone give some examples on how to get this kind of code working?
Right now my pac proxy works and turns on and off. The local storage does not seem to work at all, but I followed all the examples on the developer.chrome website. That does not work.
var channels; var ny; function endvpn() { // turn off vpn var config = { mode: "pac_script", pacScript: { data: "function FindProxyForURL(url, host) {n" + " if (host == 'google.com /*')n" + " return 'PROXY blackhole:80';n" + " return 'DIRECT';n" + "}" } }; chrome.storage.local.set({ny: false}); // change vpn button document.getElementById("vpnbtn").innerHTML = "Start Vpn"; // turn off event lisner for endvpn and start event listner to go back to startvpn document.getElementById('vpnbtn').removeEventListener('click', endvpn, false); document.getElementById("vpnbtn").addEventListener("click", startvpn); } function startvpn() { // turn on vpn var config = { mode: "pac_script", pacScript: { data: "function FindProxyForURL(url, host) {n" + " if (host == 'google.com /*')n" + " return 'PROXY blackhole:80';n" + " return 'PROXY 209.127.191.180:80';n" + "}" } }; chrome.storage.local.set({ny: true}); // change vpn button document.getElementById("vpnbtn").innerHTML = "Stop Vpn"; // turn off event lisner for startvpn and start event listner to go back to endvpn document.getElementById('vpnbtn').removeEventListener('click', startvpn, false); document.getElementById("vpnbtn").addEventListener("click", endvpn); } var rez = chrome.storage.local.get(ny); alert(rez); // start at startvpn document.getElementById("vpnbtn").addEventListener("click", startvpn);
Advertisement
Answer
Most apis within Chrome extensions are asyncronous. See the documentation here. You can provide a callback as the second argument to the ‘get’ function where you can use the variable:
chrome.storage.local.get('ny', function(result){ alert(result.ny); });