Skip to content

Commit

Permalink
Start on popup
Browse files Browse the repository at this point in the history
  • Loading branch information
arlolra committed May 30, 2019
1 parent cd650fa commit 7989810
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 16 deletions.
12 changes: 6 additions & 6 deletions proxy/proxypair.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ class ProxyPair
channel.onopen = =>
log 'WebRTC DataChannel opened!'
snowflake.state = Snowflake.MODE.WEBRTC_READY
snowflake.ui?.setActive true
snowflake.ui.setActive true
# This is the point when the WebRTC datachannel is done, so the next step
# is to establish websocket to the server.
@connectRelay()
channel.onclose = =>
log 'WebRTC DataChannel closed.'
snowflake.ui?.setStatus 'disconnected by webrtc.'
snowflake.ui?.setActive false
snowflake.ui.setStatus 'disconnected by webrtc.'
snowflake.ui.setActive false
snowflake.state = Snowflake.MODE.INIT
@flush()
@close()
Expand Down Expand Up @@ -108,11 +108,11 @@ class ProxyPair
clearTimeout @timer
@timer = 0
log @relay.label + ' connected!'
snowflake.ui?.setStatus 'connected'
snowflake.ui.setStatus 'connected'
@relay.onclose = =>
log @relay.label + ' closed.'
snowflake.ui?.setStatus 'disconnected.'
snowflake.ui?.setActive false
snowflake.ui.setStatus 'disconnected.'
snowflake.ui.setActive false
snowflake.state = Snowflake.MODE.INIT
@flush()
@close()
Expand Down
40 changes: 31 additions & 9 deletions proxy/ui.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ All of Snowflake's DOM manipulation and inputs.
###

class UI
active: false

setStatus: (msg) ->

setActive: (connected) ->
@active = connected

log: (msg) ->

Expand All @@ -16,7 +19,8 @@ class BadgeUI extends UI
constructor: ->
@$badge = document.getElementById('badge')

setActive: (connected) =>
setActive: (connected) ->
super connected
@$badge.className = if connected then 'active' else ''


Expand All @@ -32,22 +36,40 @@ class DebugUI extends UI
@$msglog.value = ''

# Status bar
setStatus: (msg) =>
setStatus: (msg) ->
@$status.innerHTML = 'Status: ' + msg

setActive: (connected) =>
setActive: (connected) ->
super connected
@$msglog.className = if connected then 'active' else ''

log: (msg) =>
log: (msg) ->
# Scroll to latest
@$msglog.value += msg + '\n'
@$msglog.scrollTop = @$msglog.scrollHeight


class WebExtUI extends UI
port: null

constructor: ->
chrome.runtime.onConnect.addListener @onConnect

postActive: ->
@port?.postMessage
active: @active

onConnect: (port) =>
@port = port
port.onDisconnect.addListener @onDisconnect
@postActive()

onDisconnect: (port) =>
@port = null

setActive: (connected) ->
chrome.browserAction.setIcon {
"path": {
"32": "icons/status-" + (if connected then "on" else "off") + ".png"
}
}
super connected
@postActive()
chrome.browserAction.setIcon
path:
32: "icons/status-" + (if connected then "on" else "off") + ".png"
3 changes: 2 additions & 1 deletion proxy/webext/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"default_icon": {
"32": "icons/status-off.png"
},
"default_title": "Snowflake"
"default_title": "Snowflake",
"default_popup": "popup.html"
}
}
19 changes: 19 additions & 0 deletions proxy/webext/popup.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
body {
margin: 1em;
}

#active {
padding: 2em;
text-align: center;
}

.learn {
padding-top: 1em;
border-top: 1px solid grey;
}

.learn a {
display: block;
color: grey;
text-decoration: none;
}
17 changes: 17 additions & 0 deletions proxy/webext/popup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="popup.css" />
<script src="popup.js"></script>
</head>
<body>
<div id="active">
<img src="icons/status-off.png" />
<p>Offline</p>
</div>
<div class="learn">
<a target="_blank" href="https://snowflake.torproject.org/">Learn more</a>
</div>
</body>
</html>
12 changes: 12 additions & 0 deletions proxy/webext/popup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const port = chrome.runtime.connect({
name: "popup"
});

port.onMessage.addListener((m) => {
const active = m.active;
const div = document.getElementById('active');
const img = div.querySelector('img');
img.src = `icons/status-${active ? "on" : "off"}.png`;
const p = div.querySelector('p');
p.innerText = active ? "Connected" : "Offline";
});

0 comments on commit 7989810

Please sign in to comment.