最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can userscript make a network request to a different domain? - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited May 4, 2024 at 4:58 woxxom 73.6k14 gold badges155 silver badges160 bronze badges asked Mar 4, 2017 at 4:01 JasonJason 1,0999 silver badges17 bronze badges 5
  • 1 is the code you posted in greasemonkey script? if so, you need to use greasemonkeys XHR - wiki.greasespot.net/GM_xmlhttpRequest – Jaromanda X Commented Mar 4, 2017 at 4:07
  • 1 side note: you should probably look at porting your code to a web extension - because I don't think greasemonkey will survive Firefox 57 - the developers are not really keen on porting greasemonkey to a web extension - they had heaps of issues with recent e10s compatibility, and I think after 10 years, they've had enough :p – Jaromanda X Commented Mar 4, 2017 at 4:42
  • 1 @JaromandaX, even if the current lead dev drops the ball (more/again), others will no doubt pick it up. Also Tampermonkey on Firefox is an increasingly viable option. – Brock Adams Commented Mar 4, 2017 at 5:13
  • of course! Tampermonkey - thanks for the heads up @BrockAdams – Jaromanda X Commented Mar 4, 2017 at 5:28
  • Thanks guys, this was a help. Actually since I control the domain I'm pulling data from I was able to fix things by editing the data I'm sending with a different CORS header on my page. Someone mentioned that but edited it out. Still, it's very handy to know about the GM extensions for cases where I'm not controlling the data. spring.io/understanding/CORS – Jason Commented Mar 4, 2017 at 7:01
Add a comment  | 

1 Answer 1

Reset to default 20

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.

发布评论

评论列表(0)

  1. 暂无评论