Search code examples
javascripttampermonkeyuserscripts

How does "instant inject" in tampermonkey work?


In tampermonkey, advanced settings, you can find a setting called "Inject mode" at the "Expirimental" tab. Here, you can select a mode called "Instant".

I was wondering, what does it do different? How does it work? Is it simular to ViolentMonkeys injection methods?


Solution

  • Judging by the source code in other extensions like Stylus or Violentmonkey (beta) that recently added the same feature:

    1. The background script makes a Blob in the background script with the data, gets its URL via URL.createObjectURL, puts it into a Set-Cookie header via chrome.webRequest API.
    2. The content script reads the URL from document.cookie and uses it in a synchronous XMLHttpRequest to get the original data synchronously.

    This trick is based on an answer by Xan.