Permalink
Browse files

Merge pull request passwordmaker#34 from wadetb/match-profile-to-site

Activate profiles automatically for specific sites
  • Loading branch information...
bitboxer committed Aug 3, 2011
2 parents c98ca6a + 968737b commit f44a003bd6ed66f44a2c47231e766ae50334c4f4
Showing with 108 additions and 8 deletions.
  1. +7 −0 html/options.html
  2. +30 −0 javascript/import.js
  3. +2 −0 javascript/options.js
  4. +60 −7 javascript/popup.js
  5. +2 −0 javascript/profile.js
  6. +2 −0 test/import.js
  7. +5 −1 test/index.html
View
@@ -47,6 +47,13 @@ <h2>Profiles</h2>
<input class="standard" id="profileNameTB"/>
</div>
<div class="row">
<div class="subrow">
<label for="siteList">Use with sites:</label>
<input class="standard" id="siteList"/>
</div>
</div>
<div class="row">
<div class="url_example">
<span id="exprotocol">http://</span><span id="exsubdomain">my.</span><span id="exdomain">example.com</span><span id="expath">/my/path?q=search#top</span>
View
@@ -97,6 +97,36 @@ RdfImporter.loadDoc = function(rdf) {
prof[opts.name] = opts.convert ? opts.convert(val) : val;
}
}
// store site patterns
var patterns = [],
patternType = [],
patternEnabled = [],
siteList = '';
for(var i=0;i<this.attributes.length;i++){
var attrName = this.attributes[i].name.replace(/.*:/g,'');
var m = attrName.match(/pattern(|type|enabled)(\d+)/);
if(m){
if (m[1] == '') {
patterns[m[2]] = this.attributes[i].value;
} else if (m[1] == 'type') {
patternType[m[2]] = this.attributes[i].value;
} else if (m[1] == 'enabled') {
patternEnabled[m[2]] = this.attributes[i].value;
}
}
}
for(var i=0;i<patterns.length;i++){
if(patternEnabled[i] == 'true'){
if(patternType[i]=='regex'){
siteList += '/'+patterns[i]+'/ ';
}else{
siteList += patterns[i]+' ';
}
}
}
prof['siteList'] = siteList;
if(prof.rdf_about == 'http://passwordmaker.mozdev.org/globalSettings'){
settings = prof;
}else if(prof.selectedCharset){
View
@@ -64,6 +64,7 @@ function removeProfile() {
function setCurrentProfile(profile) {
currentProfile = profile;
$("#profileNameTB").val(profile.title);
$("#siteList").val(profile.siteList);
$("#protocolCB").attr('checked', profile.url_protocol);
$("#subdomainCB").attr('checked', profile.url_subdomain);
$("#domainCB").attr('checked', profile.url_domain);
@@ -157,6 +158,7 @@ function highlightProfile(){
function saveProfile() {
currentProfile.title = $("#profileNameTB").val();
currentProfile.siteList = $("#siteList").val();
currentProfile.url_protocol = $("#protocolCB").attr('checked');
currentProfile.url_subdomain = $("#subdomainCB").attr('checked');
currentProfile.url_domain = $("#domainCB").attr('checked');
View
@@ -9,15 +9,58 @@ function setPasswordColors(foreground, background) {
$("#confirmation").css("color", foreground);
}
function getAutoProfileIdForUrl(url) {
var profiles = Settings.getProfiles();
for (var i in profiles) {
var profile = profiles[i];
if (profile.siteList) {
var usedText = profile.getUrl(url);
var sites = profile.siteList.split(' ');
for (var j = 0; j < sites.length; j++) {
var pat = sites[j];
if (pat[0] == '/' && pat[pat.length-1] == '/') {
pat = pat.substr(1, pat.length-2);
} else {
pat = pat.replace(/[$.+()^\[\]\\|{},]/g, '');
pat = pat.replace(/\?/g, '.');
pat = pat.replace(/\*/g, '.*');
}
if (pat[0] != '^') pat = '^' + pat;
if (pat[pat.length-1] != '$') pat = pat + '$';
var re;
try {
re = new RegExp(pat);
} catch(e) {
console.log(e + "\n");
}
if (re.test(usedText) || re.test(url)) {
return profile.id;
}
}
}
}
return null;
}
function updateFields(e) {
var password = $("#password").val();
var confirmation = $("#confirmation").val();
var usedtext = $("#usedtext").val();
var profileId = $("#profile").val();
if (profileId == "auto") {
profileId = getAutoProfileIdForUrl(usedtext);
} else {
Settings.setActiveProfileId(profileId);
}
var profile = Settings.getProfile(profileId);
Settings.setStoreLocation($("#store_location").val());
Settings.setPassword(password);
Settings.setActiveProfileId(profileId);
if (password == "") {
$("#generatedForClipboard").val("");
@@ -58,7 +101,11 @@ function matchesHash(password) {
}
function updateUsedText(url) {
var profile = Settings.getProfile($("#profile").val());
var profileId = $("#profile").val();
if (profileId == "auto") {
profileId = getAutoProfileIdForUrl(url);
}
var profile = Settings.getProfile(profileId);
$("#usedtext").val(profile.getUrl(url));
}
@@ -79,19 +126,25 @@ function showCopy() {
}
function init(url) {
var profiles = Settings.getProfiles();
Settings.getPassword(function(password) {
$("#password").val(password);
$("#confirmation").val(password);
var activeProfileId = Settings.getActiveProfileId();
var autoProfileId = getAutoProfileIdForUrl(url);
var options = "";
var profiles = Settings.getProfiles();
for (var i in profiles) {
var profile = profiles[i];
options += "<option value='"+profile.id+"'";
if (profile.id == Settings.getActiveProfileId()){
options += " selected='true' ";
if (autoProfileId && profile.id == autoProfileId) {
options += "<option value='auto' selected='true'";
} else if (!autoProfileId && profile.id == activeProfileId) {
options += "<option value='"+profile.id+"' selected='true'";
} else {
options += "<option value='"+profile.id+"'";
}
options += "'>"+profile.title+"</option>";
options += ">"+profile.title+"</option>";
}
$("#profile").empty().append(options);
View
@@ -2,6 +2,8 @@ function Profile() {
this.id = 1;
this.title = "Default";
this.siteList = "";
// Settings for the URL generation
this.url_protocol = false;
this.url_subdomain = false;
View
@@ -32,6 +32,7 @@ test("load profile 1", function () {
same(p.passwordSuffix, 'suffix1');
same(p.whereToUseL33t, 'before-hashing');
same(p.l33tLevel, 1);
same(p.siteList, '/https?://mail\\.yahoo\\.com/.*/ http?://github.com/* ');
});
test("load profile 2", function () {
@@ -52,6 +53,7 @@ test("load profile 2", function () {
same(p.passwordSuffix, 'suffix1');
same(p.whereToUseL33t, 'off');
same(p.l33tLevel, 1);
same(p.siteList, '');
});
test("save profiles", function () {
View
@@ -58,7 +58,11 @@ <h2 id="qunit-userAgent"></h2>
NS1:pattern1="https?://mail\.yahoo\.com/.*"
NS1:patterntype1="regex"
NS1:patternenabled1="true"
NS1:patterndesc1="theyahoo" />
NS1:patterndesc1="theyahoo"
NS1:pattern2="http?://github.com/*"
NS1:patterntype2="wildcard"
NS1:patternenabled2="true"
NS1:patterndesc2="thegithub" />
<RDF:Seq RDF:about="http://passwordmaker.mozdev.org/accounts">
<RDF:li RDF:resource="http://passwordmaker.mozdev.org/defaults"/>
<RDF:li RDF:resource="rdf:#$6PGpU1"/>

0 comments on commit f44a003

Please sign in to comment.