...

View Full Version : bookmarklet doesn't work



noroom
05-29-2005, 12:00 AM
i was browsing the forums for a while, and some people seem to be very experienced with Javascript. I only started playing with it last night, so I need you to help me a little...

if you put this in a webpage:

<A HREF='javascript:alert("w00t w00t pie!");'>Click or bookmark this</A>and then bookmark the link, you'll get a bookmarklet that shows an alert.

I wanted to add a button to my "Links" bar, that takes the address, extracts some strings from it, and sends me to another webpage, using some information extracted from the current location.

my first try looked like this:


if(window.location.href.indexOf("http://spaces.msn.com/members/")==0){
var loc=window.location.href


if(window.location.href.indexOf("/Blog/")>0){

var User=loc.substr(loc.indexOf("members/")+8,loc.indexOf("/Blog/",loc.indexOf("members/")+1)-loc.indexOf("members/")-8);
var Handle=loc.substr(loc.indexOf("Blog/")+5,loc.indexOf(".entry",loc.indexOf("Blog/")+1)-loc.indexOf("Blog/")-5);


window.location.href=("http://spaces.msn.com/members/"+User+"/PersonalSpace.aspx?_c11_BlogPart_handle="+Handle+"&_c11_BlogPart_blogpart=blogentry&_c11_BlogPart_frompart=myspace&_c=BlogPart&_c02_owner=1");

}else{

var User=loc.substr(loc.indexOf("members/")+8,loc.indexOf("/PersonalSpace.aspx",loc.indexOf("members/")+1)-loc.indexOf("members/")-8);
var Handle=loc.substr(loc.indexOf("handle=")+7,loc.indexOf("&",loc.indexOf("handle=")+1)-loc.indexOf("handle=")-7);

window.location.href=("http://spaces.msn.com/members/"+User+"/Blog/"+Handle+".entry");
}
}

function getbetween(sSource,sBefore,sAfter){
if((sSource.indexOf(sAfter,sSource.indexOf(sBefore)+1))>0){
return (sSource.substr((sSource.indexOf(sBefore)+sBefore.length),(sSource.indexOf(sAfter,sSource.indexOf(sB efore)+1)-sSource.indexOf(sBefore)-sBefore.length)));
} else {
return sSource.substr(sSource.indexOf(sBefore)+sBefore.length,sSource.length);
}
}

of course that didn't look too bookmarklet-friendly to me, so I converted it to this:


if(window.location.href.indexOf("http://spaces.msn.com/members/")==0){if(window.location.href.indexOf("/Blog/")>0){window.location.href=("http://spaces.msn.com/members/"+window.location.href.substr(window.location.href.indexOf("members/")+8,window.location.href.indexOf("/Blog/",window.location.href.indexOf("members/")+1)-window.location.href.indexOf("members/")-8)+"/PersonalSpace.aspx?_c11_BlogPart_handle="+window.location.href.substr(window.location.href.indexOf("Blog/")+5,window.location.href.indexOf(".entry",window.location.href.indexOf("Blog/")+1)-window.location.href.indexOf("Blog/")-5)+"&_c11_BlogPart_blogpart=blogentry&_c11_BlogPart_frompart=myspace&_c=BlogPart&_c02_owner=1");}else{window.location.href=("http://spaces.msn.com/members/"+window.location.href.substr(window.location.href.indexOf("members/")+8,window.location.href.indexOf("/PersonalSpace.aspx",window.location.href.indexOf("members/")+1)-window.location.href.indexOf("members/")-8)+"/Blog/"+window.location.href.substr(window.location.href.indexOf("handle=")+7,window.location.href.indexOf("&",window.location.href.indexOf("handle=")+1)-window.location.href.indexOf("handle=")-7)+".entry");}}

