Skip to content

Instantly share code, notes, and snippets.

@alan345
Last active January 7, 2025 17:51
Show Gist options
  • Save alan345/84e5a39959bddd0fce3c265db27bad7c to your computer and use it in GitHub Desktop.
Save alan345/84e5a39959bddd0fce3c265db27bad7c to your computer and use it in GitHub Desktop.
const getUrlFromTable = () => {
let url = "";
const tables = document.querySelectorAll("table");
tables.forEach((table) => {
const theadText = table.querySelector("thead th")?.textContent.trim();
if (theadText === "Technician Concerns") {
const tbodyRows = table.querySelectorAll("tbody tr");
const params = [];
tbodyRows.forEach((row) => {
const cells = row.querySelectorAll("td");
if (cells.length >= 2) {
const firstColumn = encodeURIComponent(
cells[0]?.textContent
.trim()
.replace(/\s+/g, "-")
.replace(/[^a-zA-Z0-9-]/g, "")
);
const secondColumn = encodeURIComponent(cells[1]?.textContent.trim());
params.push(`${firstColumn}=${secondColumn}`);
}
});
url = `https://carvis.ai?${params.join("&")}`;
}
});
return url;
};
const createButton = (url) => {
const targetElement = Array.from(
document.querySelectorAll("th.MuiTableCell-root")
).find((el) => el.textContent.trim() === "Technician Concerns");
if (targetElement) {
const button = document.createElement("button");
button.textContent = "Ask Carvis";
button.style.backgroundColor = "blue";
button.style.color = "white";
button.style.border = "none";
button.style.borderRadius = "30px";
button.style.width = "80px";
button.style.height = "20px";
button.style.cursor = "pointer";
button.style.marginLeft = "10px";
const link = document.createElement("a");
link.href = url;
link.target = "_blank";
link.appendChild(button);
targetElement.appendChild(link);
} else {
console.log(
"Text 'Technician Concerns' not found in the specified <th> elements."
);
}
};
const url = getUrlFromTable();
createButton(url);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment