【问题标题】:Userscript in Chrome, Cross-Origin communicationChrome中的用户脚本,跨域通信
【发布时间】:2012-01-10 09:20:29
【问题描述】:

Chrome 有自己的 Greasemonkey,反正它有很多限制。其中之一是它的 xmlhttprequest 不支持跨域。那么有什么办法让它工作呢?

谢谢

【问题讨论】:

    标签: google-chrome xmlhttprequest cross-domain greasemonkey userscripts


    【解决方案1】:

    如果您还希望脚本对 Opera 友好,您仍然无法使用 GM_xmlhttpRequest。但是,使用 YQL 和 jQuery,您可以这样做:

    var getCrossDomain = function (url, callback, maxage) {
            if (typeof (url) !== 'undefined') {
                var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + url + '"') + '&diagnostics=false&format=xml&callback=?&_maxage=';
                if (typeof (maxage) === 'undefined') {
                    yql += '10000'; // Value is in ms
                }
                $.getJSON(yql, function (data) {
                    if (typeof (callbackX) === 'function') {
                        callback(data.results[0]);
                    }
                }).fail(function (jqXHR, textStatus, errorThrown) {
                    // Some code here to handle a failed request
                });
            }
        };
    

    阅读http://www.yqlblog.net/blog/2010/03/12/avoiding-rate-limits-and-getting-banned-in-yql-and-pipes-caching-is-your-friend/ 了解有关 maxage 参数的信息。

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 2010-12-28
      • 2011-12-27
      • 1970-01-01
      • 2018-12-02
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 1970-01-01
      相关资源
      最近更新 更多