/** * Converts readable Javascript code into a valid bookmarklet link * * Uses https://github.com/ozh/bookmarkletgen * * @since 1.7.1 * @param string $code Javascript code * @return string Bookmarklet link */ function yourls_make_bookmarklet($code) { if (!class_exists('BookmarkletGen', false)) { require_once YOURLS_INC . '/BookmarkletGen/BookmarkletGen.php'; } $book = new BookmarkletGen(); return $book->crunch($code); }
<div class="form-group"> <p>Enter Javascript text to crunch into a bookmarklet link</p> <textarea class="form-control" cols="80" rows="10" name="code"><?php echo htmlentities($post); ?> </textarea> </div> <button type="submit" class="btn btn-success">Crunch</button> </form> <?php if ($post) { ?> <hr/> <?php include dirname(__DIR__) . '/src/BookmarkletGen.php'; $book = new BookmarkletGen(); $link = $book->crunch($post); printf('<p>Test your bookmarklet: <a href="%s">bookmarklet</a></p>', $link); ?> <?php } ?> </div> </body> </html>
<?php $javascript = <<<CODE a="http://héhé.fr"; // this is a comment // Let's define a function // that's a "literal string quoted" in a comment function pop_it( val ) { \talert ( 'this is a "test" my dear: ' + val + " is the value !" ); } /* multi line comments should be killed too */ pop_it( a ); // lots of spaces before and after CODE; include dirname(__DIR__) . '/src/BookmarkletGen.php'; $book = new BookmarkletGen(); $link = $book->crunch($javascript); printf('<a href="%s">bookmarklet</a>', $link);