chrome.extension.onRequest.addListener(LoadPasswordHandler);ListenForPasswordChanges();function LoadPasswordHandler(b){if(b.message=="passwords")InsertPasswordData(b.data.matches,b.data.showUi);else b.message=="promptSave"&&PromptSavePassword(b.data)}
function InsertPasswordData(b,g){if(b&&b.length)for(var f=0;f<document.forms.length;f++){var a=document.forms[f],h=FindPasswordField(a);if(h.element){a=FindUsernameField(a,h.index);XmarksLog("Found password form.  Password field name ["+h.element.name+"], Username field name ["+(a.element?a.element.name:"empty")+"]");for(var k=0;k<b.length;k++){var d=b[k],e=0;if(h.element.name==d.passwordField)e+=2;if(a.element&&a.element.name==d.usernameField)e+=1;else if(!a.element&&d.usernameField=="")e+=1;XmarksLog("Scoring saved password ["+
e+"].  Password field name ["+d.passwordField+"], username field name ["+d.usernameField+"]");d.score=e}b.sort(function(i,m){return m.score-i.score});if(b.length>0){if(g){h.element.style.background="-webkit-gradient(linear, left top, right top, from(#ffffff), to(rgb(60, 170, 220))) !important";if(a.element)a.element.style.background="-webkit-gradient(linear, left top, right top, from(#ffffff), to(rgb(60, 170, 220))) !important"}if(b.length==1||b[0].score>b[1].score){XmarksLog("Inserting single password");
h.element.value=b[0].password;if(a.element)a.element.value=b[0].username}if(a.element){XmarksLog("Inserting username change handler");a.element.addEventListener("keyup",c(b,a.element),false);a.element.addEventListener("blur",l(b,h.element),false);var c=function(i,m){return function(j){if(!(j.keyCode<48)){j=m.value;if(!(j.length<1))for(var n=0;n<i.length;n++)if(i[n].username.indexOf(j)==0&&i[n].username>j){m.value=i[n].username;m.setSelectionRange(j.length,i[n].username.length);break}}}},l=function(i,
m){return function(){XmarksLog("Attempting to autofill password for username: "+this.value);for(var j=0;j<i.length;j++)if(i[j].username==this.value){XmarksLog("Match found, updating password field");m.value=i[j].password;break}else XmarksLog("Not a match, skipping ["+i[j].username+"]")}}}}}}}
function ListenForPasswordChanges(){for(var b=0;b<document.forms.length;b++){var g=document.forms[b],f=FindPasswordField(g);if(f.element){var a=FindUsernameField(g,f.index);XmarksLog("Adding form and input listeners");f.element.addEventListener("keypress",h,false);a.element&&a.element.addEventListener("keypress",h,false);g.addEventListener("submit",k,false);var h=function(d){var e;if(d.target.getAttribute("type")=="password")e="lastValidXmarksPass";else if(d.target.getAttribute("type")=="text")e=
"lastValidXmarksUser";else{XmarksLog("InputFidleChangeHandler on invalid type: "+d.target.getAttribute("type"));return}d.target.value?d.target.form.setAttribute(e,JSON.stringify({value:d.target.value,field:d.target.name})):XmarksLog("Field being emptied, not overwriting previous value")},k=function(d){d=d.target;var e=null,c=FindPasswordField(d);if(!c.element||!c.element.value){XmarksLog("Couldn't find password data, attempting to pull saved value");e=(e=d.getAttribute("lastValidXmarksPass"))?JSON.parse(e):
null;if(!e){XmarksLog("Can't find password element, nothing to handle on submit");return}}else e={value:c.element.value,field:c.element.name};e={passwordField:e.field,password:e.value,url:document.location.href,formAction:d.action,usernameField:"",username:""};var l=null;if(c){c=FindUsernameField(d,c.index);if(c.element&&c.element.value)l={value:c.element.value,field:c.element.name}}if(!l){XmarksLog("Couldn't find username data, attempting to pull saved value");l=(d=d.getAttribute("lastValidXmarksUser"))?
JSON.parse(d):null}if(l){e.usernameField=l.field;e.username=l.value}XmarksLog("Sending prompt data to extension");chrome.extension.sendRequest({requestType:"promptSavePassword",promptData:e})}}}}
function PromptSavePassword(b){function g(){var c=document.createElement("input");c.setAttribute("type","button");c.style.margin="0 4px";c.style.fontFamily="Verdana, sans-serif";c.style.padding="3px 8px";c.style.backgroundColor="#f5f5f5";c.style.border="1px solid #dedede";c.style.borderTop="1px solid #eee";c.style.borderLeft="1px solid #eee";c.style.borderRadius="5px 5px";c.style.fontSize="12px";c.style.lineHeight="130%";c.style.textDecoration="none";c.style.color="black";c.style.cursor="pointer";
return c}if(document.getElementById("XmarksPasswordPrompt"))XmarksLog("XmarksPasswordPrompt already exists, not adding prompt");else{var f="save";if(b.type=="update")f="update";var a=document.createElement("div");a.id="XmarksPasswordPrompt";a.style.left="0";a.style.top="0";a.style.width="100%";a.style.zIndex="10000";a.style.position="fixed";a.style.backgroundColor="rgb(60, 170, 220)";a.style.borderBottom="1px solid black";a.style.padding="5px";a.style.fontSize="10pt";a.style.fontFamily="verdana, sans-serif";
a.style.color="black";a.style["-webkit-box-shadow"]="0 3px 3px #666";var h=document.createElement("div");h.style.paddingLeft="5px";a.style.fontSize="12pt";a.style.textAlign="left";a.style.fontFamily="Trebuchet MS, Helvetica, sans-serif";h.innerHTML="Would you like Xmarks to "+f+" your password?";f=document.createElement("div");f.style["float"]="right";f.style.width="360px";f.style.paddingRight="10px";f.style.textAlign="right";var k=g();k.setAttribute("value","Save password");k.addEventListener("click",
function(){XmarksLog("Saving password data!");chrome.extension.sendRequest({requestType:"savePassword",promptData:b});var c=document.getElementById("XmarksPasswordPrompt");c&&document.body.removeChild(c)});var d=g();d.setAttribute("value","Not now");d.addEventListener("click",function(){XmarksLog("Removing XmarksPasswordPrompt element from DOM");var c=document.getElementById("XmarksPasswordPrompt");c&&document.body.removeChild(c)});var e=g();e.setAttribute("value","Never for this site");e.addEventListener("click",
function(){XmarksLog("Adding exception");chrome.extension.sendRequest({requestType:"exceptionPassword",promptData:b});var c=document.getElementById("XmarksPasswordPrompt");c&&document.body.removeChild(c)});f.appendChild(k);f.appendChild(d);f.appendChild(e);a.appendChild(f);a.appendChild(h);document.body.appendChild(a)}}function XmarksLog(b){chrome.extension.sendRequest({requestType:"log",message:b})}
function FindPasswordField(b){if(b.elements&&b.elements.length)for(passwordIndex=0;passwordIndex<b.elements.length;passwordIndex++){var g=b.elements[passwordIndex];if(g.type=="password"&&!g.disabled)return{element:g,index:passwordIndex}}return{element:null,index:-1}}function FindUsernameField(b,g){if(b.elements&&b.elements.length)for(var f=g;f>0;f--){var a=b.elements[f-1];if(a.type=="text"&&!a.disabled)return{element:a,index:f}}return{element:null,index:-1}};
