From 8c50952bb542a09ebe591d9dba3b4d89d4837b54 Mon Sep 17 00:00:00 2001 From: Sukhbir Singh Date: Tue, 12 Dec 2017 02:59:34 -0500 Subject: [feat] Add support for calling bitmask.js from Firefox/Thunderbird - To be able to call bitmask.js from Thunderbird (for the Bitmask Thunderbird extension), we need to set `api_url' and also read the `authtoken' file from disk. This commit adds support for that and restricts the changes by using window.location.protocol. - Add function for fetching bitmaskd status. Signed-off-by: Ruben Pollan --- ui/app/lib/bitmask.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'ui/app') diff --git a/ui/app/lib/bitmask.js b/ui/app/lib/bitmask.js index 31a1e938..6ab87ff3 100644 --- a/ui/app/lib/bitmask.js +++ b/ui/app/lib/bitmask.js @@ -48,6 +48,29 @@ var bitmask = function(){ api_token = window.location.hash.replace('#', '') } + // If the script is running from a Firefox (or Thunderbird) extension, get + // the api_token from ~/.config/leap/authtoken, and also set the api_url. + if (window.location.protocol === "chrome:") { + // Use the correct URL for the API. + api_url = 'http://localhost:7070/API/'; + + // Now fetch the token file and set api_token. + Components.utils.import("resource://gre/modules/osfile.jsm") + + let tokenPath = OS.Path.join(OS.Constants.Path.homeDir, ".config", "leap", "authtoken") + let decoder = new TextDecoder(); + + setInterval(function get_token_file() { + let promise = OS.File.read(tokenPath); + promise = promise.then(array => { + api_token = decoder.decode(array); + }, ex => { + api_token = null; + }); + return get_token_file; + }(), 3000); + } + function call(command) { var url = api_url + command.slice(0, 3).join('/'); var data = JSON.stringify(command.slice(3)); @@ -131,6 +154,13 @@ var bitmask = function(){ */ stop: function() { return call(['core', 'stop']); + }, + + /** + * Get bitmaskd status + */ + status: function() { + return call(['core', 'status']); } }, -- cgit v1.2.3