...

View Full Version : Bookmarklet Parse URL



SimonDorfman
03-16-2009, 06: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!

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

javascript:void(window.open((location.href.replace(/\w+:\/\/[w{3}\.]*/,'https://authoring.')).replace(/(\/[^\/]+$)|\/$/,'/login.cfm'),''),'0');

and for the one to open in the same window:

javascript:location=(location.href.replace(/\w+:\/\/[w{3}\.]*/,'https://authoring.')).replace(/#.*/,'');

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

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


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

In bookmarklet form:

javascript:%20javascript:void((function()%20{var%20a=location.href.replace(/^http\:\/\/example.com\/(.*)$/,"$1");location.href="https://authoring.example.com/"+escape(a);})())

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!

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

SimonDorfman
03-17-2009, 05: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...

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum