From a44b18236cc76f3f258926a4e16692109bb77dee Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Mon, 9 Dec 2024 21:53:31 +0000 Subject: [PATCH] LibWeb: Add an `Internals.headless` attribute This returns true if the browser is running in headless mode. --- Libraries/LibWeb/Internals/Internals.cpp | 5 +++++ Libraries/LibWeb/Internals/Internals.h | 2 ++ Libraries/LibWeb/Internals/Internals.idl | 2 ++ Tests/LibWeb/Text/expected/Internals/headless.txt | 1 + Tests/LibWeb/Text/input/Internals/headless.html | 7 +++++++ 5 files changed, 17 insertions(+) create mode 100644 Tests/LibWeb/Text/expected/Internals/headless.txt create mode 100644 Tests/LibWeb/Text/input/Internals/headless.html diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cpp index fb2914923c5..837740cdde0 100644 --- a/Libraries/LibWeb/Internals/Internals.cpp +++ b/Libraries/LibWeb/Internals/Internals.cpp @@ -239,4 +239,9 @@ void Internals::set_echo_server_port(u16 const port) s_echo_server_port = port; } +bool Internals::headless() +{ + return internals_page().client().is_headless(); +} + } diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.h index 56dc5e2a015..ba870af6b32 100644 --- a/Libraries/LibWeb/Internals/Internals.h +++ b/Libraries/LibWeb/Internals/Internals.h @@ -55,6 +55,8 @@ public: static u16 get_echo_server_port(); static void set_echo_server_port(u16 port); + bool headless(); + private: explicit Internals(JS::Realm&); virtual void initialize(JS::Realm&) override; diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idl index 64a2994a655..04776e3bdad 100644 --- a/Libraries/LibWeb/Internals/Internals.idl +++ b/Libraries/LibWeb/Internals/Internals.idl @@ -42,4 +42,6 @@ interface Internals { DOMString getComputedRole(Element element); DOMString getComputedLabel(Element element); unsigned short getEchoServerPort(); + + readonly attribute boolean headless; }; diff --git a/Tests/LibWeb/Text/expected/Internals/headless.txt b/Tests/LibWeb/Text/expected/Internals/headless.txt new file mode 100644 index 00000000000..30e0d223cd7 --- /dev/null +++ b/Tests/LibWeb/Text/expected/Internals/headless.txt @@ -0,0 +1 @@ +Browser is running headlessly: true diff --git a/Tests/LibWeb/Text/input/Internals/headless.html b/Tests/LibWeb/Text/input/Internals/headless.html new file mode 100644 index 00000000000..ab1cf4f3673 --- /dev/null +++ b/Tests/LibWeb/Text/input/Internals/headless.html @@ -0,0 +1,7 @@ + + +