Help me untangle a few weirdities in my fine, freshly hand-rolled Cuban cigar ... er ... Javascript bookmarklet.
I need some assistance doing some slight tweaks to a JavaScript bookmarklet that I hand-rolled from a
bookmarklet, a
Greasemonkey script, and some dumb luck.
The Javascript bookmarklet code, when given some whitespace for readability, is:
javascript:h=location.href;
t=document.title;
e = "" + (window.getSelection ? window.getSelection() : document.getSelection ? document.getSelection() : document.selection.createRange().text);
e = e.replace(/\"/g, "'");
e = e.replace(/\xa0/g, " ");
e = e.replace(/\xa9/g, "\(c\)");
e = e.replace(/\xae/g, "\(r\)");
e = e.replace(/\xb7/g, "*");
e = e.replace(/\u2018/g, "'");
e = e.replace(/\u2019/g, "'");
e = e.replace(/\u201c/g, "'");
e = e.replace(/\u201d/g, "'");
e = e.replace(/\u8220/g, "'");
e = e.replace(/\u8221/g, "'");
e = e.replace(/\u2026/g, "...");
e = e.replace(/\u2002/g, " ");
e = e.replace(/\u2003/g, " ");
e = e.replace(/\u2009/g, " ");
e = e.replace(/\u2012/g, "--");
e = e.replace(/\u2013/g, "--");
e = e.replace(/\u2014/g, "--");
e = e.replace(/\u2015/g, "--");
e = e.replace(/\u2122/g, "\(tm\)");
if (e!=null) location="http://del.icio.us/WCityMike?jump=close&url=" + encodeURIComponent(h) + "&title=" + encodeURIComponent(t) + "&extended=\"" + encodeURIComponent(e).replace(/ /g, "+") + "\"";
void 0
(Of course, when it becomes a bookmarklet, I replace spaces with percentage-sign-20 and quotes with percentage-sign-22.)
If that doesn't come across properly, forgive the self-link, but the script can also be found
here.
The purpose of this bookmarklet is to take the selected text, do some swapouts of fancy stuff for plain stuff, and toss it into del.icio.us. It also is supposed to swap out double quotation marks in the selected text for single quotation marks, because it then surrounds the selected text with quotation marks for the excerpt. So, for example, if I selected the text:
"Crash" won last night's Oscar ceremony in a surprise upset.
The bookmarklet should take the text into del.icio.us as:
"'Crash' won last night's Oscar ceremony in a surprise upset."
I basically mashed together this bookmarklet because I use del.icio.us more as a linklog publishing system than as an online bookmark manager, and I wanted to automate the process of filling out the description field.
I'm looking for two tweaks in particular.
First, I'd like it to automatically remove any line feeds or carriage returns in the selected text. I use a variant of this bookmarklet with the Mac OS X application
Cocoalicious, and line feeds or carriage returns (I'm not sure which is which) get preserved and carried into the description field. (I do not think this is Cocoalicious' fault, as the JavaScript search-and-replace stuff that goes on happens before the data's passed off to the cocoalicious: URL, which when triggered prompts Cocoalicious to open with the appropriate data.) I'd prefer no carriage returns get carried over. How do I do that?
Secondly, I've found that on some webpages - perhaps 35-45% of them -- it does not catch double quotation marks, and I'm not sure what I'm missing. An example of text it doesn't match is the phrase:
You'll never miss a song even in the bathroom with Atech's "iLounge hybrid toilet paper dispenser/iPod dock".
on
this webpage. (Keep in mind the quotes in the above phrase may have been fancy-fied by MetaFilter itself; if you're helping, you may want to go to the original webpage to see the exact characters I'm speaking of.)
I used a program called
UnicodeChecker, which, amongst its features, will supposedly tell you the Unicode value of characters in pasted text. I used that to look at the apostrophes on this page, and if I'm reading my code right, the bookmarklet
should catch these quotation marks. It's not.
Finally, I'm obviously not a coder. If you have any tweaks that will optimize the code yet preserve its understandability for a newbie like me, or if you know of any other good resources or examples to include in the "dumbification" process (i.e., turn this code into this plain text), please feel free to mention them.
If there's a particularly recalcitrant code that is not going through Metafilter's posting box well, feel free to e-mail it to me. My address is in my profile.
Much obliged in advance!
Thank you!
posted by misterbrandt at 12:46 PM on March 7, 2006