ソースを参照

LibWeb: Implement navigator.webdriver

This is defined via the NavigatorAutomationInformation interface mixin
from the WebDriver spec: https://w3c.github.io/webdriver/#interface
Linus Groh 2 年 前
コミット
a982e0380e

+ 14 - 0
Userland/Libraries/LibWeb/HTML/Navigator.cpp

@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
+ * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  *
  * SPDX-License-Identifier: BSD-2-Clause
  */
@@ -8,6 +9,9 @@
 #include <LibJS/Runtime/Realm.h>
 #include <LibWeb/Bindings/Intrinsics.h>
 #include <LibWeb/HTML/Navigator.h>
+#include <LibWeb/HTML/Scripting/Environments.h>
+#include <LibWeb/HTML/Window.h>
+#include <LibWeb/Page/Page.h>
 
 namespace Web::HTML {
 
@@ -24,4 +28,14 @@ Navigator::Navigator(JS::Realm& realm)
 
 Navigator::~Navigator() = default;
 
+// https://w3c.github.io/webdriver/#dfn-webdriver
+bool Navigator::webdriver() const
+{
+    // Returns true if webdriver-active flag is set, false otherwise.
+
+    // NOTE: The NavigatorAutomationInformation interface should not be exposed on WorkerNavigator.
+    auto const& window = verify_cast<HTML::Window>(HTML::current_global_object());
+    return window.page()->is_webdriver_active();
+}
+
 }

+ 2 - 0
Userland/Libraries/LibWeb/HTML/Navigator.h

@@ -39,6 +39,8 @@ public:
     // https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-pdfviewerenabled
     bool pdf_viewer_enabled() const { return false; }
 
+    bool webdriver() const;
+
     virtual ~Navigator() override;
 
 private:

+ 8 - 3
Userland/Libraries/LibWeb/HTML/Navigator.idl

@@ -9,8 +9,8 @@ interface Navigator {
     // objects implementing this interface also implement the interfaces given below
 };
 
-// NOTE: As NavigatorContentUtils, NavigatorCookies, and NavigatorPlugins are not used in WorkerNavigator,
-//       we define them here.
+// NOTE: As NavigatorContentUtils, NavigatorCookies, NavigatorPlugins, and NavigatorAutomationInformation
+//       are not used in WorkerNavigator, we define them here.
 
 // https://html.spec.whatwg.org/multipage/system-state.html#navigatorcontentutils
 interface mixin NavigatorContentUtils {
@@ -31,6 +31,11 @@ interface mixin NavigatorPlugins {
     readonly attribute boolean pdfViewerEnabled;
 };
 
+// https://w3c.github.io/webdriver/#dom-navigatorautomationinformation
+interface mixin NavigatorAutomationInformation {
+    readonly attribute boolean webdriver;
+};
+
 Navigator includes NavigatorID;
 Navigator includes NavigatorLanguage;
 Navigator includes NavigatorOnLine;
@@ -38,4 +43,4 @@ Navigator includes NavigatorContentUtils;
 Navigator includes NavigatorCookies;
 Navigator includes NavigatorPlugins;
 Navigator includes NavigatorConcurrentHardware;
-
+Navigator includes NavigatorAutomationInformation;