Some Random Nerd

View Original

A Day One bookmarklet for iOS

There is a promotion in the Apple App Store at the moment, giving away 10 apps for free to mark the 5 year anniversary of the App Store's launch.

One of the apps there is Day One, which I had heard some good things about, so decided to give it a whirl. And I like it.

One thing that I thought would be useful was a bookmarklet to send web pages from Safari into Day One entries. I had a quick look, but couldn't find anything. So I had a stab at building one myself.

This is the Day One URL scheme;

CommandURL
Open Day Onedayone://
Start an entrydayone://post?entry="entry body"
Open Entries listdayone://entries
Open Calendardayone://calendar
Open Starred dayone://starred
Edit Entrydayone://edit?entryId=[UUID]
Preferencesdayone://preferences

And this is a bookmarklet I had previously made to work with the Drafts app;

javascript:window.location='drafts://x-callback-url/create?text='+encodeURIComponent(document.title+'\n')+encodeURIComponent(location.href)

To start with, I put the Day One URL into a very simple JavaScript bookmarklet;

javascript:window.location='dayone://post?entry="entry body"'

And it works- good start!

Taking the code from my Drafts bookmarklet to get the URL and page title gave me this;

javascript:window.location='dayone://post?entry="'+encodeURIComponent(document.title+'\n')+encodeURIComponent(location.href)+'"'

Which also worked. So this is basically the same as my Drafts bookmarklet (but without the Actions to trigger).

I had a more complex Drafts bookmarklet which checks for selected text (only works on the iPad when the Bookmarks bar is visible - otherwise any text is deselected when you pull up the bookmarks menu) - switching the base URL gave me this (I've added line breaks to make it readabl here- you probably don't want them if you're using this bookmarklet yourself. Just copy/paste the code into a text editor and remove the line breaks so it is all on a single line.)

javascript:function%20 getSelText()%7Bvar%20txt=%27%27; if(window.getSelection)%7Btxt=window.getSelection();%7D else%20if(document.getSelection)%7Btxt=document.getSelection();%7D else%20if(document.selection)%7Btxt=document.selection.createRange().text;%7D else%20return%20%27%27; return%20txt;%7D var%20q=getSelText(); if(q!=%27%27)%7B q='%3Cblockquote%3E%5Cn'+q+'%5Cn%3C%2Fblockquote%3E%5Cn';%7D var%20l='dayone://post?entry='+'%5B' +encodeURIComponent(document.title)+'%5D%28' +encodeURIComponent(location.href+'%29%5Cn'); if(!document.referrer)%7Br='';%7D else%7Br='via%20'+encodeURIComponent(document.referrer);%7D window.location=l+r+'%5Cn'+q+'%5Cn';

Which, to my surprise (once I had got rid of some stray commas and semicolons) worked!

With some text selected on a web page, this bookmarklet now opens Day One, creates an entry and populated it with the web page title (as a markdown link to the page URL) and any selected text in a blockquote HTML tag, and looks something like this;

Drafts and Safari bookmarklets — Some Random Nerd

It occurred to me that an app that plays so nicely with URL schemes (ie. sending things to other apps via their URL schemes) would probably have a scheme of its own for pulling things in. A little googling later and I found that you can; like this bookmarklet

Not a bad result at all - especially considering I managed to put it all together on my iPad on a 25 minute train journey.