0

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.

--

Notes:

using and XHR Outside of jquery works just fine!

function getPageRank(a, b) {
    a = "http://www.google.com/search?client=navclient-auto&ch=8" + awesomeHash(a) + "&features=Rank&q=info:" + a;
    var c = new XMLHttpRequest;
    c.open("GET", a, true);
    c.onreadystatechange = function () {
        if (c.readyState == 4) {
            console.log("reponse text is " + c.responseText);
            var d = c.responseText.substr(9, 2).replace(/\s$/, "");
            if (d == "" || isNaN(d * 1)) d = "0";
            b(d)
        }
    };
    c.send()
}
RobertPitt
  • 56,863
  • 21
  • 114
  • 161
  • Have you tried JSONP? This question has an answer with good references: http://stackoverflow.com/questions/333532/cross-site-ajax-requests – Bernhard Hofmann Jul 05 '10 at 12:11
  • i cant use jsonp as this is a standalone jQuery plugin, and should not be called back to any of our servers but googles own! what so ever. – RobertPitt Jul 05 '10 at 13:06

2 Answers2

1

You cannot fetch any content from a different server (than the one where the page is hosted) using AJAX. Browsers prohibit this explicitly as a security measure.

The best you can do is make an AJAX request to a server-side script hosted on your own server and let that script communicate with Google.

VoteyDisciple
  • 37,319
  • 5
  • 97
  • 97
  • This is incorrect, i have a standalone version that creates a XMLHttpRequest and does it in pretty much the same manner and it works perfectly fine! – RobertPitt Jun 07 '10 at 01:41
  • @Robert: When you say "standalone", do you mean you're loading a file, or a page from a local web server? – Bernhard Hofmann Jul 05 '10 at 12:10
  • I mean standalone as a javascript file, but i forgot to mention that that code runs in Google Chrome Plugins and has privs from the chrome browser itself :( you was right.. – RobertPitt Jul 05 '10 at 13:08
-1

for safety,no browser allow ajax conneting annother domain, otherwise, Lot of google will appear,weber can host a "html"page to service ?

but json can do,it runtimely insert a script on a page, and the script src property is set.

google page rank api only return a text like "1:1:5" ,which can not be explained by scripter,thus all gone!

So,ajax can`t do it.

99soon
  • 1