Skip to content


Repository files navigation

Bookmarklet Gen Tests Codacy Badge

Convert (readable) Javascript code into bookmarklet links

Requires PHP 7.2 - 8.1


  • removes comments
  • compresses code by removing extraneous spaces, but not within literal strings. Example:
    function   someName(   param   ) {
     alert( "this is a string" )
    will return:
  • encodes what needs to be encoded
  • wraps code into a self invoking function ready for bookmarking

This is basically a slightly enhanced PHP port of the excellent Bookmarklet Crunchinator:


If you are using Composer, add this requirement to your composer.json file and run composer install:

        "require": {
            "ozh/bookmarkletgen": "1.2.0"

Or simply in the command line : composer install ozh/bookmarkletgen

If you're not using composer, download the class file and include it manually.


$javascript = <<<CODE
var link=""; // destination
window.location = link;

require 'vendor/autoload.php'; // if you install using Composer
require 'path/to/Bookmarkletgen.php'; // otherwise

$book = new \Ozh\Bookmarkletgen\Bookmarkletgen;
$link = $book->crunch( $javascript );

printf( '<a href="%s">bookmarklet</a>', $link );

will print:

<a href="javascript:(function()">bookmarklet</a>


This library comes with unit tests to make sure the resulting crunched Javascript is valid code under various PHP versions.

Current version supports PHP 7.2+ (previous releases were tested with PHP 5.3 - 7.1)


Do whatever the hell you want to do with it