Alexa Traffic Rank for links

Show alexa traffic rank for all links

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name           Alexa Traffic Rank for links
// @namespace      http://userscripts.org/users/75950
// @description    Show alexa traffic rank for all links
// @include        *
// @version 0.0.1.20140527152834
// ==/UserScript==

function htmltocontext(source) {
   // Thanks Seniltai

   var dt = document.implementation.createDocumentType("html", "-//W3C//DTD HTML 4.01 Transitional//EN", "http://www.w3.org/TR/html4/loose.dtd");
   var doc = document.implementation.createDocument('','', dt);
   html = doc.createElement('html');
   html.innerHTML = source;
   doc.appendChild(html);

   return doc;
} 

window.addEventListener('load', addMouseOverListeners, false);

function addMouseOverListeners() {
   var theLinks = document.links;

   for(var i=0; i<theLinks.length; i++) {
	var gmatch = theLinks[i].href.match( /http.*:\/\/(.[^/]+)\// );
	var linkDomain = '';
	if(gmatch) linkDomain = gmatch[1];
	if(document.domain != linkDomain) theLinks[i].addEventListener('mouseover', handleMouseOver, false);
   }
}

function handleMouseOver(event) {
	var theTarget = event.target;
	if (theTarget.tagName == 'IMG') theTarget = theTarget.parentNode;
	if (theTarget.tagName != 'A') return;
	var gmatch = theTarget.href.match( /http.*:\/\/(.[^/]+)\// );
	var linkDomain = '';
	if(gmatch) linkDomain = gmatch[1];
   
	GM_xmlhttpRequest({
	  method: 'GET',
	  url: 'http://www.alexa.com/siteinfo/'+linkDomain,
	  onload: function ( xhr ) {
		var theDoc = htmltocontext(xhr.responseText);
		var theDivHTML = theDoc.getElementById('siteStats').getElementsByClassName('data')[1].innerHTML;
		theDivHTML = theDivHTML.replace(/^\s*|\s*$/gi, '');
		gmatch = theDivHTML.match( /.*>\s*(.+)$/ );
		if(gmatch) theDivHTML = gmatch[1];
		theTarget.title = 'Alexa Traffic Rank: '+theDivHTML;
	  }
	});

   event.target.removeEventListener('mouseover', handleMouseOver, false);
}