Drupal "Blog this" bookmarklet v2.0 (ish)
My earlier bookmarklet spawned a fair bit of activity. Yay me! It has also been reworked by Valerie to make it more suitable for her purposes, and she's got it working properly with 'unclean' URLs. Nice one, Drupal community!
However, I found a problem the other day - if a webpage's title has a pipe character in it, attempting to 'Blog This' causes an 'internal server error'. It's something to do with pipes being used to move data between processes on Unix-like operating systems, I expect. They appear perfectly when you're typing away, but if they're involved with hyperlinks (like I've used to head up the blockquote section), everything fails ungracefully.
My solution is to use Javascript's replace function to replace any occurance of the pipe in the title with a double colon instead, so "Blah Blah | The Register" becomes "Blah Blah :: The Register". It works well so far! If you fancy a tinker, just update this section of the Javascript in the bookmarklet.
Here's the code, once again for peer review =) I've not added Valerie's modification for unclean URLs because it uses 'story' instead of 'blogs' and, having clean URLs working, it's hard for me to test and verify. That said, I've tried it and it seems a fine base for tinkering with. If you need the unclean URL support, head over to Valerie's website.
javascript:
u=document.location.href;
t=document.title.replace(/\|/g,'::');
s='<blockquote>'+window.getSelection()+'</blockquote>';
pre='<em>From <a target="_blank" href="'+escape(u)+'">'+escape(t) + escape('</a>:</em><br /> ');
void(window.open('http://www.example.com/node/add/blog?edit[title]='+escape('Link: '+t)+'&edit[body]='+pre+escape(s)+escape('<a href="'+u+'" target="_blank">Link</a>'),'_blank','width=710,height=500,status=yes,resizable=yes,scrollbars=yes'));
I'd like to be able to offer you a bookmarklet ready to drag to the links bar, but here's the deal: I can't. There's a pipe character in there, and as mentioned before, this breaks things when in links. The best I can do is offer you the code, as above, on one long line to make copying and pasting into a bookmark's properties a whole lot easier.
javascript: u=document.location.href; t=document.title.replace(/\|/g,'::'); s='<blockquote>'+window.getSelection()+'</blockquote>'; pre='<em>From <a target="_blank" href="'+escape(u)+'">'+escape(t) + escape('</a>:</em><br /> '); void(window.open('http://www.example.com/node/add/blog?edit[title]='+escape('Link: '+t)+'&edit[body]='+pre+escape(s)+escape('<a href="'+u+'" target="_blank">Link</a>'),'_blank','width=710,height=500,status=yes,resizable=yes,scrollbars=yes'));Once again I remind you to change www.example.com to the correct address for your Drupal website, and other caveats noted before still apply.
Thanks everybody for trying this out, and I welcome any and all feedback - working together makes us more rocking (or something).
- Irregular Shed's blog
- Login to post comments






pipe problem server dependent?
Hi, I also responded to you at my blog. I'm not having the pipe problem. Could it be server dependent? My site is on a linux server running apache 2. It might also be the case that I don't understand the pipe problem... I made a test node by quoting the register.
You might be right, you know.
I'm hosting on a similarly set-up server, but these things can be set up in so many ways... I probably should try on a different server to be on the safe side!
Hey ho - it works at least =)
I've been looking for a way
I've been looking for a way to do this ever since moving off of Typepad, and this showed the best promise for what I needed.
I tried this bookmarklet, and I found a problem. It didn't pull in the quoted text, and thus didn't create a link either.
It did pull in the page title, and everything else works, so I'm not sure what's up.
Thanks for any help you can offer.
Are you using Internet Explorer?
If you are, that's probably the problem. IE doesn't support the window.getSelection() command - to get the text that's been selected you have to leap through some more hoops. It's explained quite well here.
I'd try writing a cross-platform bookmarklet, but I don't have any plans to use Internet Explorer ;-)
No IE
Thanks for your reply. I'm using Firefox 1.5, and like you, have no plans to use IE.
I'm stumped then!
You could try swapping window.getSelection() for document.getSelection() - that might work (that's the way Safari does it). Otherwise... no idea, sorry!
If you use one machine more than others, you could try Performancing - that's what I use quite a lot these days.
I tried that switch, no
I tried that switch, no luck. I'm going to experiment with modifying my old Typepad bookmarklet to see why it works and this doesn't. I will also try Performancing. Thanks for your help.
javascript:q=""+(window.getSe
javascript:q=""+(window.getSelection?window.getSelection():document.getSelection?document.getSelection():document.selection.createRange().text);location.href='http://www.myspizace.com/node/add/userlink?edit[url]='+encodeURIComponent(location.href)+'&edit[title]='+encodeURIComponent(document.title)+'&edit[body]='+q;
this code with prepopulate module work like a charm... but i'm not skilled with javascript and i need to open the link in a new focused window. How can i do this?
thanks in advance!
thanks for information...
thanks for information...