来自不同域的Greasemonkey AJAX请求? [英] Greasemonkey AJAX request from a different domain?

查看:128
本文介绍了来自不同域的Greasemonkey AJAX请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JavaScript(使用Greasemonkey)从我自己的网站提取数据来自定义另一个网站。我使用的代码如下:

  function getURL(url,func)
{
var xhr = new XMLHttpRequest();
xhr.open(GET,url,true);
xhr.onload = function(e)
{
if(xhr.readyState == 4)
{
if(xhr.status == 200)
{
func(xhr.responseText,url);
}
其他
{
alert(xhr.statusText,0);
}
}
};
xhr.onerror = function(e)
{
alert(getURL Error:+ xhr.statusText); //在这里收取错误
};
xhr.send(null);
}

以上工作完全正常,它从URL获取文本并将其返回我传递给函数的匿名函数,只要该文件与我调用它的页面位于同一个域中。但是,如果域名不同,则会触发 onerror



如何将其排序以便我可以从此设置中的其他域中提取数据?

解决方案

Greasemonkey(和Tampermonkey)内置支持交叉域AJAX。使用 GM_xmlhttpRequest函数



这是一个完整的说明该过程的用户脚本:

  // == UserScript == 
// @name _Starter GM中的AJAX请求,TM等。
// @match *://YOUR_SERVER.COM/YOUR_PATH/*
// @grant GM_xmlhttpRequest
// @connect targetdomain1.com
// == / UserScript ==

GM_xmlhttpRequest({
method:'GET',
url:'http://targetdomain1.com/some_page.htm',
onload:function(responseDetails){
//在这里做所有响应处理...
console.log(
GM_xmlhttpRequest()响应是:\ n,
responseDetails.responseText.substring(0,80)+'...'
);
}
});

您还应养成使用 @connect 指令 - 即使Firefox上的Greasemonkey并非严格要求,但<。 / p>

I'm trying to get JavaScript (with Greasemonkey) to pull data from my own site to customize another site. The code I'm using is as follows:

function getURL(url, func)
{
  var xhr = new XMLHttpRequest();
  xhr.open("GET", url, true);
  xhr.onload = function (e) 
  {
    if (xhr.readyState == 4) 
    {
      if (xhr.status == 200) 
      {
        func(xhr.responseText, url);
      } 
      else
      {
        alert(xhr.statusText, 0);
      }
    }
  };
  xhr.onerror = function (e)
  {
    alert("getURL Error: "+ xhr.statusText); // picks up error here
  };
  xhr.send(null);  
}

The above works perfectly fine, it gets the text from the URL and returns it to the anonymous function that I pass into the function, as long as the file is on the same domain as the page I'm calling it from. However, if the domain is different then the onerror gets triggered.

How can I sort it out so I can pull in data from a different domain in this set up?

解决方案

Greasemonkey (and Tampermonkey) has built-in support for cross-domain AJAX. Use the GM_xmlhttpRequest function.

Here's a complete userscript that illustrates the process:

// ==UserScript==
// @name        _Starter AJAX request in GM, TM, etc.
// @match       *://YOUR_SERVER.COM/YOUR_PATH/*
// @grant       GM_xmlhttpRequest
// @connect     targetdomain1.com
// ==/UserScript==

GM_xmlhttpRequest ( {
    method:     'GET',
    url:        'http://targetdomain1.com/some_page.htm',
    onload:     function (responseDetails) {
                    // DO ALL RESPONSE PROCESSING HERE...
                    console.log (
                        "GM_xmlhttpRequest() response is:\n",
                        responseDetails.responseText.substring (0, 80) + '...'
                    );
                }
} );

You should also get in the habit of using the @connect directive -- even though it's not strictly required for Greasemonkey on Firefox, yet.

这篇关于来自不同域的Greasemonkey AJAX请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