【发布时间】:2012-01-10 09:20:29
【问题描述】:
Chrome 有自己的 Greasemonkey,反正它有很多限制。其中之一是它的 xmlhttprequest 不支持跨域。那么有什么办法让它工作呢?
谢谢
【问题讨论】:
标签: google-chrome xmlhttprequest cross-domain greasemonkey userscripts
Chrome 有自己的 Greasemonkey,反正它有很多限制。其中之一是它的 xmlhttprequest 不支持跨域。那么有什么办法让它工作呢?
谢谢
【问题讨论】:
标签: google-chrome xmlhttprequest cross-domain greasemonkey userscripts
如果您还希望脚本对 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 参数的信息。
【讨论】: