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

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

programmeradmin7浏览0评论

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.

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.

Share Improve this question asked Jul 11, 2014 at 0:15 user3810422user3810422 2972 gold badges3 silver badges5 bronze badges 1
  • wiki.greasespot.net/GM_xmlhttpRequest – epascarello Commented Jul 11, 2014 at 0:24
Add a comment  | 

1 Answer 1

Reset to default 17

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.

发布评论

评论列表(0)

  1. 暂无评论