11

I was working on a userscript today, which had a goal of displaying chat pings immediately at the main site. This would allow me to respond more quicker-like to chat pings.

However, I was met by this fearsome warrior:

enter image description here

Can we please set some decent header things? I want to be able to do this really bad.

1
  • 2
    Why don't you want to do it really well?
    – TRiG
    Commented Dec 20, 2013 at 2:43

2 Answers 2

8

Certainly not. We're not going to allow CORS for pages that require (or at least change with) authentication. Even with origin restrictions, this would need a very good reason, and "I want to be able to do this really bad" isn't one (and calling this a "bug" is pretty far out there).

Here's a tip though: User scripts can run on more than one page and do different things depending on what site they're running on, and then there's postMessage.

2

A real-world use case for making such requests is fetching post titles to help chat users use Markdown-formatted links in messages (and avoiding one-boxing — which is considered bad etiquette by many rooms at least on Stack Overflow — to get meaningful titles) without resorting to using the API for each and every post.

It does not have to be the overly permissive (and opening a whole can of worms) "allow-all header":

Access-Control-Allow-Origin: *

It would be of great help to userscript authors that need their scripts to make cross-origin requests at least between sites of the Stack Exchange network. For example, currently, it is impossible to make a request to *.stackapps.com from *.stackoverflow.com because the access control header is not set on responses.

I realize that since Access-Control-Allow-Origin (see spec) can only allow one origin (unless the value is the wildcard), it will require the backend to maintain a list of allowed Origin (see MDN) request headers and echo it if the latter is in the list, so the request is just to reconsider the decision made more than 7 years ago.


Userscripts have the userscript APIs, specifically GM_xmlhttpRequest and GM.xmlHttpRequest, which allow making cross-origin requests – Makyen 2 hours ago

While this is indeed possible, usage of GM_* functions makes userscripts dependent on being run via the manager (whereas with either fetch or XMLHttpRequest the script does not depend on one), as well as incurs maintenance cost (for example, not as popular as it used to be but still used Greasemonkey has an asynchronous API and exposes the functions as methods on a global GM object instead).

8
  • Userscripts have the userscript APIs, specifically GM_xmlhttpRequest and GM.xmlHttpRequest, which allow making cross-origin requests. While it might be easier if using those wasn't required. It's not something which you are completely prevented from accomplishing. There are also other possible ways which you could make such requests with a userscript.
    – Makyen
    Commented Jul 27, 2021 at 11:41
  • 1
    @Makyen yeah, I am not saying there are no ways to make cross-origin requests, just that it would be helpful if the scripts could do so directly, especially if one does not want to add @grant headers needed to use GM_* functions.
    – 0Valt
    Commented Jul 27, 2021 at 11:43
  • I certainly understand there are compatibility issues with Greasemonkey 4. However, the asynchronous vs. synchronous interfaces difference isn't really an issue for GM_xmlhttpRequest and GM.xmlHttpRequest, because those are asynchronous calls in all userscript managers due to the (primarily) inherent asynchronous nature of network requests. In other words, there's very little difference for using the basic functionality of that API across different userscript managers. Requesting both of those APIs and choosing to use whichever one is actually available is a couple lines of code.
    – Makyen
    Commented Jul 27, 2021 at 15:50
  • There is, however, a difference between the environment which your userscript code runs in when it uses @grant none vs. the environment when you ask for one or more of the userscript APIs. That can substantially change/increase how one has to interact with the page scripts. So, yes, it would be more convenient to be able to make native cross-origin requests from within other SE sites, but it's not something that needs to happen.
    – Makyen
    Commented Jul 27, 2021 at 15:50
  • 1
    @Makyen well, yeah, all I am saying is that using GM_* and GM.* functions ties the script to using a userscript manager and makes the author either account for the differences in exposed APIs or choose to explicitly not support one of the managers. By stressing the async vs sync difference I just pointed out that there are differences in exposed APIs, and the burden of reconciling them is placed on the userscript developer even if all they wanted is to make cross-origin requests "inside" the SE network.
    – 0Valt
    Commented Jul 27, 2021 at 16:05
  • 1
    Certainly far from a necessity but it could at least be reconsidered given that the initial Undo's request was dismissed a bit too out of hand, in my opinion as there are use cases for making cross-origin requests from one SE network site to another for userscript authors.
    – 0Valt
    Commented Jul 27, 2021 at 16:11
  • 1
    I'm not saying that it can't be reconsidered, just that it's not necessary in order to accomplish what you desire. Even using GM_xmlhttpRequest and/or GM.xmlHttpRequest isn't necessary in order to accomplish what you want. As was implied in the original answer here, it's also quite possible to create an iframe and pass messages back and forth from the instance of your userscript running in the current page with an instance of your script running in the domain within which you wish to make requests/get data (e.g. just pick some URL to a 404 page for your script's use and run on that page).
    – Makyen
    Commented Jul 27, 2021 at 16:33
  • 1
    Yeah, well, it's more of an attempt to give this old request some new life - I just keep stressing out that it's not my intention to say that we don't have any other ways of making cross-origin requests within the network, just that it would be nice to have a direct way to do so without resorting to either userscript manager-specific functions or workarounds which an iframe is, methinks, is.
    – 0Valt
    Commented Jul 27, 2021 at 17:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .