Permalink
Browse files

Add some help to the homepage in relation to using the API. Add a boo…

…kmarklet and add handling in page for receiving bookmarklet requests
  • Loading branch information...
1 parent c682193 commit d7d9e0cde3ea94e4ad6efde643a4943b4dc49909 @lozzd committed May 18, 2011
Showing with 35 additions and 1 deletion.
  1. +35 −1 index.php
View
@@ -12,7 +12,7 @@
<body>
<h1>URL Shortener</h1>
<form method="post" action="shorten.php" id="shortener">
-<label for="longurl">URL to shorten</label><br /> <input type="text" name="longurl" id="longurl" ><br /> <input type="submit" id="shortenbutton" value="Shorten">
+<label for="longurl">URL to shorten</label><br /> <input type="text" name="longurl" id="longurl" value="<? echo $_GET['longurl'] ?>" ><br /> <input type="submit" id="shortenbutton" value="Shorten">
</form>
</form>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
@@ -34,5 +34,39 @@
</script>
<br />
<div id="statsbox" name="statsbox"><div>
+<br />
+<br />
+<h3>Bookmarklet</h3>
+<?php
+$name_parts = explode('.', $_SERVER['HTTP_HOST']);
+$bookmark = $name_parts[0];
+$bookmarklet = "<a href=\"javascript:void(location.href='http://" . $_SERVER['HTTP_HOST'] . "/?b=1&longurl='+encodeURIComponent(location.href))\">";
+?>
+<p>Drag <?php echo $bookmarklet ?>Shorten</a> to your bookmark toolbar to create a button that easily shortens URLs</p>
+<h3>API</h3>
+<p>Easily get a short URL by hitting http://<?php echo $_SERVER['HTTP_HOST'] ?>/shorten.php with the variable "longurl" as the URL-encoded URL you wish to shorten. </p>
+<p>For example, in PHP:<br />
+<pre>$shorturl = file_get_contents('http://<?php echo $_SERVER['HTTP_HOST'] ?>/shorten.php?longurl=' . urlencode($url));</pre></p>
+
+<?php
+
+if ($_GET['b'] == 1) {
+?>
+<script type="text/javascript">
+$.ajax({data: {longurl: $('#longurl').val()}, url: 'shorten.php', complete: function (XMLHttpRequest, textStatus) {
+ $('#statsbox').html('<li>' + $('#longurl').val() + ' shortened to ' + XMLHttpRequest.responseText + '</li>' +
+ '<li>Copy to clipboard using CMD/CTRL + C now</li>' +
+ '<li><a href="' + XMLHttpRequest.responseText + '+">View stats</a></li>');
+ $('#shortenbutton').hide();
+ $('#longurl').val(XMLHttpRequest.responseText);
+ $('#longurl').focus();
+ $('#longurl').select();
+ }});
+</script>
+<?;
+
+}
+
+?>
</body>
</html>

0 comments on commit d7d9e0c

Please sign in to comment.