How to best do an instapaper bookmarklet?

759 views
Skip to first unread message

scrotty

unread,
Oct 18, 2010, 9:00:05 PM10/18/10
to Pentadactyl
Instapaper (instapaper.com) has a bookmarklet that they advise users
drag to their bookmarks bar. When the user finds a page they'd like to
read later, they simply click this button and it is "saved' on the
instapaper site.

I looked at the bookmarklet's source and see this (I've elided part of
my unique identifier from the embedded URL):

<a href="javascript:function iprl5(){var
d=document,z=d.createElement(&#039;scr&#039;+&#039;ipt&#039;),b=d.body,l=d.location;try{if(!
b)throw(0);d.title=&#039;(Saving...)
&#039;+d.title;z.setAttribute(&#039;src&#039;,l.protocol+&#039;//
www.instapaper.com/j/39jwr?u=&#039;+encodeURIComponent(l.href)+&#039;&amp;t=&#039;+(new
Date().getTime()));b.appendChild(z);}catch(e){alert(&#039;Please wait
until the page has loaded.&#039;);}}iprl5();void(0)"
class="bookmarklet"
onclick="return explain_bookmarklet();"
title="Read Later">Read Later</a>

What would be the Pentadactyl idiom for incorporating this
functionality?

Thanks!
Sean

Kris Maglione

unread,
Oct 18, 2010, 9:09:28 PM10/18/10
to Pentadactyl
On Mon, Oct 18, 2010 at 06:00:05PM -0700, scrotty wrote:
>Instapaper (instapaper.com) has a bookmarklet that they advise users
>drag to their bookmarks bar. When the user finds a page they'd like to
>read later, they simply click this button and it is "saved' on the
>instapaper site.
>...

>What would be the Pentadactyl idiom for incorporating this
>functionality?

Just give the bookmark a keyword and use that. You can even bind
a command or mapping to it, if that helps.

:com ip :open instapaper
:map -ex gI :open instapaper

--
Kris Maglione

The programmer, like the poet, works only slightly removed from pure
thought-stuff. He builds his castles in the air, from air, creating
by exertion of the imagination. Few media of creation are so
flexible, so easy to polish and rework, so readily capable of
realizing grand conceptual structures.
--Frederick P. Brooks, Jr.

Sean Crotty

unread,
Oct 18, 2010, 10:21:04 PM10/18/10
to penta...@googlegroups.com
On Mon, Oct 18, 2010 at 7:09 PM, Kris Maglione <magli...@gmail.com> wrote:
On Mon, Oct 18, 2010 at 06:00:05PM -0700, scrotty wrote:
Instapaper (instapaper.com) has a bookmarklet that they advise users
drag to their bookmarks bar. What would be the Pentadactyl idiom for incorporating this
functionality?

Just give the bookmark a keyword and use that. You can even bind a command or mapping to it, if that helps.

 :com ip :open instapaper
 :map -ex gI :open instapaper


Thanks, Kris. I thought that you might advise embedding the javascript in the rc or somesuch. This works great.

Kris Maglione

unread,
Oct 18, 2010, 10:27:35 PM10/18/10
to penta...@googlegroups.com
On Mon, Oct 18, 2010 at 08:21:04PM -0600, Sean Crotty wrote:
>Thanks, Kris. I thought that you might advise embedding the javascript in
>the rc or somesuch. This works great.

It's really not a good idea, and is generally a lot more trouble
than it's worth. To do it safely requires using the
evalInSandbox functionality of Gecko. You could, of course, just
:open the entire bookmarklet url, but I think that using
bookmark keywords is cleaner.

--
Kris Maglione

If buffer overflows are ever controlled, it won't be due to mere
crashes, but due to their making systems vulnerable to hackers.
Software crashes due to mere incompetence apparently don't raise any
eyebrows, because no one wants to fault the incompetent programmer and
his incompetent boss.
--Henry Baker

sme

unread,
Oct 21, 2010, 11:21:04 PM10/21/10
to Pentadactyl
I think Kris' suggestion is the easier route, however for vimperator I
used the following code in a plugin/instapaper.js file (hope
formatting isn't mangled):

(function() {
commands.addUserCommand(['readlater', 'rl'],
'Send current page to Instapaper',
function (args) {
var d=content.document;
var z=d.createElement('scr'+'ipt');
var b=d.body;

try {
if(!b)
throw(0);
d.title='(Saving...) '+d.title;
z.setAttribute('src', 'http://www.instapaper.com/j/
jp9gKeh1r8Hc'); /* instapaper api url */
b.appendChild(z);
}
catch(e) {
alert('Please wait until the page has loaded.');
}
}
);
})();

Kris Maglione

unread,
Oct 21, 2010, 11:37:19 PM10/21/10
to Pentadactyl
On Thu, Oct 21, 2010 at 08:21:04PM -0700, sme wrote:
>I think Kris' suggestion is the easier route, however for vimperator I
>used the following code in a plugin/instapaper.js file (hope
>formatting isn't mangled):

For the record, I do think that if you're going to write
something like this from scratch, then a command is a much
better option than a bookmarklet, especially given that it
remains easy to edit and doesn't require later mangling. But if
you already have a bookmarklet in hand, there's nothing to gain
by converting it to a command as such.

That said... I'm physically incapable of looking at a piece of
code without commenting on it, so I have to ask why the need for
the closure and the 'scr'+'ipt' hack (which I assume tends to
serve in avoiding certain content filters in the wild)? Also,
I'd really suggest not using alert in code meant for
Pentadactyl. Modal dialogs bug most users, and Pentadactyl/Vim
users more than most.

This is what I'd have written:

js <<EOF


commands.addUserCommand(['readlater', 'rl'],
'Send current page to Instapaper',
function (args) {

try {
let doc = content.document;
dactyl.assert(doc.body);
doc.title = '(Saving...) ' + d.title;
doc.body.appendChild(util.xmlToDom( /* instapaper api url */
<script type="application/javascript"
src="http://www.instapaper.com/j/jp9gKeh1r8Hc"/>,
doc));
}
catch(e) {
dactyl.echoerr('Please wait until the page has loaded.');
}
}, {}, true);
EOF

Or these days possibly,

com! rl -javascript
\ -desc 'Send current page to Instapaper'
\ try {
\ let doc = content.document;
\ dactyl.assert(doc.body);
\ doc.title = '(Saving...) ' + d.title;
\ doc.body.appendChild(util.xmlToDom( /* instapaper api url */
\ <script type="application/javascript"
\ src="http://www.instapaper.com/j/jp9gKeh1r8Hc"/>,
\ doc));
\ }
\ catch(e) {
\ dactyl.echoerr('Please wait until the page has loaded.');
\ }

--
Kris Maglione

I have always found that plans are useless, but planning is
indispensable.
--Dwight Eisenhower

sme

unread,
Oct 21, 2010, 11:44:23 PM10/21/10
to Pentadactyl
On Oct 21, 8:37 pm, Kris Maglione <maglion...@gmail.com> wrote:
> On Thu, Oct 21, 2010 at 08:21:04PM -0700, sme wrote:
> >I think Kris' suggestion is the easier route, however for vimperator I
> >used the following code in a plugin/instapaper.js file (hope
> >formatting isn't mangled):
>
> For the record, I do think that if you're going to write
> something like this from scratch, then a command is a much
> better option than a bookmarklet, especially given that it
> remains easy to edit and doesn't require later mangling. But if
> you already have a bookmarklet in hand, there's nothing to gain
> by converting it to a command as such.
>
> That said... I'm physically incapable of looking at a piece of
> code without commenting on it, so I have to ask why the need for
> the closure and the 'scr'+'ipt' hack (which I assume tends to
> serve in avoiding certain content filters in the wild)? Also,
> I'd really suggest not using alert in code meant for
> Pentadactyl. Modal dialogs bug most users, and Pentadactyl/Vim
> users more than most.

Agreed, although in my usage of it I have never once had it pop up.

>
> This is what I'd have written:

[snip] much better code example.

I think I like your version better anyway, mine was pieced together
from other code and a beginner knowledge of js.

thanks for the tips
-s
Reply all
Reply to author
Forward
0 new messages