// ==UserScript==
// @name Pestpac - Easy Quick Scheduler Buttons
// @version 3.22
// @description Adds buttons to select technicians by area and dates up to 7 days from the latest selected date in the calendar in Pestpac
// @match https://app.pestpac.com/appointment/*
// @author Jamie Cruz
// @grant none
// @license MIT
// @namespace https://greasyfork.org/users/1433767
// ==/UserScript==
(function() {
'use strict';
function addTechButton(buttonText, color, rowRanges) {
var button = document.createElement("button");
button.innerHTML = buttonText;
button.style.margin = "10px";
button.style.padding = "10px";
button.style.backgroundColor = color;
button.style.color = "white";
button.style.border = "none";
button.style.borderRadius = "5px";
button.style.cursor = "pointer";
button.style.width = "88px"; // Adjust the width
button.style.height = "25px"; // Adjust the height
var refreshSearchButton = document.getElementById("butRefreshSearch");
if (refreshSearchButton) {
refreshSearchButton.parentNode.insertBefore(button, refreshSearchButton.nextSibling);
} else {
console.error("Element with ID 'butRefreshSearch' not found.");
}
button.addEventListener("click", function() {
// Unselect all previously selected rows and remove their class
document.querySelectorAll("tr.SelectedRow").forEach(row => {
row.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, view: window }));
row.classList.remove("SelectedRow");
});
// Add "SelectedRow" class to specified rows and trigger click event
for (let range of rowRanges) {
for (let i = range.start; i <= range.end; i++) {
let row = document.querySelector("table.insetTable #MultiTechRow" + i);
if (row) {
row.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, view: window }));
row.classList.add("SelectedRow");
} else {
console.error("Row with ID 'MultiTechRow" + i + "' not found.");
}
}
}
});
}
function addSelectButton(buttonText, color) {
var button = document.createElement("button");
button.innerHTML = buttonText;
button.style.margin = "5px"; // Adjust the margin for better fit
button.style.padding = "1px 3px"; // Adjust the padding for better fit
button.style.backgroundColor = color;
button.style.color = "white";
button.style.border = "none";
button.style.borderRadius = "5px";
button.style.cursor = "pointer";
button.style.width = "18px"; // Adjust the width
button.style.height = "18px"; // Adjust the height
var updateCalendarButton = document.getElementById("butUpdateCalendarDate");
if (updateCalendarButton) {
updateCalendarButton.style.width = "62px"; // Adjust the width of the update calendar button
var newCell = document.createElement("td");
newCell.align = "right";
newCell.appendChild(button);
var parentRow = updateCalendarButton.closest("tr");
if (parentRow) {
var nextSibling = updateCalendarButton.closest("td").nextElementSibling;
if (nextSibling) {
parentRow.insertBefore(newCell, nextSibling);
} else {
parentRow.appendChild(newCell);
}
// Align the existing cell to the center
updateCalendarButton.closest("td").align = "center";
} else {
console.error("Parent row of the update calendar button not found.");
}
// Adjust the table width
var parentTable = updateCalendarButton.closest("table");
if (parentTable) {
parentTable.style.width = "200px"; // Set the width of the table to 200px
} else {
console.error("Parent table of the update calendar button not found.");
}
} else {
console.error("Element with ID 'butUpdateCalendarDate' not found.");
}
button.addEventListener("click", function() {
// Find the latest selected date
var selectedDates = document.querySelectorAll(".selectedcalendarlink");
if (selectedDates.length === 0) {
alert("No date selected.");
return;
}
var latestSelectedDate = selectedDates[selectedDates.length - 1];
var latestDateText = latestSelectedDate.textContent.trim();
var latestDate = new Date(latestDateText);
// Select dates up to 7 days from the latest selected date
var calendarDates = document.querySelectorAll("table.insetTable .calendarlink, table.insetTable .selectedcalendarlink");
var count = 0;
var latestFound = false;
for (var i = 0; i < calendarDates.length; i++) {
var dateText = calendarDates[i].textContent.trim();
var date = new Date(dateText);
if (latestFound && count < 7) {
calendarDates[i].dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, view: window }));
count++;
}
if (dateText === latestDateText) {
latestFound = true;
}
}
});
}
window.onload = function() {
// Add tech buttons
addTechButton("MD TM", "#de8285", [{ start: 8, end: 10 }, { start: 15, end: 15}]);
addTechButton("VA TM", "#ADD8E6", [{ start: 4, end: 4 }, { start: 6, end: 7 }]);
addTechButton("MD ACT", "#de8285", [{ start: 8, end: 11 }, { start: 14, end: 17 }]);
addTechButton("VA ACT", "#ADD8E6", [{ start: 3, end: 4 }, { start: 6, end: 7 }]);
addTechButton("MD techs", "#E03a3e", [{ start: 8, end: 18 }]);
addTechButton("VA techs", "#00297B", [{ start: 3, end: 7 }]);
// Add select button
addSelectButton("+7", "#1565C0");
};
})();