jQuery AJAX(谷歌 PageRank)

发布于 2024-09-04 10:44:47 字数 4286 浏览 4 评论 0原文

我需要一点帮助..

我一直在开发一个 Jqery 插件来使用 XHR 获取网站上 url 的页面排名,

问题是当从 google 服务器请求排名时,页面不返回任何内容,但如果我使用检查器并获取请求的 url,然后通过我的浏览器访问它,显示页面排名。所以它一定是带有标题的东西,但这让我感到困惑。

这是一些源代码,但我删除了一些不需要审查的方面。

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 (示例)

<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>

我只是无法理解为什么要这样做。

--

注意:

在 jquery 之外使用 XHR 效果很好!

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()
}

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()
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北斗星光 2024-09-11 10:44:47

您无法使用 AJAX 从不同的服务器(与托管页面的服务器不同)获取任何内容。作为一种安全措施,浏览器明确禁止这样做。

您能做的最好的事情就是向托管在您自己的服务器上的服务器端脚本发出 AJAX 请求,并让该脚本与 Google 进行通信。

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.

笑,眼淚并存 2024-09-11 10:44:47

为了安全起见,没有浏览器允许ajax连接另一个域,否则,会出现很多google,weber可以托管一个“html”页面来服务吗?

但 json 可以做到,它在运行时在页面上插入脚本,并设置脚本 src 属性。

谷歌页面排名 API 只返回类似“1:1:5”的文本,脚本编写者无法解释该文本,因此一切都消失了!

所以,ajax做不到。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文