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?

3 Answers 3

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...

Sign up to request clarification or add additional context in comments.

3 Comments

This works. But what happens to the = if you do not replace it with anything. Is it not needed?
=is padding, and isn't strictly needed. Check here for explanation of padding. PHP can decode Base64 without padding.
Hmm, that's a pity... The function window.btoa(s) isn't supported by IE8. Sorry.
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 …

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.