5

I would like to create a bookmarklet for adding bookmarks. So you just click on the Bookmark this Page JavaScript Snippet in your Bookmarks and you are redirected to the page.

This is my current bookmarklet:

"javascript: location.href='http://…/bookmarks/add/'+encodeURIComponent(document.URL);"

This gives me an URL like this when I click on it on the Bookmarklet page:

http://localhost/~mu/cakemarks/bookmarks/add/http%3A%2F%2Flocalhost%2F~mu%2Fcakemarks%2Fpages%2Fbookmarklet

The server does not like that though:

The requested URL /~mu/cakemarks/bookmarks/add/http://localhost/~mu/cakemarks/pages/bookmarklet was not found on this server.

This gives the desired result, but is pretty useless for my use case:

http://localhost/~mu/cakemarks/bookmarks/add/test-string

There is the CakePHP typical mod_rewrite in progress, and it should transform the last part into a parameter for my BookmarksController::add($url = null) action.

What am I doing wrong?

Martin Ueding
  • 8,245
  • 6
  • 46
  • 92

3 Answers3

2

I had a similar problem, and tried different solutions, only to be confused by the cooperation between CakePHP and my Apache-config.

My solution was to encode the URL in Base64 with JavaScript in browser before sending the request to server.

Your bookmarklet could then look like this:

javascript:(function(){function myb64enc(s){s=window.btoa(s);s=s.replace(/=/g, '');s=s.replace(/\+/g, '-');s=s.replace(/\//g, '_');return s;} window.open('http://…/bookmarks/add/'+myb64enc(window.location));})()

I make two replacements here to make the Base64-encoding URL-safe. Now it's only to reverse those two replacements and Base64-decode at server-side. This way you won't confuse your URL-controller with slashes...

poplitea
  • 3,585
  • 1
  • 25
  • 39
  • This works. But what happens to the `=` if you do not replace it with anything. Is it not needed? – Martin Ueding Aug 31 '11 at 12:19
  • `=`is padding, and isn't strictly needed. Check [here](http://en.wikipedia.org/wiki/Base64#Padding) for explanation of padding. PHP can decode Base64 without padding. – poplitea Aug 31 '11 at 12:57
  • Hmm, that's a pity... The function window.btoa(s) isn't supported by IE8. Sorry. – poplitea Aug 31 '11 at 13:01
2

Bases on poplitea's answer I translate troubling characters, / and : manually so that I do not any special function.

function esc(s) {
    s=s.replace(/\//g, '__slash__');
    s=s.replace(/:/g, '__colon__');
    s=s.replace(/#/g, '__hash__');
    return s;
}

In PHP I convert it back easily.

$url = str_replace("__slash__", "/", $url);
$url = str_replace("__colon__", ":", $url);
$url = str_replace("__hash__", "#", $url);

I am not sure what happens with chars like ? and so …

Martin Ueding
  • 8,245
  • 6
  • 46
  • 92
0

Not sure, but hope it helps you should add this string to yout routs.php

Router::connect (
  '/crazycontroller/crazyaction/crazyparams/*',
  array('controller'=>'somecontroller', 'action'=>'someaction')
);

and after that your site will able to read url like this

http://site.com/crazycontroller/crazyaction/crazyparams/http://crazy.com
sukinsan
  • 513
  • 3
  • 14