now, if i embed that into the HTML of my MSN Space (http://spaces.msn.com/members/noroom) (online blog) and then click on the link, it works (it takes me to the edit page for that post) but if i click it from within my bookmarks it doesn't work. I really don't want to add this link to every post I make, so bookmarklets are the only way to go.

Anyone can help me out here, please? I stayed up until 3 am last night trying to figure out why it won't do anything! :(

Harry Armadillo
05-29-2005, 03:17 AM
It would have been helpful to have had a couple examples of the URLs you want manipulated, but this should do the trick:
javascript:(function(){var n,u,h,a='http://spaces.msn.com/members/',l=window.location.href;if(l.match(a)){if(l.match(/blog\//i)){u=l.replace(/.+members\/(\w+)\/blog.+/i,"$1");h=l.replace(/.+blog\/([a-z0-9!_-]+)\.entry.*/i,"$1");n=a+u+"/PersonalSpace.aspx?_c11_BlogPart_handle="+h+"&_c11_BlogPart_blogpart=blogentry&_c11_BlogPart_frompart=myspace&_c=BlogPart&_c02_owner=1"}else{u=l.replace(/.+members\/(\w+)\/personalspace.+/i,"$1");h=l.replace(/.+handle=([^&]+).*/i,"$1");n=a+u+"/Blog/"+h+".entry"}window.location=n}})()

Remember to link back to codingforums! :)

noroom
05-29-2005, 06:01 AM
Harry Armadillo, thank you so much for your reply!!!

I'm so sorry, it was kinda dumb of me not to post the URLs I'm trying to handle. I already wrote a program in VB.NET that does this, but it would be really neat if I managed to port it to Javascript, to have this functionality added to IE in the form of a Bookmarklet or Favlet. And don't worry, I'll link to CF once I get it working! :D

for example, I want to be able to turn (either one of) these URLs: (edit mode)

http://spaces.msn.com/members/noroom/PersonalSpace.aspx?_c11_BlogPart_handle=cns!1pm37hXcDmSG_fVVGV-7UPAQ!1770&_c11_BlogPart_blogpart=blogentry&_c11_BlogPart_frompart=myspace&_c=BlogPart&_c02_owner=1

http://spaces.msn.com/members/noroom/PersonalSpace.aspx?_c11_BlogPart_blogpart=blogentry&_c=BlogPart&_c02_owner=1&handle=cns!1pm37hXcDmSG_fVVGV-7UPAQ!1903

Into this one: (preview mode)

http://spaces.msn.com/members/noroom/Blog/cns!1pm37hXcDmSG_fVVGV-7UPAQ!1770.entry

And vice-versa (using the first example for the to_edit-mode procedure). The code I posted manages to do it from within an HTML website, but doesn't do anything from within a bookmarklet. :(

Harry Armadillo
05-29-2005, 07:06 AM
I made a minor change to the bookmarklet above -- works on those URL using Firefox, should work with IE too.

noroom
05-29-2005, 03:10 PM
Harry Armadillo, I gave your script a try in Firefox and it works! I just changed the single quotes you used for variable a for double quotes, because it was causing an error when linked in the form of HREF='javascript:blah'

But then I tried my code in Firefox, the huge neverending line I posted in my first post, and it works too! But it doesn't work on IE!

God, oh god why not? :confused:

Harry Armadillo
05-30-2005, 12:12 AM
IE keeps reducing the length allowed. It works in IE5.5, but in IE6 the bookmarklet is too long. I'll see if I can slim it down....

Okay, this should be small enough. If I shrink it anymore, even I won't understand how/what it's doing.
javascript:(function(){var b="_blogpart",a="http://spaces.msn.com/members/",l=window.location.href;if(l.match(a)){window.location=a+l.replace(/.+mbers\/([^\/]+).*/,"$1")+((l.match(/blog\//i))?"/PersonalSpace.aspx?_c11"+b+"_handle="+l.replace(/.+blog\/([a-z0-9!_-]+)\.entry.*/i,"$1")+"&_c11"+b+b+"=blogentry&_c11"+b+"_frompart=myspace&_c=BlogPart&_c02_owner=1":"/Blog/"+l.replace(/.+handle=([^&]+).*/i,"$1")+".entry")}})()

noroom
05-30-2005, 01:47 AM
Harry, you are my hero!!
Thank you so much for helping me out man, it finally works on IE too! :D

You're awesome! CodingForums is awesome!!! :thumbsup:
AHH, IT'S A GOOD DAY!! :cool:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum