I have been trying and failing to penetrate the shadow-dom of the Google Vertex Chat Bot we are building at work, I need to style inside it. I Have tried many things, I threw in the suggestions my boss gave me from chat GPT that she insists should work. Thats to say I hav no one I work with who has ever worked with a shadow-dom so I am on my own here. I have watched a few videos but most seem to just show you how to build your own, not penetrate and existing one. Any guidance would be of help. <3
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Vertex Backup</title>
<meta name="description" content="Webpage for xxxx">
<link rel="stylesheet" href="assets/css/style.css">
<script src="assets/js/vertex.js"></script>
<script src="https://cloud.google.com/ai/gen-app-builder/client?hl=en_US"></script>
<style type="text/css">
.grecaptcha-badge {
visibility: hidden;
}
</style>
</head>
<body> <!-- Widget JavaScript bundle -->
<div class="host" id="host">
<gen-search-widget configId="numbers" triggerId="searchWidgetTrigger">
</gen-search-widget>
<input id="searchWidgetTrigger" type="button" value="Try a POC AI chatbot" disabled />
<div class="userConsentCheckbox" id="userConsent">
Placeholder for the dynamically created checkbox
</div>
</div>
</body>
</html>
document.addEventListener("DOMContentLoaded", function () {
console.log("linked");
// Step 1: Create a Shadow DOM
const host = document.getElementById("host");
if (host) {
const shadowRoot = host.attachShadow({ mode: "open" });
// Step 2: Add some content to the Shadow DOM
shadowRoot.innerHTML = `
<style>
.content {
background: blue;
}
</style>
`;
// Step 3: Access the Shadow DOM
const shadowContent = shadowRoot.querySelector(".content");
if (shadowContent) {
console.log(shadowContent.textContent); // Output: Inside Shadow DOM
} else {
console.error("Shadow DOM content not found.");
}
} else {
console.error("Host element not found.");
}
console.log("inside function");
// Create and append the button
const aiButton = document.createElement("input");
aiButton.type = "button";
aiButton.id = "searchWidgetTrigger";
aiButton.value = "Try a POC AI chatbot";
aiButton.disabled = true; // Disabled by default
document.body.appendChild(aiButton);
// Create and append the container for the checkbox
const aiContainer = document.createElement("div");
aiContainer.className = "userConsentCheckbox";
aiContainer.id = "userConsent";
document.body.appendChild(aiContainer);
// Create checkbox dynamically
const aiCheckbox = document.createElement("input");
aiCheckbox.type = "checkbox";
aiCheckbox.id = "userConsentCheckbox";
aiCheckbox.className = "userConsentCheckbox";
aiCheckbox.checked = false; // Unchecked by default
// Create label for the checkbox
const aiLabel = document.createElement("label");
aiLabel.htmlFor = "userConsentCheckbox"; // Set the label 'for' attribute
aiLabel.appendChild(aiCheckbox);
aiLabel.appendChild(document.createTextNode(" Consent to terms of use"));
console.log(aiLabel);
// Append label to container
if (aiContainer) {
aiContainer.appendChild(aiLabel);
console.log("ai-container");
// Add event listener to checkbox
aiCheckbox.addEventListener("change", function () {
aiButton.disabled = !this.checked;
console.log("Checkbox state changed");
});
// Ensure the button is disabled initially
aiButton.disabled = !aiCheckbox.checked;
} else {
console.error("Container element not found.");
}
// Create and configure the gen-search-widget element dynamically
const genSearchWidget = document.createElement("gen-search-widget");
genSearchWidget.setAttribute(
"configId",
"numbers"
);
genSearchWidget.setAttribute("triggerId", "searchWidgetTrigger");
document.body.appendChild(genSearchWidget);
//This was an attempt that didn't do what I wanted
// Access the shadow DOM
// genSearchWidget.addEventListener("click", () => {
// const genSearchShadowRoot = genSearchWidget.shadowRoot;
// if (genSearchShadowRoot) {
// const shadowContent = genSearchShadowRoot.querySelector(".content"); // Change this to the element you want to manipulate
// console.log("Shadow DOM!");
// if (shadowContent) {
// shadowContent.textContent = "Shadow DOM Content Modified!";
// console.log("Shadow DOM Content Modified!");
// } else {
// console.log("Element not found in shadow DOM.");
// }
// } else {
// console.log("Shadow root not found.");
// }
// });
});
CHAT GPTS SUGGESTIONS
- Internal Styling: You can include a
<style>
tag directly inside the shadow DOM. This is the most common method as it keeps the styles encapsulated within the component. Here’s an example:
class MyComponent extends HTMLElement {
constructor() {
super();
const shadow = this.attachShadow({mode: 'open'});
shadow.innerHTML = `
<style>
p {
color: blue;
}
</style>
<p>Hello, Shadow DOM!</p>
`;
}
}
customElements.define('my-component', MyComponent);
In this example, a <p>
element inside the shadow DOM is styled directly by CSS within the same shadow root.
- External Stylesheets: You can also link external stylesheets within the shadow DOM. This can be handy if you have common styles you wish to reuse across multiple components:
class MyComponent extends HTMLElement {
constructor() {
super();
const shadow = this.attachShadow({mode: 'open'});
shadow.innerHTML = `
<link rel="stylesheet" href="/styles/component-styles.css">
<p>Hello, Shadow DOM!</p>
`;
}
}
customElements.define('my-component', MyComponent);
Here, component-styles.css
will only apply to the contents of the shadow DOM where it’s included.
- CSS Custom Properties (Variables): While regular styles don’t penetrate the shadow boundary, CSS custom properties do. This allows you to define a consistent theme or style externally that can be applied inside the shadow DOM.
/* In your global stylesheet */
:root {
--main-color: green;
}
/* Inside your shadow DOM */
<style>
p {
color: var(--main-color);
}
</style>
This way, the paragraph inside the shadow DOM will use the color defined by the --main-color
variable.
- ::part and ::theme (Part of CSS Shadow Parts): If you need to style specific parts of your shadow DOM from outside, you can expose parts of the shadow DOM for styling using the
::part
selector. This requires marking parts of your shadow DOM with thepart
attribute.
// Inside your shadow DOM definition
shadow.innerHTML = `
<style>
p {
color: blue;
}
</style>
<p part="highlight">Hello, Shadow DOM!</p>
`;
/* In your global stylesheet */
my-component::part(highlight) {
color: red;
}
In this example, the <p>
element can be styled from outside the shadow DOM because it’s exposed via the part="highlight"
attribute.
These methods provide flexibility in styling components that use shadow DOM while maintaining the benefits of style encapsulation.
Sorry if this was too much info, it was just the easiest way to show what I am working with