Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 231653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:57:04+00:00 2026-05-11T19:57:04+00:00

I have a Greasemonkey userscript which runs most of its code in an unprivileged

  • 0

I have a Greasemonkey userscript which runs most of its code in an unprivileged context by inserting a <script> tag like so:

function code {
  ...
}

var script = document.createElement("script");
script.type = "application/javascript";
script.innerHTML = "(" + code + ")();";
document.body.appendChild(script);

This avoids the need to do dangerous things with unsafeWindow.

However, I my code needs to get information from an API on another domain which doesn’t support JSONP. GM_xmlhttpRequest can access other domains, but is only available in the privileged Greasemonkey context.

I’d like to write a function which provides a limited interface and makes exactly the API call I need using GM_xmlhttpRequest, and then expose that (theoretically safe) function to the normal page context. My first attempt at that was something like:

unsafeWindow.foo = function() {
  console.log("Foo!");
  console.log(GM_xmlhttpRequest.toString());
  GM_xmlhttpRequest({
    method: "GET",
    url: "http://www.example.net/",
    headers: {
      "User-Agent": navigator.userAgent,
      "Accept": "text/html"
    },
    onload: function(response) {
      console.log(response);
      unsafeWindow.console.log(response);
      alert(response);
      unsafeWindow.alert(response);
    }
  });
  console.log("Bar!");
};

Interestingly, when I call foo() from the page context, I see “Foo!”, the stringified GM_xmlhttpRequest and “Bar!” in the console log. However, I never get the response in the console or in an alert. If I make the GM_xmlhttpRequest on its own in the GM context, I get both alerts and log messages.

The question is: is what I’m trying to do even possible? Or is there another way to accomplish the same thing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:57:04+00:00Added an answer on May 11, 2026 at 7:57 pm

    Aha! The answer has been posted to a similar question.

    Greasemonkey does this very intentionally, and provides a simple workaround on the 0.7.20080121.0 compatibility page:

    unsafeWindow.foo = function() {
      window.setTimeout(GM_xmlhttpRequest, 0, {
        // ...
      });
    };
    

    That works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a greasemonkey user script with this single line of code... window.close(); but
I have a Greasemonkey script which operates on a search results page at a
I have created a Greasemonkey script that runs fine in the firebug editor, with
I have a very simple greasemonkey script that I want to call an already
I have a UserScript which should remove all images of a certain class on
I have the following (fairly) simple JavaScript snippet that I have wired into Greasemonkey.
By means of a regular expression and Greasemonkey I have an array of results
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I'm writing some javascript (a greasemonkey/userscript) that will insert some input fields into a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.