Friday, January 13, 2017

3shrink: adventures in one click shrinking

While I'm pleased with the core functionality of 3shrink, it definitely needed a faster interface. That is, a way to effortlessly encode and decode 3shrink URLs, as well as bits of text. For now I've only solved half the problem, the shrinking side of things. The solution was straightforward: create a couple of bookmarklets. Here they are in action.

With these bookmarklets in place, I'm only one click away from a 3shrink code.

The implementation of the bookmarklets is nearly trivial. Here's the PHP snippet that powers them both:

<?php
/*
 * A PHP file for generating a URL shrinking bookmarklet
 */
$code = <<<EOF
(function(){
  window.location = 
    'http://{$_SERVER['SERVER_NAME']}/shrink?i=' + encodeURIComponent($src)
})()
EOF;
$code = esc_attr($code);
?>
<a href="javascript:<?= $code ?>">3shrink <?= $label ?></a>

And here's the calling code that renders the bookmarklets in the footer:

<div class="footer">
  <a href="/">Home</a> | <a href="http://blogbyben.com/">Built By Ben</a> |
  Bookmarklets: 
  [<?= snippet('bookmarklet',
       ['label' => "URL", 'src' => 'window.location']) ?>]
  [<?= snippet('bookmarklet', 
       ['label' => "TXT", 'src' => "prompt('Text to Shrink')"]) ?>]
</div>

Installing the bookmarklets is as simple as browsing to your preferred instance of 3shrink, say http://projx.3shrink.com/ and dragging the appropriate links from the footer into your bookmark toolbar.

You'll want to note that the bookmarklet's a domain specific. That is, the bookmarklets in the footer of the site http://projx.3shrink.com register URLs with a different context than http://personal.3shrink.com.

With the shrinking side of things streamlined, the next area to focus on is how to quickly expand 3shrink codes. Stay tuned for that solution.

No comments:

Post a Comment