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.
JavaScript
x
62
62
1
var channels;
2
var ny;
3
function endvpn() {
4
// turn off vpn
5
var config = {
6
mode: "pac_script",
7
pacScript: {
8
data: "function FindProxyForURL(url, host) {n" +
9
" if (host == 'google.com /*')n" +
10
" return 'PROXY blackhole:80';n" +
11
" return 'DIRECT';n" +
12
"}"
13
}
14
};
15
16
chrome.storage.local.set({ny: false});
17
// change vpn button
18
document.getElementById("vpnbtn").innerHTML = "Start Vpn";
19
// turn off event lisner for endvpn and start event listner to go back to startvpn
20
document.getElementById('vpnbtn').removeEventListener('click', endvpn, false);
21
document.getElementById("vpnbtn").addEventListener("click", startvpn);
22
23
24
25
}
26
function startvpn() {
27
// turn on vpn
28
29
30
31
var config = {
32
mode: "pac_script",
33
pacScript: {
34
data: "function FindProxyForURL(url, host) {n" +
35
" if (host == 'google.com /*')n" +
36
" return 'PROXY blackhole:80';n" +
37
" return 'PROXY 209.127.191.180:80';n" +
38
"}"
39
}
40
};
41
42
chrome.storage.local.set({ny: true});
43
44
45
// change vpn button
46
document.getElementById("vpnbtn").innerHTML = "Stop Vpn";
47
// turn off event lisner for startvpn and start event listner to go back to endvpn
48
document.getElementById('vpnbtn').removeEventListener('click', startvpn, false);
49
document.getElementById("vpnbtn").addEventListener("click", endvpn);
50
51
52
53
}
54
55
var rez = chrome.storage.local.get(ny);
56
alert(rez);
57
58
59
60
// start at startvpn
61
document.getElementById("vpnbtn").addEventListener("click", startvpn);
62
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:
JavaScript
1
4
1
chrome.storage.local.get('ny', function(result){
2
alert(result.ny);
3
});
4