Skip to content

Commit

Permalink
[cdp] Extract dom mutation listener
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Oct 5, 2020
1 parent 611c019 commit 3f210c1
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 40 deletions.
9 changes: 9 additions & 0 deletions java/client/src/org/openqa/selenium/devtools/BUILD.bazel
Expand Up @@ -27,6 +27,12 @@ java_library(
],
)

copy_file(
name = "mutation-listener",
src = "//javascript/cdp-support:mutation-listener.js",
out = "mutation-listener.js"
)

java_export(
name = "devtools",
srcs = glob(
Expand All @@ -38,6 +44,9 @@ java_export(
],
exclude = PROTOTYPE_SOURCES + GENERATOR_SOURCES,
),
resources = [
":mutation-listener",
],
maven_coordinates = "org.seleniumhq.selenium:selenium-devtools:%s" % SE_VERSION,
pom_template = "//java/client/src/org/openqa/selenium:template-pom",
visibility = [
Expand Down
Expand Up @@ -17,7 +17,7 @@

package org.openqa.selenium.devtools.events;

import com.google.common.base.Joiner;
import com.google.common.io.Resources;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
Expand All @@ -29,10 +29,13 @@
import org.openqa.selenium.logging.EventType;
import org.openqa.selenium.logging.HasLogEvents;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.openqa.selenium.json.Json.MAP_TYPE;

public class CdpEventTypes {
Expand Down Expand Up @@ -64,45 +67,18 @@ public void initializeListener(HasLogEvents loggable) {
}

public static EventType<Void> domMutation(Consumer<DomMutationEvent> handler) {
String script = Joiner.on("\n").join(new String[]{
"(function() {",
"const observer = new MutationObserver((mutations) => {",
" for (const mutation of mutations) {",
" switch (mutation.type) {",
" case \"attributes\":",
// Don't report our own attribute has changed.
" if (mutation.attributeName == 'data-__webdriver_id') {",
" break;",
" }",
" const curr = mutation.target.getAttribute(mutation.attributeName);",
" var id = mutation.target.dataset.__webdriver_id",
" if (!id) {",
" id = Math.random().toString(36).substring(2) + Date.now().toString(36);",
" mutation.target.dataset.__webdriver_id = id;",
" }",
" const json = JSON.stringify({",
" \"target\": id,",
" \"name\": mutation.attributeName,",
" \"value\": curr,",
" \"oldValue\": mutation.oldValue",
" });",
" __webdriver_attribute(json);",
" break;",
" default:",
" break;",
" }",
" }",
"});",
"observer.observe(document, {",
" \"attributes\": true,",
" \"attributeOldValue\": true,",
" \"characterData\": true,",
" \"characterDataOldValue\": true,",
" \"childList\": true,",
" \"subtree\": true",
"});",
"})();"
});
Require.nonNull("Handler", handler);

URL url = CdpEventTypes.class.getResource("/org/openqa/selenium/devtools/mutation-listener.js");
if (url == null) {
throw new IllegalStateException("Unable to find helper script");
}
String script;
try {
script = Resources.toString(url, UTF_8);
} catch (IOException e) {
throw new IllegalStateException("Unable to read helper script");
}

return new EventType<Void>() {
@Override
Expand Down
7 changes: 7 additions & 0 deletions javascript/cdp-support/BUILD.bazel
@@ -0,0 +1,7 @@
package(default_visibility = [
"//java/client/src/org/openqa/selenium/devtools:__pkg__",
])

exports_files([
"mutation-listener.js",
])
38 changes: 38 additions & 0 deletions javascript/cdp-support/mutation-listener.js
@@ -0,0 +1,38 @@
(function () {
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
switch (mutation.type) {
case 'attributes':
// Don't report our own attribute has changed.
if (mutation.attributeName === "data-__webdriver_id") {
break;
}
const curr = mutation.target.getAttribute(mutation.attributeName);
var id = mutation.target.dataset.__webdriver_id
if (!id) {
id = Math.random().toString(36).substring(2) + Date.now().toString(36);
mutation.target.dataset.__webdriver_id = id;
}
const json = JSON.stringify({
'target': id,
'name': mutation.attributeName,
'value': curr,
'oldValue': mutation.oldValue
});
__webdriver_attribute(json);
break;
default:
break;
}
}
});

observer.observe(document, {
'attributes': true,
'attributeOldValue': true,
'characterData': true,
'characterDataOldValue': true,
'childList': true,
'subtree': true
});
})();

0 comments on commit 3f210c1

Please sign in to comment.