| 1 |
//------------------------------------- Initialisation --------------------------------------// |
| 2 |
"use strict"; |
| 3 |
|
| 4 |
require("chrome-extension-async"); |
| 5 |
const Interface = require("./interface"); |
| 6 |
const DetailsInterface = require("./detailsInterface"); |
| 7 |
const helpers = require("../helpers"); |
| 8 |
const m = require("mithril"); |
| 9 |
|
| 10 |
run(); |
| 11 |
|
| 12 |
//----------------------------------- Function definitions ----------------------------------// |
| 13 |
|
| 14 |
/** |
| 15 |
* Handle an error |
| 16 |
* |
| 17 |
* @since 3.0.0 |
| 18 |
* |
| 19 |
* @param Error error Error object |
| 20 |
* @param string type Error type |
| 21 |
*/ |
| 22 |
function handleError(error, type = "error") { |
| 23 |
if (type == "error") { |
| 24 |
console.log(error); |
| 25 |
} |
| 26 |
var node = { view: () => m(`div.part.${type}`, error.toString()) }; |
| 27 |
m.mount(document.body, node); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Run the main popup logic |
| 32 |
* |
| 33 |
* @since 3.0.0 |
| 34 |
* |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
async function run() { |
| 38 |
try { |
| 39 |
var response = await chrome.runtime.sendMessage({ action: "getSettings" }); |
| 40 |
if (response.status != "ok") { |
| 41 |
throw new Error(response.message); |
| 42 |
} |
| 43 |
var settings = response.settings; |
| 44 |
|
| 45 |
var root = document.getElementsByTagName("html")[0]; |
| 46 |
root.classList.remove("colors-dark"); |
| 47 |
root.classList.add(`colors-${settings.theme}`); |
| 48 |
|
| 49 |
if (settings.hasOwnProperty("hostError")) { |
| 50 |
throw new Error(settings.hostError.params.message); |
| 51 |
} |
| 52 |
|
| 53 |
if (typeof settings.origin === "undefined") { |
| 54 |
throw new Error("Unable to retrieve current tab information"); |
| 55 |
} |
| 56 |
|
| 57 |
// get list of logins |
| 58 |
response = await chrome.runtime.sendMessage({ action: "listFiles" }); |
| 59 |
if (response.status != "ok") { |
| 60 |
throw new Error(response.message); |
| 61 |
} |
| 62 |
|
| 63 |
const logins = helpers.prepareLogins(response.files, settings); |
| 64 |
for (let login of logins) { |
| 65 |
login.doAction = withLogin.bind({ settings: settings, login: login }); |
| 66 |
} |
| 67 |
|
| 68 |
var popup = new Interface(settings, logins); |
| 69 |
popup.attach(document.body); |
| 70 |
} catch (e) { |
| 71 |
handleError(e); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Do a login action |
| 77 |
* |
| 78 |
* @since 3.0.0 |
| 79 |
* |
| 80 |
* @param string action Action to take |
| 81 |
* @return void |
| 82 |
*/ |
| 83 |
async function withLogin(action) { |
| 84 |
try { |
| 85 |
// replace popup with a "please wait" notice |
| 86 |
switch (action) { |
| 87 |
case "fill": |
| 88 |
handleError("Filling login details...", "notice"); |
| 89 |
break; |
| 90 |
case "launch": |
| 91 |
handleError("Launching URL...", "notice"); |
| 92 |
break; |
| 93 |
case "launchInNewTab": |
| 94 |
handleError("Launching URL in a new tab...", "notice"); |
| 95 |
break; |
| 96 |
case "copyPassword": |
| 97 |
handleError("Copying password to clipboard...", "notice"); |
| 98 |
break; |
| 99 |
case "copyUsername": |
| 100 |
handleError("Copying username to clipboard...", "notice"); |
| 101 |
break; |
| 102 |
case "copyOTP": |
| 103 |
handleError("Copying OTP token to clipboard...", "notice"); |
| 104 |
break; |
| 105 |
case "getDetails": |
| 106 |
handleError("Loading entry details...", "notice"); |
| 107 |
break; |
| 108 |
default: |
| 109 |
handleError("Please wait...", "notice"); |
| 110 |
break; |
| 111 |
} |
| 112 |
|
| 113 |
// Firefox requires data to be serializable, |
| 114 |
// this removes everything offending such as functions |
| 115 |
const login = JSON.parse(JSON.stringify(this.login)); |
| 116 |
|
| 117 |
// hand off action to background script |
| 118 |
var response = await chrome.runtime.sendMessage({ |
| 119 |
action: action, |
| 120 |
login: login, |
| 121 |
}); |
| 122 |
if (response.status != "ok") { |
| 123 |
throw new Error(response.message); |
| 124 |
} else { |
| 125 |
if (response.login && typeof response.login === "object") { |
| 126 |
response.login.doAction = withLogin.bind({ |
| 127 |
settings: this.settings, |
| 128 |
login: response.login, |
| 129 |
}); |
| 130 |
} |
| 131 |
if (action === "getDetails") { |
| 132 |
var details = new DetailsInterface(this.settings, response.login); |
| 133 |
details.attach(document.body); |
| 134 |
} else { |
| 135 |
window.close(); |
| 136 |
} |
| 137 |
} |
| 138 |
} catch (e) { |
| 139 |
handleError(e); |
| 140 |
} |
| 141 |
} |