-
Notifications
You must be signed in to change notification settings - Fork 153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Early JavaScript code cannot be intercepted even with "@run-at document-start", "Instant" injection mode needed #459
Labels
help wanted
Extra attention is needed
Comments
biergaizi
changed the title
Early JavaScript code cannot be intercepted even with "@run-at document-start"
Early JavaScript code cannot be intercepted even with "@run-at document-start", Need "Instant" Injection Mode.
Mar 30, 2023
biergaizi
changed the title
Early JavaScript code cannot be intercepted even with "@run-at document-start", Need "Instant" Injection Mode.
Early JavaScript code cannot be intercepted even with "@run-at document-start", "Instant" Injection Mode Needed
Mar 30, 2023
biergaizi
changed the title
Early JavaScript code cannot be intercepted even with "@run-at document-start", "Instant" Injection Mode Needed
Early JavaScript code cannot be intercepted even with "@run-at document-start", "Instant" injection mode needed
Mar 30, 2023
Thanks for the feedback. Apple strictly restricts the API related to cookies, and as far as we know, it is impossible to achieve the same trick. Duplicate issue #184 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm trying to write a Userscript to change the behavior of websites by monkey-patching the built-in JavaScript functions, for example, replacing them with my custom versions to intercept their actions. This requires one to execute the Userscript before everything else. Unfortunately, I found even with
@run-at document-start
, early JavaScript function calls cannot be intercepted, since the Userscript is still running too late.Demo
To replicate this problem, I selected the JavaScript
alert()
function as a demo. First, save the following webpage astest.html
:Then save the following Userscript as
intercept-alert.user.js
, which monkey-patches the JavaScript "alert()` function with a custom version:Open the webpage, you would see that the Userscript cannot intercept the
alert("Early call!");
, only thealert("Late call!");
can be intercepted.Discussion
In fact, one can also replicate this problem with TamperMonkey and ViolentMonkey on Chromium. To overcome this problem, both plugins implemented a workaround. TamperMonkey calls it "inject mode: instant", and ViolentMonkey calls it "Synchronous
page
mode`.ViolentMonkey's description is:
The workaround basically abuses
document.cookie
and a synchronousXMLHttpRequest
call in a tricky way in order to force the browser to run the scripts in a synchronous manner, ensuring that the Userscript runs before everything else.The source code of ViolentMonkey's implementation is available here: violentmonkey/violentmonkey#1100
Is it possible to implement the same feature in quoid/userscripts? Or is there an alternative method to monkey-patch and intercept JavaScript function without this limitation?
The text was updated successfully, but these errors were encountered: