jQuery AJAX (google PageRank)
Posted
by RobertPitt
on Stack Overflow
See other posts from Stack Overflow
or by RobertPitt
Published on 2010-06-07T01:34:21Z
Indexed on
2010/06/07
1:42 UTC
Read the original article
Hit count: 426
Hey guys,
I need a little help..
iv'e been developing a Jqery plug-in to get the page ranks of urls on a website using XHR,
The problem is when requesting the rank from google servers the page is returned no content, but if i use an inspector and get the url that was requests and go to it via my browser the pageranks are shown. so it must be something with headers but its just got me puzzled.
Heres some source code but i have removed several aspects that are not needed to review.
pagerank.plugin.js
(
$.fn.PageRank = function(callback)
{
var _library = new Object();
//Creat the system library
_library.parseUrl = function(a)
{
var b = {};
var a = a || '';
/*
* parse the url to extract its parts
*/
if (a = a.match(/((s?ftp|https?):\/\/){1}([^\/:]+)?(:([0-9]+))?([^\?#]+)?(\?([^#]+))?(#(.+))?/)) {
b.scheme = a[2] ? a[2] : "http";
b.host = a[3] ? a[3] : null;
b.port = a[5] ? a[5] : null;
b.path = a[6] ? a[6] : null;
b.args = a[8] ? a[8] : null;
b.anchor = a[10] ? a[10] : null
}
return b
}
_library.ValidUrl = function(url)
{
var b = true;
return b = url.host === undefined ? false : url.scheme != "http" && url.scheme != "https" ? false : url.host == "localhost" ? false : true
}
_library.toHex = function(a){
return (a < 16 ? "0" : "") + a.toString(16)
}
_library.hexEncodeU32 = function(a) {
}
_library.generateHash = function(a)
{
for (var b = 16909125, c = 0; c < a.length; c++)
{
}
return _library.hexEncodeU32(b)
}
var CheckPageRank = function(domain,_call)
{
var hash = _library.generateHash(domain);
$.ajax(
{
url: 'http://www.google.com/search?client=navclient-auto&ch=8'+hash+'&features=Rank&q=info:' + escape(domain),
async: true,
dataType: 'html',
ifModified:true,
contentType:'',
type:'GET',
beforeSend:function(xhr)
{
xhr.setRequestHeader('Referer','http://google.com/'); //Set Referer
},
success: function(content,textS,xhr){
var d = xhr.responseText.substr(9, 2).replace(/\s$/, "");
if (d == "" || isNaN(d * 1)) d = "0";
_call(d);
}
});
}
//Return the callback
$(this).each(function(){
urlsegments = _library.parseUrl($(this).attr('href'))
if(_library.ValidUrl(urlsegments))
{
CheckPageRank(urlsegments.host,function(rank){
alert(rank)
callback(rank);
});
}
});
return this; //Dont break any chain.
}
)(jQuery);
Index.html (example)
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript" src="pagerank.plugin.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('a').PageRank(function(pr){
alert(pr);
})
});
</script>
</head>
<body>
<a href="http://facebook.com">a</a>
<a href="http://twitter.com">a</a>
<div></div>
</body>
</html>
i just cant understand why its doing this.
© Stack Overflow or respective owner