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

  • SEARCH
  • Home
  • 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 8023897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:42:05+00:00 2026-06-04T22:42:05+00:00

I was facing an issue while developing this small userscript . When I wanted

  • 0

I was facing an issue while developing this small userscript. When I wanted to block every XMLHttpRequest from the running website with my script, nothing was happening (at least with Chrome):

function main() {
  // Override XHR.open with a custom function
  window.XMLHttpRequest.prototype.open = function() {
    // Nothing... so it's supposed to block every xhr.open() call
  }
}
main();

Same thing when replacing window by unsafeWindow.

However, when I used this little trick, everything worked like a charm:

// No more call to main(), and:
var script = document.createElement("script");
script.textContent = "(" + main.toString() + ")();";
document.body.appendChild(script);

Every call to xhr.open is replaced by my custom function, no more AJAX.

So I guess the window element is not the same when main is called from inside the script than when it’s called from a <script></script> container. Can someone explain me why ?

  • 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-06-04T22:42:06+00:00Added an answer on June 4, 2026 at 10:42 pm

    See “Are Chrome user-scripts separated from the global namespace like Greasemonkey scripts?”. Both Chrome userscripts/content-scripts and Greasemonkey scripts are isolated from the page’s javascript. This is done to help keep you from being hacked, but it also reduces conflicts and unexpected side-effects.

    However, the methods are different for each browser…

    Firefox:

    1. Runs scripts in an XPCNativeWrapper sandbox, unless @grant none is in effect (as of GM 1.0).
    2. Wraps the script in an anonymous function by default.
    3. Provides unsafeWindow to access the target page’s javascript. But beware that it is possible for hostile webmasters to follow unsafeWindow usage back to the script’s context and thus gain elevated privileges to pwn you with.

    Chrome:

    1. Runs scripts in an “isolated world”.
    2. Wraps the script in an anonymous function.
    3. Strictly blocks any access to the page’s JS by the script and vice-versa.
      Recent versions of Chrome now provide an object named unsafeWindow, for very-limited compatibility, but this object does not provide any access to the target page’s JS. It is the same as window in the script scope (which is not window in the page scope).

    That said, the version of your script that used unsafeWindow should work on/in Firefox if implemented correctly. It might work using the Tampermonkey extension on Chrome, but I’m not going to double-check that right now.

    When you do that “trick” (var script = document.createElement("script"); ...), you are injecting code into the target page. This bypasses the sandbox and is the only way on a normal Chrome userscript for a script to interact with the page’s JS.

    Injection advantages:

    1. The only way for non-Tampermonkey userscripts to access objects or functions provided by the target page.
    2. Almost always fully compatible between Chrome, Firefox, Opera, etc. (IE is, as always, something else.)
    3. Often easier to debug the whole script; developer tools work normally.

    Injection drawbacks:

    1. The script, at least the injected parts, cannot use the enhanced privileges (especially cross-domain) provided by the GM_ functions — especially GM_xmlhttpRequest().
      Note that currently Chrome only supports GM_addStyle, GM_xmlhttpRequest, GM_log and GM_openInTab, fully, natively.
      Tampermonkey supports GM_ functions almost fully, however.

    2. Can cause side effects or conflicts with the page’s JS.

    3. Using external libraries introduces even more conflicts and timing issues. It’s nowhere near as easy as @require.
      @require, also runs the external JS from a local copy — speeding execution and all but eliminating reliance on an external server.

    4. The page can see, use, change, or block the script.

    5. Requires JS to be enabled. Firefox Greasemonkey, especially, can run on a page which has JS blocked. This can be godsend on bloated, crappy, and/or intrusive pages.

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

Sidebar

Related Questions

More precisely, I am facing this issue while trying to install https://github.com/barraponto/thacker-profile (commit 75c693581796eddae5fc39870dfcc2647e00c81b
I am facing some issue while developing Video capturing application. 1) When I start
facing the issue while debugging String.hashCode() line: not available i have refereed this ,
I am facing small issue while firing jquery event to replace element p or
I am facing the following issue while uploading the video from my android application.
I am facing a weird issue while running my project on the device. I
I am facing issue while trying into insert special symbols like pound character (£
I am facing issue while i load a form on ajax request. i followed
I am facing an issue while loading a UIWebView .I have a webview(1st) where
I am facing an issue while streaming PDF file on a Java EE web

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.