|
function reorderChecks() { |
|
console.log("Reordering GitHub PR checks..."); |
|
|
|
// Select all merge status items |
|
const mergeStatusItems = document.querySelectorAll( |
|
"div.merge-status-item:has(.merge-status-icon)" |
|
); |
|
|
|
// If no items found, exit the function |
|
if (mergeStatusItems.length === 0) return; |
|
|
|
// Convert NodeList to Array for easier manipulation |
|
const itemsArray = Array.from(mergeStatusItems); |
|
|
|
// Sort the array |
|
itemsArray.sort((a, b) => { |
|
const aSuccessful = a.querySelector(".octicon-check") !== null; |
|
const bSuccessful = b.querySelector(".octicon-check") !== null; |
|
|
|
// First, sort by status |
|
if (aSuccessful !== bSuccessful) { |
|
return aSuccessful ? 1 : -1; |
|
} |
|
|
|
const aName = a ? a.textContent.trim() : ""; |
|
const bName = b ? b.textContent.trim() : ""; |
|
|
|
return aName.localeCompare(bName); |
|
}); |
|
|
|
// Get the parent container |
|
const container = mergeStatusItems[0].parentNode; |
|
|
|
// Remove all existing items |
|
mergeStatusItems.forEach((item) => item.remove()); |
|
|
|
// Append sorted items back to the container |
|
itemsArray.forEach((item) => container.appendChild(item)); |
|
|
|
console.log("GitHub PR checks reordered."); |
|
} |
|
|
|
function waitForChecksAndReorder() { |
|
const branchActionItem = document.querySelector(".merge-status-list"); |
|
if (branchActionItem) { |
|
// If the merge-status-list exists, wait a short time for its content to fully load |
|
setTimeout(() => { |
|
try { |
|
reorderChecks(); |
|
} catch (error) { |
|
console.error("Error while reordering checks:", error); |
|
} |
|
}, 1000); |
|
} else { |
|
// If the merge-status-list doesn't exist yet, check again after a short delay |
|
setTimeout(waitForChecksAndReorder, 500); |
|
} |
|
} |
|
|
|
// Create and start a MutationObserver to detect when the merge-status-list is added to the DOM |
|
const observer = new MutationObserver((mutations, obs) => { |
|
const branchActionItem = document.querySelector(".merge-status-list"); |
|
if (branchActionItem) { |
|
waitForChecksAndReorder(); |
|
obs.disconnect(); // Stop observing once we've found the element |
|
} |
|
}); |
|
|
|
// Start observing the document with the configured parameters |
|
observer.observe(document.body, { childList: true, subtree: true }); |
|
|
|
document.addEventListener("DOMContentLoaded", (event) => { |
|
waitForChecksAndReorder(); |
|
|
|
observer.observe(document.body, { childList: true, subtree: true }); |
|
}); |