View Full Version : Bookmarklet Parse URL

03-16-2009, 07:14 PM
I'm trying to create two bookmarklets:

1. Take the current URL of the page and open a new window with a URL based on the current page. Some examples (I use "->" to mean "this URL turns into that URL"):

http://example.com/som/faculty_staff.cfm#HeaderTitle -> https://authoring.example.com/som/login.cfm
http://example.com/som/faculty_staff.cfm -> https://authoring.example.com/som/login.cfm
http://example.com/som/ -> https://authoring.example.com/som/login.cfm
http://example.com/news/webpublishing/index.cfm -> https://authoring.example.com/news/webpublishing/login.cfm

2. Take the current URL of the page and open a URL (in the same window) based on the current page. Some examples:

http://example.com/som/faculty_staff.cfm#HeaderTitle -> https://authoring.example.com/som/faculty_staff.cfm
http://example.com/som/faculty_staff.cfm -> https://authoring.example.com/som/faculty_staff.cfm
http://example.com/som/ -> https://authoring.example.com/som/
http://example.com/news/webpublishing/index.cfm -> https://authoring.example.com/news/webpublishing/index.cfm

More notes:
I plan to use these bookmarklets in sequence, first pressing 1 to log into the CMS, then pressing 2 to edit the current page.

I started trying to figure out these problems using regexp, but I eventually gave up since regexp still confuses me to no end... Perhaps there's a non-regexp way to solve this problem? Or perhaps some regexp folks can lend a hand? I didn't see a regexp section to this forum...

Many thanks in advance for any help!

03-17-2009, 04:15 PM
For the one to open in new window:


and for the one to open in the same window:


Those should also work in case of www.example.com domain [not only example.com]

03-17-2009, 05:57 PM
I figured out the solution to bookmarklet #2:

var a=location.href.replace(/^http\:\/\/example.com\/(.*)$/,"$1");location.href="https://authoring.example.com/"+escape(a);

In bookmarklet form:


The way I figured this out was finding some good code to copy and build off of. Namely, this Google Cache bookmarklet (http://rentzsch.com/notes/googleCacheHacking).

Still scratching my head over bookmarklet #1.

Any help would be much appreciated!

03-17-2009, 06:01 PM
I posted both bookmarklets, is there anything wrong with them?

03-17-2009, 06:09 PM
Thank you so much, freedom_razor! As I was writing my follow up post, you must have just posted your reply. These both work great!

One odd thing was that after using the login.cfm one, I was getting an error from the CMS. I think it might be from the referrer being a non-https address.

I think I need to add something to set document.referrer to nothing... Still looking into this...

03-17-2009, 06:18 PM
AFAIK document.referrer is a read-only property.