2020-01-18 15:20:13 +01:00
|
|
|
/*
|
2022-09-26 20:30:40 +02:00
|
|
|
Copyright (C) 2020-2022 Grégory Soutadé
|
2020-01-18 15:20:13 +01:00
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2022-09-26 20:30:40 +02:00
|
|
|
import {SERVER, browser, notify, debug} from "./misc.js";
|
|
|
|
import {get_preference, send_tab_message} from "./compat.js";
|
|
|
|
|
2021-03-02 16:54:24 +01:00
|
|
|
var username_filled = false;
|
|
|
|
|
|
|
|
function _server_response(response, tabs, do_submit, force_copy)
|
2022-09-26 20:30:40 +02:00
|
|
|
{
|
2021-03-02 16:54:24 +01:00
|
|
|
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", "");
|
2022-09-26 20:30:40 +02:00
|
|
|
window.setTimeout(function() {window.close();}, 2000);
|
2021-03-02 16:54:24 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill + optional copy */
|
2022-09-26 20:30:40 +02:00
|
|
|
var parameters = {
|
2021-03-02 16:54:24 +01:00
|
|
|
"type":"setPassword",
|
|
|
|
"password":response.password,
|
2022-09-26 20:30:40 +02:00
|
|
|
"submit":do_submit,
|
2021-03-02 16:54:24 +01:00
|
|
|
};
|
|
|
|
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", "");
|
2022-09-26 20:30:40 +02:00
|
|
|
window.setTimeout(function() {window.close();}, 2000);
|
2021-03-02 16:54:24 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
notify("Password filled", "");
|
2022-09-26 20:30:40 +02:00
|
|
|
window.setTimeout(function() {window.close();}, 2000);
|
2021-03-02 16:54:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2020-01-18 15:20:13 +01:00
|
|
|
|
|
|
|
function _query_tabs_get_password(tabs)
|
|
|
|
{
|
2020-08-30 12:03:20 +02:00
|
|
|
var force_copy = false;
|
|
|
|
|
2020-01-18 15:20:13 +01:00
|
|
|
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;
|
2020-08-30 12:03:20 +02:00
|
|
|
|
|
|
|
if (mkey.startsWith("@_"))
|
|
|
|
force_copy = true;
|
|
|
|
|
2020-01-18 15:20:13 +01:00
|
|
|
if (mkey.startsWith("@@") || mkey.startsWith("@_"))
|
|
|
|
mkey = mkey.substring(2);
|
|
|
|
|
|
|
|
var domain = tabs[0].url;
|
|
|
|
|
|
|
|
var logins = new Array();
|
|
|
|
logins.push(username);
|
|
|
|
|
2022-09-26 20:30:40 +02:00
|
|
|
var parameters = {
|
2020-01-18 15:20:13 +01:00
|
|
|
type:"password",
|
|
|
|
logins:logins,
|
|
|
|
domain:domain,
|
|
|
|
mkey:mkey,
|
|
|
|
options:{}
|
|
|
|
};
|
|
|
|
|
|
|
|
browser.runtime.sendMessage(parameters, {},
|
2021-03-02 16:54:24 +01:00
|
|
|
function (response)
|
|
|
|
{
|
|
|
|
return _server_response(response, tabs, do_submit, force_copy);
|
|
|
|
}
|
|
|
|
);
|
2020-01-18 15:20:13 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_password(evt)
|
|
|
|
{
|
|
|
|
debug('get_password');
|
|
|
|
|
|
|
|
evt.preventDefault();
|
|
|
|
|
2022-09-26 20:30:40 +02:00
|
|
|
browser.tabs.query({active:true, currentWindow:true}).then(_query_tabs_get_password);
|
2020-01-18 15:20:13 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-26 20:30:40 +02:00
|
|
|
var pform = document.getElementById("passwordForm");
|
2020-01-18 15:20:13 +01:00
|
|
|
|
|
|
|
if (pform != null)
|
|
|
|
pform.onsubmit = get_password;
|
|
|
|
|
|
|
|
function _query_tabs_init(tabs)
|
|
|
|
{
|
|
|
|
debug("_query_tabs_init");
|
|
|
|
|
|
|
|
if (tabs.length != 1) return;
|
|
|
|
|
|
|
|
/* Fill username */
|
2022-09-26 20:30:40 +02:00
|
|
|
var parameters = {
|
2020-01-18 15:20:13 +01:00
|
|
|
"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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-02 16:54:24 +01:00
|
|
|
/* Clipboard copy */
|
|
|
|
parameters = {
|
|
|
|
"type":"getPopupClipboard"
|
|
|
|
};
|
|
|
|
|
|
|
|
browser.runtime.sendMessage(parameters, {},
|
|
|
|
function (response)
|
|
|
|
{
|
|
|
|
document.getElementById("clipboard").checked = response.value;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2020-01-18 15:20:13 +01:00
|
|
|
/* Setup server link address */
|
|
|
|
parameters = {
|
2021-03-02 16:54:24 +01:00
|
|
|
"type":"getServerAddress"
|
2020-01-18 15:20:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
browser.runtime.sendMessage(parameters, {},
|
2022-09-26 20:30:40 +02:00
|
|
|
function (response)
|
|
|
|
{
|
|
|
|
if (tabs[0].url === undefined) return true;
|
|
|
|
if (tabs[0].url.startsWith("chrome://newtab/")) tabs[0].url = "";
|
|
|
|
var url = response.value;
|
|
|
|
url = url.substring(0, url.lastIndexOf('/'));
|
|
|
|
url += '?';
|
|
|
|
url += 'url=' + encodeURI(tabs[0].url.split("?")[0]);
|
|
|
|
url += '&user=' + document.getElementById("gPassUsername").value;
|
|
|
|
var link = document.getElementById("serverLink");
|
|
|
|
link.href = url;
|
|
|
|
return true;
|
|
|
|
});
|
2020-01-18 15:20:13 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-26 20:30:40 +02:00
|
|
|
browser.tabs.query({active:true, currentWindow:true}).then(_query_tabs_init);
|