Skip to content

Instantly share code, notes, and snippets.

@angeloevangelista
Last active May 31, 2023 17:34
Show Gist options
  • Save angeloevangelista/e3c8cfe1b6e2bfa52ec7034bf52fb21b to your computer and use it in GitHub Desktop.
Save angeloevangelista/e3c8cfe1b6e2bfa52ec7034bf52fb21b to your computer and use it in GitHub Desktop.
function fillTags(tagsObject) {
const tagsSpanElement =
document
.evaluate('(//h2/span[contains(., "Tags")])[last()]', document)
.iterateNext() ||
document
.evaluate('(//button/span[contains(., "Add new tag")])[last()]', document)
.iterateNext();
const checkElementIsRootContainer = (element) =>
!!element &&
element.tagName === "DIV" &&
element.textContent.includes("Tags") &&
element.textContent.includes("Add") &&
Array.from(element.classList).some(
(p) => p.includes("awsui_root") || p.includes("awsui_container")
);
let tagsContainerElement = tagsSpanElement;
do {
tagsContainerElement = tagsContainerElement.parentElement;
} while (
tagsContainerElement.tagName != "BODY" &&
!checkElementIsRootContainer(tagsContainerElement)
);
if (tagsContainerElement.tagName === "BODY") {
console.log('error: could not found "Tags" container');
return;
}
const addButtonElement = document
.evaluate('//button[contains(., "Add")]', tagsContainerElement)
.iterateNext();
Object.entries(tagsObject).forEach(([key, value]) => {
addButtonElement.click();
const [nextKeyInput, nextValueInput] = Array.from(
tagsContainerElement.querySelectorAll("input")
).slice(-2);
setTimeout(() => {
nextKeyInput.setAttribute("value", key);
nextKeyInput.dispatchEvent(new Event("change", { bubbles: true }));
nextValueInput.setAttribute("value", value);
nextValueInput.dispatchEvent(new Event("change", { bubbles: true }));
}, 10);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment