Created
July 18, 2009 13:01
-
-
Save nanto/149553 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name bit.ly referred | |
// @namespace http://nanto.asablo.jp/blog/ | |
// @include http://* | |
// ==/UserScript== | |
var apiKey = '&login=___&apiKey=___'; | |
var siteinfo = GM_getValue('siteinfo', ''); | |
if (siteinfo) { | |
callback(siteinfo); | |
} else { | |
console.log('no siteinfo'); | |
GM_xmlhttpRequest({ | |
method: 'GET', | |
url: 'http://wedata.net/databases/LDRize/items.json', | |
onload: function (res) { | |
console.log('got siteinfo'); | |
GM_setValue('siteinfo', res.responseText); | |
callback(res.responseText); | |
} | |
}); | |
} | |
function callback(siteinfo) { | |
console.log('callback begin'); | |
var loc = location.href; | |
var sites = eval(siteinfo); | |
var siteData = null; | |
for (var i = 0; i < sites.length; i++) { | |
var data = sites[i].data; | |
if (loc.match(data.domain)) { | |
siteData = data; | |
break; | |
} | |
} | |
if (!siteData) return; | |
console.log('paragraph = ' + data.paragraph); | |
$X(data.paragraph).forEach(function (p) { | |
var link = data.link ? $X(data.link, p)[0] : p; | |
var href = link.href; | |
console.log('href = ' + href); | |
GM_xmlhttpRequest({ | |
method: 'GET', | |
url: 'http://api.bit.ly/shorten?version=2.0.1&longUrl=' + encodeURIComponent(href) + apiKey, | |
onload: function (res) { | |
var hash = eval('(' + res.responseText + ')').results[href].hash; | |
console.log([href, hash].join('\n')); | |
GM_xmlhttpRequest({ | |
method: 'GET', | |
url: 'http://api.bit.ly/stats?version=2.0.1&hash=' + hash + apiKey, | |
onload: function (res) { | |
console.log(res.responseText); | |
var count = JSON.parse(res.responseText).results.clicks; | |
var text = document.createTextNode(" " + count + ' clicks'); | |
link.parentNode.insertBefore(text, link.nextSibling); | |
} | |
}); | |
} | |
}); | |
}); | |
console.log('end'); | |
} | |
function $X(xpath, context) { | |
var res = document.evaluate(xpath, context || document, null, 7, null); | |
var result = []; | |
for (var i = 0; i < res.snapshotLength; i++) | |
result.push(res.snapshotItem(i)); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment