views:

179

answers:

1

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&amp;ch=8'+hash+'&amp;features=Rank&amp;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"&gt;&lt;/script&gt;
        <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"&gt;a&lt;/a&gt;
<a href="http://twitter.com"&gt;a&lt;/a&gt;
        <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&amp;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()
}
A: 

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
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
@Robert: When you say "standalone", do you mean you're loading a file, or a page from a local web server?
Bernhard Hofmann
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