TinyURL Popup Preview

By Yuichirou Last update Nov 23, 2008 — Installed 3,389 times. Daily Installs: 1, 3, 0, 3, 1, 0, 0, 2, 1, 2, 0, 2, 2, 3, 1, 4, 1, 2, 4, 4, 0, 0, 4, 4, 5, 1, 0, 1, 0, 3, 3, 1

There are 2 previous versions of this script.

// ==UserScript==
// @name           TinyURL Popup Preview
// @namespace      http://d.hatena.ne.jp/Yuichirou/
// @description    This script enables you to check *safely* where the TinyURL links redirect to (as a TITLE attribute popup).
// @include        http://*
// @include        https://*
// ==/UserScript==

var links = document.evaluate("//a[contains(@href,'tinyurl.com')]",
                              document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

for (var i = 0; i < links.snapshotLength; i++) {
  getFullURL(links.snapshotItem(i));
}

function getFullURL(link) {
  var re = decodeURIComponent(link.href).match(/\/tinyurl\.com\/([0-9a-z]+)/);
  if (!re || re[1] == "preview") return;
  var title = link.getAttribute("title");
  if (title == "") link.setAttribute("title", "loading...");

  GM_xmlhttpRequest({
    method: 'GET',
    url: 'http://tinyurl.com/preview.php?num=' + re[1],
    onload: function(res) {
      var re = res.responseText.match("<blockquote><b>(.+)</b></blockquote>");
      if (re) {
        var url = re[1].replace(/\<br \/\>/g, "").replace(/&amp;/g, "&");
        if (title != "") {
          link.setAttribute("title", link.getAttribute("title") + " (link to " + url + ")");
        } else {
          link.setAttribute("title", "(link to " + url + ")");
        }
      } else if (title == "") {
        link.setAttribute("title", "failed...");
      }
    },
    onerror: function() {
      if (title == "") {
        link.setAttribute("title", "failed...");
      }
    }
  });
}