Disable Javascript in Headless Mode

969 views
Skip to first unread message

Jeremy West

unread,
Nov 29, 2017, 11:12:29 AM11/29/17
to headle...@chromium.org
I'm trying to find the easiest way to disable javascript when running in headless mode.

I tried running in non-headless mode while setting --user-data-dir, disabling javascript through the normal content settings, and then continuing to use the same user-data-dir when running in headless (hoping that setting would be used regardless of whether I was running in headless or not). The setting did not seem to be respected in headless mode (though it did appear to keep working when I wasn't in headless).

I've also had decent success in using Network.setRequestInterception for documents, manually requesting said documents on the server side, parsing the document with an HTML parser, and then programmatically stripping all script nodes, and then manually continuing the request with the new response. This mostly works, but is complicated, and isn't 100% foolproof (things like onload properties still slip by, etc).

I have not noticed anything else in the protocol docs relating to this. Any guidance would be most welcome.

-Jeremy

Alex Clarke

unread,
Nov 29, 2017, 11:32:23 AM11/29/17
to Jeremy West, headless-dev
On 29 November 2017 at 16:12, Jeremy West <pixel...@gmail.com> wrote:
I'm trying to find the easiest way to disable javascript when running in headless mode.
 
There isn't currently a good way to do this unless you're a C++ embedder, in which case you can use HeadlessBrowserContext::Builder::SetOverrideWebPreferencesCallback to register a callback and set WebPreferences::javascript_enabled to false.

I'd suggest filing a bug (crbug.com) and if it gets a few stats we can think about adding a command line flag or something. Make sure to post it here so we can make sire it has the right labels.

I tried running in non-headless mode while setting --user-data-dir, disabling javascript through the normal content settings, and then continuing to use the same user-data-dir when running in headless (hoping that setting would be used regardless of whether I was running in headless or not). The setting did not seem to be respected in headless mode (though it did appear to keep working when I wasn't in headless).
Headless is a bit special, it's basically it's own content/ embedder and a lot of chrome/ layer features don't work.

 

I've also had decent success in using Network.setRequestInterception for documents, manually requesting said documents on the server side, parsing the document with an HTML parser, and then programmatically stripping all script nodes, and then manually continuing the request with the new response. This mostly works, but is complicated, and isn't 100% foolproof (things like onload properties still slip by, etc).

Wow that sounds pretty complicated :)  I'm afraid I don't have any advice there
 

I have not noticed anything else in the protocol docs relating to this. Any guidance would be most welcome.


-Jeremy

--
You received this message because you are subscribed to the Google Groups "headless-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to headless-dev+unsubscribe@chromium.org.
To post to this group, send email to headle...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/headless-dev/CAD5JyWUVpgJ-yqQrk-EBiOZdbLZJ5Wd-4eGM2GbFGua-4XE%2BGw%40mail.gmail.com.

Paul Irish

unread,
Nov 29, 2017, 12:41:41 PM11/29/17
to Jeremy West, headless-dev
Emulation.setScriptExecutionDisabled should do the trick for you.

--

Jeremy West

unread,
Nov 29, 2017, 12:44:15 PM11/29/17
to Paul Irish, headless-dev
Hah, of course I'd miss that! Thank you for the quick responses and pointing me in the right direction. This just got much easier!

Alex Clarke

unread,
Nov 29, 2017, 1:53:29 PM11/29/17
to Paul Irish, Jeremy West, headless-dev
Reply all
Reply to author
Forward
0 new messages