Add toggle functionality to web extension

This commit is contained in:
Cecylia Bocovich 2019-06-26 20:21:44 -04:00
parent ead579a6e9
commit 799a13d385
5 changed files with 37 additions and 3 deletions

View file

@ -4,6 +4,9 @@ Entry point.
debug = false debug = false
snowflake = null snowflake = null
config = null
broker = null
ui = null
# Log to both console and UI if applicable. # Log to both console and UI if applicable.
# Requires that the snowflake and UI objects are hooked up in order to # Requires that the snowflake and UI objects are hooked up in order to
@ -21,8 +24,17 @@ init = () ->
snowflake = new Snowflake config, ui, broker snowflake = new Snowflake config, ui, broker
log '== snowflake proxy ==' log '== snowflake proxy =='
update = () ->
if !ui.enabled
# Do not activate the proxy if any number of conditions are true.
snowflake.disable()
log 'Currently not active.'
return
# Otherwise, begin setting up WebRTC and acting as a proxy. # Otherwise, begin setting up WebRTC and acting as a proxy.
dbg 'Contacting Broker at ' + broker.url dbg 'Contacting Broker at ' + broker.url
log 'Starting snowflake'
snowflake.setRelayAddr config.relayAddr snowflake.setRelayAddr config.relayAddr
snowflake.beginWebRTC() snowflake.beginWebRTC()

View file

@ -4,6 +4,7 @@ All of Snowflake's DOM manipulation and inputs.
class UI class UI
active: false active: false
enabled: false
setStatus: (msg) -> setStatus: (msg) ->
@ -74,10 +75,17 @@ class WebExtUI extends UI
total: @stats.reduce ((t, c) -> total: @stats.reduce ((t, c) ->
t + c t + c
), 0 ), 0
enabled: @enabled
onConnect: (port) => onConnect: (port) =>
@port = port @port = port
port.onDisconnect.addListener @onDisconnect port.onDisconnect.addListener @onDisconnect
port.onMessage.addListener @onMessage
@postActive()
onMessage: (m) =>
@enabled = m.enabled
update()
@postActive() @postActive()
onDisconnect: (port) => onDisconnect: (port) =>

View file

@ -13,5 +13,6 @@
}, },
"default_title": "Snowflake", "default_title": "Snowflake",
"default_popup": "popup.html" "default_popup": "popup.html"
} },
"permissions": ["cookies", "https://snowflake.torproject.org/"]
} }

View file

@ -12,9 +12,9 @@
<p></p> <p></p>
</div> </div>
<div class="b toggle"> <div class="b toggle">
<label>Turn Off</label> <label id=toggle>Turn On</label>
<label class="switch"> <label class="switch">
<input type="checkbox" checked> <input id="enabled" type="checkbox"/>
<span class="slider round"></span> <span class="slider round"></span>
</label> </label>
</div> </div>

View file

@ -11,4 +11,17 @@ port.onMessage.addListener((m) => {
const clients = active ? 1 : 0; const clients = active ? 1 : 0;
ps[0].innerText = `${clients} client${(clients !== 1) ? 's' : ''} connected.`; ps[0].innerText = `${clients} client${(clients !== 1) ? 's' : ''} connected.`;
ps[1].innerText = `Your snowflake has helped ${m.total} user${(m.total !== 1) ? 's' : ''} circumvent censorship in the last 24 hours.`; ps[1].innerText = `Your snowflake has helped ${m.total} user${(m.total !== 1) ? 's' : ''} circumvent censorship in the last 24 hours.`;
const enabled = m.enabled
const enabledText = document.getElementById('toggle');
if (enabled) {
document.getElementById('enabled').checked = true;
enabledText.innerText = 'Turn Off';
} else {
document.getElementById('enabled').checked = false;
enabledText.innerText = 'Turn On';
}
}); });
document.addEventListener('change', (event) => {
port.postMessage({enabled: event.target.checked});
})