Skip to content

Instantly share code, notes, and snippets.

@searls
Created December 2, 2011 17:42
Show Gist options
  • Save searls/1424138 to your computer and use it in GitHub Desktop.
Save searls/1424138 to your computer and use it in GitHub Desktop.
Bookmarklet to plug in harvest CSV exports to BigTime
var jquerify = function(callback) {
//ripped from karl: http://www.learningjquery.com/2009/04/better-stronger-safer-jquerify-bookmarklet/
var el=document.createElement('div'),
b=document.getElementsByTagName('body')[0],
otherlib=false,
msg='';
el.style.position='fixed';
el.style.height='32px';
el.style.width='220px';
el.style.marginLeft='-110px';
el.style.top='0';
el.style.left='50%';
el.style.padding='5px 10px';
el.style.zIndex = 1001;
el.style.fontSize='12px';
el.style.color='#222';
el.style.backgroundColor='#f99';
if (typeof $=='function') {
otherlib=true;
}
// more or less stolen form jquery core and adapted by paul irish
function getScript(url,success){
var script=document.createElement('script');
script.src=url;
var head=document.getElementsByTagName('head')[0],
done=false;
// Attach handlers for all browsers
script.onload=script.onreadystatechange = function(){
if ( !done && (!this.readyState
|| this.readyState == 'loaded'
|| this.readyState == 'complete') ) {
done=true;
success();
script.onload = script.onreadystatechange = null;
head.removeChild(script);
}
};
head.appendChild(script);
}
getScript('http://code.jquery.com/jquery-latest.min.js',function() {
if (!(typeof jQuery=='undefined')) {
callback(jQuery);
}
});
};
jquerify(function($) {
var addExpense = function() {
var expense = parseCsv(getNextRow());
openForm('ProjectNm');
when({
thisHappens: function() { return dialogIsVisible("Projects") },
doThis: function() {
$('#tableSection a.list-item:contains("'+expense.project+'")',$('#IF_DIALOG')[0].contentWindow.document).trigger('click')
openForm('LK_SHOW_CatSID');
when({
thisHappens: function() { return dialogIsVisible("Expense Categories") },
doThis: function() {
$('#tableSection .list-item a:contains("'+expense.category+'")',$('#IF_DIALOG')[0].contentWindow.document).trigger('click')
$(':input[name=Dt]').val(expense.date);
$(':input[name=CostIN]').val(expense.cost);
$('textarea[name=Nt]').text(expense.notes);
$('#cmdGO').trigger('click')
}
});
}
});
};
var openForm = function(name) {
$(':input[name="'+name+'"]').siblings('img').trigger('click');
};
var when = function(config) {
var id = setInterval(function() {
if(config.thisHappens()) {
config.doThis();
clearInterval(id);
}
},100);
};
getNextRow = function() {
var csv = (sessionStorage['harvestExpenseReport'] || (prompt("Paste your Harvest CSV export:").split("\n").slice(1).join("\n"))).split("\n");
var first = csv.shift();
var leftovers = csv.join("\n");
if(/\S/.test(leftovers)) {
sessionStorage['harvestExpenseReport'] = leftovers;
} else {
delete sessionStorage['harvestExpenseReport'];
}
return first;
};
parseCsv = function(row) {
var vals = row.split(/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/)
return {
date: (function(){ var d="2011-11-28".split(/-/); return d[1]+'/'+d[2]+'/'+d[0]; })(),
project: vals[2],
category: vals[4],
cost: vals[9],
notes: vals[8]
};
};
dialogIsVisible = function(title) {
return $('#tableSection .list-head:contains("'+title+'")',$('#IF_DIALOG')[0].contentWindow.document).is(':visible');
};
$('<span class="quick-add-expense">Quick Expense</span>').insertAfter('#cmdGO').css({
display: 'inline-block',
padding: '3px',
background: 'white',
border: '1px solid black',
margin: '0px 15px 0px 5px',
cursor: 'hand'
}).on('click',addExpense);
if(sessionStorage['harvestExpenseReport']) {
addExpense();
}
});
javascript:var jquerify=function(b){var c=document.createElement("div");document.getElementsByTagName("body");c.style.position="fixed";c.style.height="32px";c.style.width="220px";c.style.marginLeft="-110px";c.style.top="0";c.style.left="50%";c.style.padding="5px 10px";c.style.zIndex=1001;c.style.fontSize="12px";c.style.color="#222";c.style.backgroundColor="#f99";(function(b,c){var a=document.createElement("script");a.src=b;var f=document.getElementsByTagName("head")[0],g=!1;a.onload=a.onreadystatechange=function(){if(!g&&(!this.readyState||"loaded"==this.readyState||"complete"==this.readyState))g=!0,c(),a.onload=a.onreadystatechange=null,f.removeChild(a)};f.appendChild(a)})("http://code.jquery.com/jquery-latest.min.js",function(){"undefined"!=typeof jQuery&&b(jQuery)})};jquerify(function(b){var c=function(){var a=parseCsv(getNextRow());d("ProjectNm");e({thisHappens:function(){return dialogIsVisible("Projects")},doThis:function(){b('#tableSection a.list-item:contains("'+a.project+'")',b("#IF_DIALOG")[0].contentWindow.document).trigger("click");d("LK_SHOW_CatSID");e({thisHappens:function(){return dialogIsVisible("Expense Categories")},doThis:function(){b('#tableSection .list-item a:contains("'+a.category+'")',b("#IF_DIALOG")[0].contentWindow.document).trigger("click");b(":input[name=Dt]").val(a.date);b(":input[name=CostIN]").val(a.cost);b("textarea[name=Nt]").text(a.notes);b("#cmdGO").trigger("click")}})}})},d=function(a){b(':input[name="'+a+'"]').siblings("img").trigger("click")},e=function(a){var b=setInterval(function(){a.thisHappens()&&(a.doThis(),clearInterval(b))},100)};getNextRow=function(){var a=(sessionStorage.harvestExpenseReport||prompt("Paste your Harvest CSV export:").split("\n").slice(1).join("\n")).split("\n"),b=a.shift(),a=a.join("\n");/\S/.test(a)?sessionStorage.harvestExpenseReport=a:delete sessionStorage.harvestExpenseReport;return b};parseCsv=function(a){var a=a.split(/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/),b="2011-11-28".split(/-/);return{date:b[1]+"/"+b[2]+"/"+b[0],project:a[2],category:a[4],cost:a[9],notes:a[8]}};dialogIsVisible=function(a){return b('#tableSection .list-head:contains("'+a+'")',b("#IF_DIALOG")[0].contentWindow.document).is(":visible")};b('<span class="quick-add-expense">Quick Expense</span>').insertAfter("#cmdGO").css({display:"inline-block",padding:"3px",background:"white",border:"1px solid black",margin:"0px 15px 0px 5px",cursor:"hand"}).on("click",c);sessionStorage.harvestExpenseReport&&c()});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment