Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I allow Cross-Origin Requests from greasemonkey scripts in Firefox?

I'm developing a Greasemonkey script that implements a couple of tools onto a webpage. This script makes a request for data from

http://localhost/chess/heartbeat.php 

Now currently in Firefox I am getting this console error which totally stops my jQuery AJAX request for data.

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at

http://localhost/chess/heartbeat.php.

This can be fixed by moving the resource to the same domain or enabling CORS.

I am able to work around this using Google Chrome. When I have it as a simple browser extension for chrome, I'm able to have it do the same thing as Greasemonkey and I can add the following permissions to the manifest file for the plugin which allows me to make the same data request which Firefox blocked:

"permissions": [
    "<all_urls>"
  ]

Anyway, this works on chrome, but I want to achieve the same effect on Firefox. I've been researching this issue and I can't find a simple answer.

like image 759
user3810422 Avatar asked Jul 11 '14 00:07

user3810422


People also ask

How do I enable cross origin requests in Firefox?

Allow CORS: Access-Control-Allow-Origin lets you easily perform cross-domain Ajax requests in web applications. Simply activate the add-on and perform the request. CORS or Cross Origin Resource Sharing is blocked in modern browsers by default (in JavaScript APIs).

How do I use Greasemonkey scripts in Firefox?

Click on the Firefox drop-down menu at the top left of the browser and select Add-ons. Type Greasemonkey into the add-ons search box at the top right of the browser. Find Greasemonkey in the list and click on Install.

Does Greasemonkey work on Firefox?

Greasemonkey is a userscript manager made available as a Mozilla Firefox extension.

How do you allow cross origin?

In Java servlets. Simply add a header to your HttpServletResponse by calling addHeader : response. addHeader("Access-Control-Allow-Origin", "*");


1 Answers

Normally XMLHttpRquest, and that includes jQuery's higher-level API around it, does not allow unrestricted cross-site requests but is limited by the same-origin policy and CORS.

As @epascarello already pointed out, you may use GM.xmlHttpRequest which allows you to perform any cross-site XHR even when the server does not implement CORS or allows the origin site. It also comes with some other goodies.

You should add a @grant GM.xmlHttpRequest metadata block to your user script or your script may break in the future.

Since you mentioned Chrome extensions: Firefox extensions can perform cross-site XHR as well. e.g. most user scripts should be easily portable to an SDK add-on using PageMod and enabling certain permissions analog to what you'd do in a Chrome extension.

like image 130
nmaier Avatar answered Oct 23 '22 17:10

nmaier