Skip to content

Instantly share code, notes, and snippets.

@michaelgold
Forked from al3xandru/gist:1169583
Last active December 11, 2015 16:38
Show Gist options
  • Save michaelgold/4628886 to your computer and use it in GitHub Desktop.
Save michaelgold/4628886 to your computer and use it in GitHub Desktop.
Gmail to Omnifocus Bookmarklet
javascript:(function(){
var enc=encodeURIComponent,
w=window,
frames=w.frames,
d=document,
tn=w.getSelection?w.getSelection():(d.getSelection)?d.getSelection():(d.selection?d.selection.createRange().text:0),
pu=w.location.href,
isGMail=w.location.host.match(/mail\.google\.com/),
tt=pt=d.title,
subjSpans=d.getElementsByClassName("hP"),i,url;
emailSpans=d.getElementsByClassName("iw"),i,url;
accountSpans=d.getElementsByClassName("gbgt"),i,url;
if(isGMail){
if(subjSpans){
tt=subjSpans[0].innerText
}
else {
tt=d.title.substring(d.title.indexOf("-")+1,d.title.lastIndexOf("-")).replace(/^ +/,"").replace(/ +$/,"")
};
if (emailSpans){
em=emailSpans[0].innerText;
ema=emailSpans[0].childNodes[0].getAttribute("email")
}
if (accountSpans){
ac=accountSpans[4].childNodes[0].innerText;
}
if(tn!="")tn+="\n\n";
tn+="Account: "+ac+"\nFrom: "+em+" "+ema+"\nSubject: "+tt+"\n"+pu;
pu="https://mail.google.com/mail/u/0/?qs=true&search=query&q=subject:("+enc(tt)+")"
};
url='omnifocus:///add?note='+enc(tn+"\n"+pu)+'&name='+enc(tt);
w.location.href=url
})();
// one liner:
javascript:(function(){var enc=encodeURIComponent,w=window,frames=w.frames,d=document,tn=w.getSelection?w.getSelection():(d.getSelection)?d.getSelection():(d.selection?d.selection.createRange().text:0),pu=w.location.href,isGMail=w.location.host.match(/mail\.google\.com/),tt=pt=d.title,subjSpans=d.getElementsByClassName("hP"),i,url;if(isGMail){if(subjSpans){tt=subjSpans[0].innerText}else{tt=d.title.substring(d.title.indexOf("-")+1,d.title.lastIndexOf("-")).replace(/^ +/,"").replace(/ +$/,"")};if(tn!="")tn+="\n\n";tn+="From email subject:("+tt+")\n"+pu;pu="https://mail.google.com/mail/u/0/?qs=true&search=query&q=subject:("+enc(tt)+")"};url='omnifocus:///add?note='+enc(tn+"\n"+pu)+'&name='+enc(tt);w.location.href=url})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment