Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    May 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question bookmarklet doesn't work

    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:
    Code:
    <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:

    Code:
    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(sBefore)+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:

    Code:
    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 (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!
    Last edited by noroom; 05-29-2005 at 12:02 AM. Reason: added link to my MSN Space

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It would have been helpful to have had a couple examples of the URLs you want manipulated, but this should do the trick:
    Code:
    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!
    Last edited by Harry Armadillo; 05-29-2005 at 07:01 AM.

  • #3
    New to the CF scene
    Join Date
    May 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

    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.
    Last edited by noroom; 05-29-2005 at 06:02 AM. Reason: formatting issues (urls)

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I made a minor change to the bookmarklet above -- works on those URL using Firefox, should work with IE too.

  • #5
    New to the CF scene
    Join Date
    May 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #6
    Regular Coder
    Join Date
    Feb 2005
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Code:
    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")}})()
    Last edited by Harry Armadillo; 05-30-2005 at 12:34 AM.

  • #7
    New to the CF scene
    Join Date
    May 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Harry, you are my hero!!
    Thank you so much for helping me out man, it finally works on IE too!

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •