0

I'm working on a chrome extension for XHR intercepting.

In order to reach the body of the response I injected some codes to intercept the XmlHttpRequest class.

But it kept missing the first batch of requests as the injected script ran in an unpredictable order (usually the last).

So I wonder if there is a way to make sure the dynamically-injected-script runs earlier than the original scripts of the web page.

The following is the interception module of my content script.

The content script is set to be run at "document_start"

const doIntercept = () => {
 const xhrOverrideScript = document.createElement('script');
 xhrOverrideScript.type = 'text/javascript';
 xhrOverrideScript.innerHTML = INTERCEPT_SCRIPT_SHALL_BE_REPLACED_BY_WEBPACK;
 document.body.prepend(xhrOverrideScript);
}
const tryIntercept = () => {
 if (document.body && document.head) {
 doIntercept();
 } else {
 requestIdleCallback(tryIntercept);
 }
};
const interceptXHR = (onFinish) => {
 requestIdleCallback(tryIntercept);
 onFinish();
}
export default interceptXHR;
asked Aug 25, 2019 at 7:40
0

1 Answer 1

1

Remove requestIdleCallback and simply append the script to document.documentElement directly without waiting for a head or a body to appear.

Here's the entire code:

const interceptXHR = () => {
 const el = document.createElement('script');
 el.textContent = INTERCEPT_SCRIPT_SHALL_BE_REPLACED_BY_WEBPACK;
 document.documentElement.appendChild(el);
 el.remove();
};

It'll run synchronously so there's no need for a callback either.
There are more methods but some of them are asynchronous.

There's a relatively popular mistaken idea that one must append script elements to a head or body, not to the root element, but they simply confuse HTML specs (see "content model") with DOM specs that don't impose such a limit.

answered Aug 25, 2019 at 8:35
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.