Close over init so that we can return if a feature isn't detected

I guess alternatively, just use an if/else block.
This commit is contained in:
Arlo Breault 2019-07-06 16:40:03 +02:00
parent f97c37fe5d
commit 27a92ab03a
2 changed files with 130 additions and 121 deletions

View file

@ -2,34 +2,38 @@
Entry point. Entry point.
*/ */
if (((typeof TESTING === "undefined" || TESTING === null) || !TESTING) && !Util.featureDetect()) { var snowflake, query, debug, silenceNotifications, log, dbg, init;
;(function() {
if (((typeof TESTING === "undefined" || TESTING === null) || !TESTING) && !Util.featureDetect()) {
console.log('webrtc feature not detected. shutting down'); console.log('webrtc feature not detected. shutting down');
return; return;
} }
var snowflake = null; snowflake = null;
var query = Query.parse(location); query = Query.parse(location);
var debug = Params.getBool(query, 'debug', false); debug = Params.getBool(query, 'debug', false);
var silenceNotifications = Params.getBool(query, 'silent', false); silenceNotifications = Params.getBool(query, 'silent', false);
// 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
// log to console. // log to console.
var log = function(msg) { log = function(msg) {
console.log('Snowflake: ' + msg); console.log('Snowflake: ' + msg);
return snowflake != null ? snowflake.ui.log(msg) : void 0; return snowflake != null ? snowflake.ui.log(msg) : void 0;
}; };
var dbg = function(msg) { dbg = function(msg) {
if (debug || ((snowflake != null ? snowflake.ui : void 0) instanceof DebugUI)) { if (debug || ((snowflake != null ? snowflake.ui : void 0) instanceof DebugUI)) {
return log(msg); return log(msg);
} }
}; };
var init = function() { init = function() {
var broker, config, ui; var broker, config, ui;
config = new Config; config = new Config;
if ('off' !== query['ratelimit']) { if ('off' !== query['ratelimit']) {
@ -55,19 +59,21 @@ var init = function() {
dbg('Contacting Broker at ' + broker.url); dbg('Contacting Broker at ' + broker.url);
snowflake.setRelayAddr(config.relayAddr); snowflake.setRelayAddr(config.relayAddr);
return snowflake.beginWebRTC(); return snowflake.beginWebRTC();
}; };
// Notification of closing tab with active proxy. // Notification of closing tab with active proxy.
window.onbeforeunload = function() { window.onbeforeunload = function() {
if (!silenceNotifications && Snowflake.MODE.WEBRTC_READY === snowflake.state) { if (!silenceNotifications && Snowflake.MODE.WEBRTC_READY === snowflake.state) {
return Snowflake.MESSAGE.CONFIRMATION; return Snowflake.MESSAGE.CONFIRMATION;
} }
return null; return null;
}; };
window.onunload = function() { window.onunload = function() {
snowflake.disable(); snowflake.disable();
return null; return null;
}; };
window.onload = init; window.onload = init;
}());

View file

@ -2,49 +2,50 @@
Entry point. Entry point.
*/ */
var debug = false; var debug, snowflake, config, broker, ui, log, dbg, init, update, silenceNotifications;
var snowflake = null; ;(function () {
var config = null; silenceNotifications = false;
debug = false;
snowflake = null;
config = null;
broker = null;
ui = null;
var broker = null; // Log to both console and UI if applicable.
// Requires that the snowflake and UI objects are hooked up in order to
var ui = null; // log to console.
log = function(msg) {
// Log to both console and UI if applicable.
// Requires that the snowflake and UI objects are hooked up in order to
// log to console.
var log = function(msg) {
console.log('Snowflake: ' + msg); console.log('Snowflake: ' + msg);
return snowflake != null ? snowflake.ui.log(msg) : void 0; return snowflake != null ? snowflake.ui.log(msg) : void 0;
}; };
var dbg = function(msg) { dbg = function(msg) {
if (debug) { if (debug) {
return log(msg); return log(msg);
} }
}; };
if (!Util.featureDetect()) { if (!Util.featureDetect()) {
chrome.runtime.onConnect.addListener(function(port) { chrome.runtime.onConnect.addListener(function(port) {
return port.postMessage({ return port.postMessage({
missingFeature: true missingFeature: true
}); });
}); });
return; return;
} }
var init = function() { init = function() {
config = new Config; config = new Config;
ui = new WebExtUI(); ui = new WebExtUI();
broker = new Broker(config.brokerUrl); broker = new Broker(config.brokerUrl);
snowflake = new Snowflake(config, ui, broker); snowflake = new Snowflake(config, ui, broker);
log('== snowflake proxy =='); log('== snowflake proxy ==');
return ui.initToggle(); return ui.initToggle();
}; };
var update = function() { update = function() {
if (!ui.enabled) { if (!ui.enabled) {
// Do not activate the proxy if any number of conditions are true. // Do not activate the proxy if any number of conditions are true.
snowflake.disable(); snowflake.disable();
@ -56,19 +57,21 @@ var update = function() {
log('Starting snowflake'); log('Starting snowflake');
snowflake.setRelayAddr(config.relayAddr); snowflake.setRelayAddr(config.relayAddr);
return snowflake.beginWebRTC(); return snowflake.beginWebRTC();
}; };
// Notification of closing tab with active proxy. // Notification of closing tab with active proxy.
window.onbeforeunload = function() { window.onbeforeunload = function() {
if (!silenceNotifications && Snowflake.MODE.WEBRTC_READY === snowflake.state) { if (!silenceNotifications && Snowflake.MODE.WEBRTC_READY === snowflake.state) {
return Snowflake.MESSAGE.CONFIRMATION; return Snowflake.MESSAGE.CONFIRMATION;
} }
return null; return null;
}; };
window.onunload = function() { window.onunload = function() {
snowflake.disable(); snowflake.disable();
return null; return null;
}; };
window.onload = init; window.onload = init;
}());