// ==UserScript==
// @name Automate Change Link Clicker
// @namespace http://example.com
// @version 1.0
// @description Automate clicking on "Change" links and subsequent actions.
// @match https://sellercentral.amazon.co.uk/bulk-buy-shipping*
// @grant none
// ==/UserScript==
(function() {
// Function to simulate a click event
function simulateClick(element) {
var event = new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window
});
element.dispatchEvent(event);
}
function processTableRows() {
let table = document.evaluate('//*[@id="MYO-ST-app"]/div/div[2]/table', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
let rows = table.querySelectorAll('tr:not(:first-child)');
let results = [];
rows.forEach(row => {
let changeElem = row.querySelector('td:nth-child(4) div div:last-child a.a-link-normal');
let removeBtn = row.querySelector('td:last-child span.a-button span.a-button-inner');
if (changeElem || removeBtn) {
results.push({
changeElem: changeElem,
removeBtn: removeBtn
});
}
});
return results.filter(item => !(item.changeElem === null && item.removeBtn === null));
}
async function processResults(results, selectedOption) {
for (let index = 0; index < results.length; index++) {
const item = results[index];
if (item.changeElem) {
simulateClick(item.changeElem);
let foundRow = await new Promise(resolve => {
setTimeout(() => {
let rows = document.querySelectorAll('.shipping-service-selector-row');
let rowFound = false;
for (let row of rows) {
let tds = row.querySelectorAll('td');
if (tds.length >= 3 && tds[2].textContent.trim() === selectedOption) {
console.log("Found the row for link index:", index);
simulateClick(tds[0]);
rowFound = true;
break;
}
}
resolve(rowFound);
}, 1000);
});
if (!foundRow && item.removeBtn) {
simulateClick(item.removeBtn);
}
} else if (item.removeBtn) {
simulateClick(item.removeBtn);
}
}
}
function runScript() {
let selectedOption = dropdown.value;
let results = processTableRows();
processResults(results, selectedOption);
}
// Create a dropdown with options
let dropdown = document.createElement('select');
[
"Parcel Next Day",
"Amazon Shipping Standard Tracked",
"Amazon Shipping One-Day Tracked",
"Hermes Standard",
"Hermes Standard - Drop Off",
"Hermes Next Day",
"Hermes Two Day - Drop Off",
"Parcel Saturday",
"Royal Mail Tracked 24",
"Royal Mail 24 (Parcel) (CRL)",
"Royal Mail 1st Class Medium Parcel",
"Royal Mail Signed For 1st Class Medium Parcel",
"Royal Mail Special Delivery Guaranteed by 1pm (Drop Off)"
].forEach(optionText => {
let option = document.createElement('option');
option.value = optionText;
option.innerText = optionText;
dropdown.appendChild(option);
});
// Styling the dropdown
dropdown.style.backgroundColor = 'lightgrey';
// Create the button to run the script
let btn = document.createElement('button');
btn.innerText = 'Change Shipping';
btn.style.backgroundColor = 'lightgrey';
btn.addEventListener('click', runScript);
// Create a container div to hold both dropdown and button
let container = document.createElement('div');
container.style.position = 'fixed';
container.style.top = '10px';
container.style.right = '10px';
container.style.zIndex = '9999';
container.style.display = 'flex';
container.style.gap = '5px';
container.appendChild(dropdown);
container.appendChild(btn);
document.body.appendChild(container);
})();