gPass/chrome_addon/popup/popup.js

186 lines
4.4 KiB
JavaScript
Raw Normal View History

/*
Copyright (C) 2020 Grégory Soutadé
This file is part of gPass.
gPass is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
gPass is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with gPass. If not, see <http://www.gnu.org/licenses/>.
*/
var username_filled = false;
function _server_response(response, tabs, do_submit, force_copy)
{
debug("Get Response");
if (response.value == SERVER.OK)
{
/* Only copy */
if (document.getElementById("clipboard").checked)
{
navigator.clipboard.writeText(response.password).then(function() {
notify("Password pasted into clipboard", "");
window.close();
});
return true;
}
/* Fill + optional copy */
parameters = {
"type":"setPassword",
"password":response.password,
"submit":do_submit
};
send_tab_message(tabs[0].id, parameters,
function(arg)
{
debug("Response to setPassword " + arg);
if (arg === "" || force_copy)
{
navigator.clipboard.writeText(response.password).then(function() {
notify("Password pasted into clipboard", "");
window.close();
});
}
else
{
notify("Password filled", "");
window.close();
}
}
);
}
return true;
}
function _query_tabs_get_password(tabs)
{
var force_copy = false;
if (tabs.length <= 0) return;
var username = document.getElementById("gPassUsername").value;
var mkey = document.getElementById("gPassMasterKey").value;
if (username == "" || mkey == "")
return;
document.getElementById("gPassMasterKey").value = "";
var do_submit = !mkey.startsWith("@_") && username_filled;
if (mkey.startsWith("@_"))
force_copy = true;
if (mkey.startsWith("@@") || mkey.startsWith("@_"))
mkey = mkey.substring(2);
var domain = tabs[0].url;
var logins = new Array();
logins.push(username);
parameters = {
type:"password",
logins:logins,
domain:domain,
mkey:mkey,
options:{}
};
browser.runtime.sendMessage(parameters, {},
function (response)
{
return _server_response(response, tabs, do_submit, force_copy);
}
);
return true;
}
function get_password(evt)
{
debug('get_password');
evt.preventDefault();
browser.tabs.query({active:true, currentWindow:true}, _query_tabs_get_password);
return false;
}
pform = document.getElementById("passwordForm");
if (pform != null)
pform.onsubmit = get_password;
function _query_tabs_init(tabs)
{
debug("_query_tabs_init");
if (tabs.length != 1) return;
/* Fill username */
parameters = {
"type":"getUsername"
};
send_tab_message(tabs[0].id, parameters,
function (response)
{
if (response !== undefined && response != "")
{
document.getElementById("gPassUsername").value = response;
document.getElementById("gPassMasterKey").focus();
username_filled = true;
}
});
/* Clipboard copy */
parameters = {
"type":"getPopupClipboard"
};
browser.runtime.sendMessage(parameters, {},
function (response)
{
document.getElementById("clipboard").checked = response.value;
return true;
});
/* Setup server link address */
parameters = {
"type":"getServerAddress"
};
browser.runtime.sendMessage(parameters, {},
function (response)
{
url = response.value;
url = url.substring(0, url.lastIndexOf('/'));
url += '?';
url += 'url=' + encodeURI(tabs[0].url.split("?")[0]);
url += '&user=' + document.getElementById("gPassUsername").value;
link = document.getElementById("serverLink");
link.href = url;
return true;
});
return true;
}
browser.tabs.query({active:true, currentWindow:true}, _query_tabs_init);