Synchronize firefox webextension code

This commit is contained in:
Grégory Soutadé 2021-03-02 14:56:26 +01:00
parent 0e48a34d71
commit b79000accb
7 changed files with 216 additions and 92 deletions

View File

@ -162,7 +162,7 @@ async function ask_server(logins, domain, wdomain, mkey, sendResponse, options)
{ {
debug("New pbkdf2 level " + server_pbkdf2_level); debug("New pbkdf2 level " + server_pbkdf2_level);
pbkdf2_level = server_pbkdf2_level; pbkdf2_level = server_pbkdf2_level;
set_preference("pbkdf2_level", pbkdf2_level); set_preference("pbkdf2_level", pbkdf2_level, null);
ret = SERVER.RESTART_REQUEST; ret = SERVER.RESTART_REQUEST;
} }
break; break;
@ -234,6 +234,8 @@ function update_gpass_icon(iconId, tabId)
break; break;
default: default:
} }
debug(icon_name);
icon_infos["path"] = { icon_infos["path"] = {
16:"icons/gpass" + icon_name + "_icon_16.png", 16:"icons/gpass" + icon_name + "_icon_16.png",
@ -245,12 +247,29 @@ function update_gpass_icon(iconId, tabId)
browser.browserAction.setIcon(icon_infos); browser.browserAction.setIcon(icon_infos);
} }
function is_gpass_enabled(uri) async function is_gpass_enabled(uri)
{ {
var domain = parseURI.parseUri(uri); return await get_preference("always_disabled").then(
domain = domain["host"]; function(always_disabled) {
debug("Is gpass enabled for " + domain + " ?"); if (always_disabled)
return get_preference("disable-" + domain); {
debug("Always disabled");
return new Promise(function(resolve, reject) {
resolve(1); // null -> enabled, 1 -> disabled
});
}
else
{
debug("Check for enable");
var domain = parseURI.parseUri(uri);
domain = domain["host"];
debug("Is gpass enabled for " + domain + " ?");
return get_preference("disable-" + domain);
}
}
);
} }
function save_gpass_enable_config(uri, enable) function save_gpass_enable_config(uri, enable)
@ -267,7 +286,7 @@ function save_gpass_enable_config(uri, enable)
else else
{ {
debug("Disable gpass for " + domain); debug("Disable gpass for " + domain);
set_preference(key, true); set_preference(key, true, null);
} }
} }
@ -278,7 +297,7 @@ function _query_tabs_is_gpass_enabled(tabs, sendResponse)
is_gpass_enabled(tabs[0].url).then( is_gpass_enabled(tabs[0].url).then(
function (key_present) { function (key_present) {
enabled = (key_present == null); enabled = (key_present == null);
update_gpass_icon((enabled)?GPASS_ICON.ENABLED:GPASS_ICON.DISABLED, tabs[0].id); update_gpass_icon((enabled)?GPASS_ICON.NORMAL:GPASS_ICON.DISABLED, tabs[0].id);
sendResponse({"enabled":enabled}); sendResponse({"enabled":enabled});
} }
); );
@ -300,28 +319,36 @@ function _query_tabs_update_icon(tabs, iconId)
} }
} }
function update_enable(enabled, tab, saveConfig)
{
if (enabled)
{
parameters = {type:"blockForms"};
saveConfig = true;// Force save when enable website
debug("Now enabled");
}
else
{
parameters = {type:"unblockForms"};
debug("Now disabled");
}
if (saveConfig)
save_gpass_enable_config(tab.url, enabled);
update_gpass_icon((enabled)?GPASS_ICON.NORMAL:GPASS_ICON.DISABLED, tab.id);
browser.tabs.sendMessage(tab.id, parameters);
}
function gpass_switch_enable(tab) function gpass_switch_enable(tab)
{ {
debug("Switch enable");
is_gpass_enabled(tab.url).then( is_gpass_enabled(tab.url).then(
function (key_present) function (key_present)
{ {
enabled = (key_present == null); enabled = (key_present == null);
// Do switch // Do switch
enabled = !enabled; enabled = !enabled;
if (enabled) update_enable(enabled, tab, true);
{
parameters = {type:"blockForms"};
debug("Now enabled");
}
else
{
parameters = {type:"unblockForms"};
debug("Now disabled");
}
save_gpass_enable_config(tab.url, enabled);
update_gpass_icon((enabled)?GPASS_ICON.ENABLED:GPASS_ICON.DISABLED, tab.id);
browser.tabs.sendMessage(tab.id, parameters);
}); });
} }
@ -361,15 +388,6 @@ function extension_load()
}); });
return true; return true;
} }
else if (request.type == "switch_enable")
{
debug("Switch enable");
browser.tabs.query({active:true, currentWindow:true},
function cb(tabs) {
_query_tabs_switch_enable(tabs, sendResponse)
});
return true;
}
else if (request.type == "update_icon") else if (request.type == "update_icon")
{ {
debug("update_icon"); debug("update_icon");
@ -387,7 +405,8 @@ function extension_load()
if (!browser.menus && browser.contextMenus) if (!browser.menus && browser.contextMenus)
browser.menus = browser.contextMenus; browser.menus = browser.contextMenus;
/* Settings */
browser.menus.create({ browser.menus.create({
id: 'settings', id: 'settings',
title: 'gPass Settings', title: 'gPass Settings',
@ -396,16 +415,30 @@ function extension_load()
/* Not supported by Chrome */ /* Not supported by Chrome */
if (browser.menus.onShown) if (browser.menus.onShown)
title = 'disable gPass for this website'; title = 'Disable gPass for this website';
else else
title = 'disable or enable gPass for this website'; title = 'Disable or enable gPass for this website';
/* Enable/disable */
browser.menus.create({ browser.menus.create({
id: 'switch_enable', id: 'switch_enable',
title: title, title: title,
contexts: ['browser_action'] contexts: ['browser_action']
}); });
/* Not supported by Chrome */
if (browser.menus.onShown)
title = 'Disable gPass for ALL websites';
else
title = 'Disable or enable gPass for ALL websites';
/* Always enable/disable */
browser.menus.create({
id: 'always_disable',
title: title,
contexts: ['browser_action']
});
browser.menus.onClicked.addListener( browser.menus.onClicked.addListener(
function(info, tab) { function(info, tab) {
switch (info.menuItemId) { switch (info.menuItemId) {
@ -413,6 +446,26 @@ function extension_load()
browser.runtime.openOptionsPage(); browser.runtime.openOptionsPage();
break; break;
case 'always_disable':
get_preference('always_disabled').then(
function (always_disabled) {
debug('Change always disable');
debug(always_disabled);
always_disabled = !always_disabled;
set_preference('always_disabled', always_disabled,
function(error)
{
browser.tabs.query({},
function cb(tabs) {
for (i=0; i<tabs.length; i++)
update_enable(!always_disabled, tabs[i], false);
});
}
);
}
);
break;
case 'switch_enable': case 'switch_enable':
gpass_switch_enable(tab); gpass_switch_enable(tab);
break; break;
@ -428,9 +481,9 @@ function extension_load()
function (key_present) { function (key_present) {
enabled = (key_present == null); enabled = (key_present == null);
if (enabled) if (enabled)
title = 'disable gPass for this website'; title = 'Disable gPass for this website';
else else
title = 'enable gPass for this website'; title = 'Enable gPass for this website';
browser.menus.update("switch_enable", browser.menus.update("switch_enable",
{ {
"title":title "title":title
@ -441,6 +494,25 @@ function extension_load()
); );
} }
); );
browser.menus.onShown.addListener(
function(info, tab) {
get_preference('always_disabled').then(
function (always_disabled) {
if (always_disabled)
title = 'Enable gPass for ALL websites';
else
title = 'Disable gPass for ALL websites';
browser.menus.update("always_disable",
{
"title":title
}
);
browser.menus.refresh();
}
);
}
);
} }
} }

View File

@ -17,6 +17,11 @@
along with gPass. If not, see <http://www.gnu.org/licenses/>. along with gPass. If not, see <http://www.gnu.org/licenses/>.
*/ */
var default_preferences = {"pbkdf2_level": 1000,
"account_url": "https://gpass-demo.soutade.fr/demo",
"always_disabled":false
};
function get_preference(key) function get_preference(key)
{ {
return browser.storage.local.get(key) return browser.storage.local.get(key)
@ -38,9 +43,11 @@ function get_preference(key)
); );
} }
function set_preference(key, value) function set_preference(key, value, sendResponse)
{ {
browser.storage.local.set({[key]:value}); browser.storage.local.set({[key]:value});
if (sendResponse)
sendResponse(true);
} }
function delete_preference(key) function delete_preference(key)

View File

@ -56,23 +56,35 @@ function try_get_name(fields, type_filters, match)
for (var i=0; i<fields.length; i++) for (var i=0; i<fields.length; i++)
{ {
var field = fields[i]; var field = fields[i];
var to_test = !match;
for (var a=0; a<type_filters.length; a++) for (var a=0; a<type_filters.length; a++)
{ {
if ((match && field.getAttribute("type") == type_filters[a]) || if (match && field.getAttribute("type") == type_filters[a])
(!match && field.getAttribute("type") != type_filters[a])) {
{ to_test = true;
if (field.hasAttribute("name") && field.value != "") break;
{ }
name = field.getAttribute("name");
// Subset of common user field
if (name == "user") user = field.value;
else if (name == "usr") user = field.value;
else if (name == "username") user = field.value;
else if (name == "login") user = field.value;
_add_name(all_logins, field.value); if(!match && field.getAttribute("type") == type_filters[a])
} {
to_test = false;
break;
}
}
if (to_test)
{
if (field.hasAttribute("name") && field.value != "")
{
name = field.getAttribute("name");
// Subset of common user field
if (name == "user") user = field.value;
else if (name == "usr") user = field.value;
else if (name == "username") user = field.value;
else if (name == "login") user = field.value;
_add_name(all_logins, field.value);
} }
} }
} }
@ -83,28 +95,59 @@ function try_get_name(fields, type_filters, match)
return all_logins; return all_logins;
} }
function get_logins(form, all_logins)
{
var fields = form.getElementsByTagName("input");
var logins = null;
debug("get_logins");
var type_filters = new Array();
// Get all <input type="text"> && <input type="email">
type_filters.push("text");
type_filters.push("email");
if (all_logins)
logins = try_get_name(fields, type_filters, true);
else
{
// Get all other fields except text, email and password
type_filters.push("password");
type_filters.push("hidden");
logins = try_get_name(fields, type_filters, false);
}
return logins;
}
function on_focus(e) function on_focus(e)
{ {
if (gpass_enabled) if (!gpass_enabled)
return;
var logins = get_logins(this.form, true);
var all_logins = get_logins(this.form, false);
if (logins.length || all_logins.length)
{ {
parameters = { parameters = {
type:"update_icon", type:"update_icon",
icon_id:GPASS_ICON.ACTIVATED, icon_id:GPASS_ICON.ACTIVATED,
}; };
browser.runtime.sendMessage(parameters, {}); browser.runtime.sendMessage(parameters, {});
} }
} }
function on_blur(e) function on_blur(e)
{ {
if (gpass_enabled) if (!gpass_enabled)
{ return;
parameters = {
type:"update_icon", parameters = {
icon_id:GPASS_ICON.NORMAL, type:"update_icon",
}; icon_id:GPASS_ICON.NORMAL,
browser.runtime.sendMessage(parameters, {}); };
} browser.runtime.sendMessage(parameters, {});
} }
function on_sumbit(e) function on_sumbit(e)
@ -116,15 +159,8 @@ function on_sumbit(e)
debug("on_submit"); debug("on_submit");
type_filters = new Array(); logins = get_logins(form, true);
// Get all <input type="text"> && <input type="email"> all_logins = get_logins(form, false);
type_filters.push("text");
type_filters.push("email");
logins = try_get_name(fields, type_filters, true);
// Get all other fields except text, email and password
type_filters.push("password");
all_logins = try_get_name(fields, type_filters, false);
if (!logins.length) if (!logins.length)
logins = all_logins; logins = all_logins;
@ -259,8 +295,6 @@ function unblock_all_forms()
{ {
debug("unblock all forms"); debug("unblock all forms");
on_blur(null);
for(var i=0; i<managed_forms.length; i++) for(var i=0; i<managed_forms.length; i++)
{ {
var form = managed_forms[i]; var form = managed_forms[i];

View File

@ -22,10 +22,6 @@ var DEBUG = false;
SERVER = {OK : 0, FAILED : 1, RESTART_REQUEST : 2}; SERVER = {OK : 0, FAILED : 1, RESTART_REQUEST : 2};
GPASS_ICON = {NORMAL:0, DISABLED:1, ACTIVATED:2}; GPASS_ICON = {NORMAL:0, DISABLED:1, ACTIVATED:2};
var default_preferences = {"pbkdf2_level": 1000,
"account_url": "https://gpass-demo.soutade.fr/demo",
"crypto_v1_compatible": true};
var browser = browser || chrome; var browser = browser || chrome;
var crypto = crypto || window.crypto; var crypto = crypto || window.crypto;

View File

@ -3,7 +3,7 @@
"name": "gPass", "name": "gPass",
"short_name": "gPass", "short_name": "gPass",
"version": "1.0", "version": "1.1",
"description": "gPass : global password manager", "description": "gPass : global password manager",
"icons" : {"16":"icons/gpass_icon_16.png", "32":"icons/gpass_icon_32.png", "64":"icons/gpass_icon_64.png", "128":"icons/gpass_icon_128.png"}, "icons" : {"16":"icons/gpass_icon_16.png", "32":"icons/gpass_icon_32.png", "64":"icons/gpass_icon_64.png", "128":"icons/gpass_icon_128.png"},
"author" : "Grégory Soutadé", "author" : "Grégory Soutadé",
@ -39,8 +39,6 @@
"<all_urls>", "<all_urls>",
"activeTab", "activeTab",
"notifications", "notifications",
"webRequest",
"webRequestBlocking",
"tabs", "tabs",
"storage", "storage",
"clipboardWrite", "clipboardWrite",

View File

@ -8,7 +8,10 @@
<b>Account URL</b> URL of your gPass account <input id="account_url" type="text"/><br /> <b>Account URL</b> URL of your gPass account <input id="account_url" type="text"/><br />
<b>WARNING</b> It should be a valid HTTPS URL because navigator doesn't like mixed content (HTTPS/HTTP). If not, requests will silentely failed. If you have an auto-signed certificate, add it to trusted ones.<br/> <b>WARNING</b> It should be a valid HTTPS URL because navigator doesn't like mixed content (HTTPS/HTTP). If not, requests will silentely failed. If you have an auto-signed certificate, add it to trusted ones.<br/>
<br/> <br/>
<b>PBKDF2 level</b> Number of iterations used to derivate master key <input id="pbkdf2" type="number"/><br /> <b>PBKDF2 level</b> Number of iterations used to derivate master key <input id="pbkdf2_level" type="number"/><br />
<br/>
<br/>
<b>Always disabled</b> Disable gPass for all websites. You can still use popup, but no hook is attached to login forms<input id="always_disabled" type="checkbox"/><br />
<br/> <br/>
<br/> <br/>
<input type="button" id="save" value="Save"/> <input type="button" id="save" value="Save"/>

View File

@ -1,33 +1,47 @@
var default_preferences = {"pbkdf2_level": 1000, var default_preferences = {"pbkdf2_level": 1000,
"account_url": "https://gpass-demo.soutade.fr/demo" "account_url": "https://gpass-demo.soutade.fr/demo",
"always_disabled":false
}; };
function save() { function save() {
var account_url = document.getElementById('account_url').value; var account_url = document.getElementById('account_url').value;
var pbkdf2 = document.getElementById('pbkdf2').value; var pbkdf2_level = document.getElementById('pbkdf2_level').value;
var always_disabled = document.getElementById('always_disabled').checked;
browser.storage.local.set({ browser.storage.local.set({
"account_url":account_url, "account_url":account_url,
"pbkdf2_level":pbkdf2, "pbkdf2_level":pbkdf2_level,
"always_disabled": always_disabled,
}) })
.then(function ok() { alert("Saved"); }, .then(function ok() { alert("Saved"); },
function err() { alert("Cannot save your preferences");} function err() { alert("Cannot save your preferences");}
); );
} }
function restoreOption(preferences, pref)
{
var res;
if (!preferences.hasOwnProperty(pref))
res = default_preferences[pref];
else
res = preferences[pref];
var element = document.getElementById(pref);
if (element.type == 'checkbox')
element.checked = res;
else
element.value = res;
}
function restoreOptions() function restoreOptions()
{ {
document.getElementById('account_url').value = default_preferences['account_url'];
document.getElementById('pbkdf2').value = default_preferences['pbkdf2_level'];
browser.storage.local.get().then( browser.storage.local.get().then(
function(prefs) function(prefs)
{ {
if (prefs.hasOwnProperty("account_url")) restoreOption(prefs, 'account_url');
document.getElementById('account_url').value = prefs["account_url"]; restoreOption(prefs, 'pbkdf2_level');
restoreOption(prefs, 'always_disabled');
if (prefs.hasOwnProperty("pbkdf2_level"))
document.getElementById('pbkdf2').value = prefs["pbkdf2_level"];
} }
); );
} }