Created
March 30, 2025 15:05
-
-
Save eevmanu/abec937cd8a9880e9614f3a748cc4deb to your computer and use it in GitHub Desktop.
script to sort sciencedirect topics from the one that has more to the least in https://www.sciencedirect.com/topics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name ScienceDirect Topics Sorter | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description Sorts ScienceDirect topic blocks by count (descending) on Ctrl+Shift+S, toggles back to original order. | |
// @author Your Name Here | |
// @match https://www.sciencedirect.com/topics* | |
// @grant none | |
// @run-at document-idle | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const containerSelector = '.child-domain-blocks-grid'; | |
const itemSelector = 'a.child-domain-block'; | |
const countSelector = '.child-domain-count'; | |
let originalOrder = null; // To store the initial order of elements | |
let isSorted = false; // State flag | |
// --- Helper Functions --- | |
function getSortableItems(container) { | |
return [...container.querySelectorAll(itemSelector)]; | |
} | |
function getCountFromItem(item) { | |
const countElement = item.querySelector(countSelector); | |
if (!countElement || !countElement.textContent) { | |
return 0; // Handle cases where count element or text is missing | |
} | |
// Remove commas and parse as integer (base 10) | |
return parseInt(countElement.textContent.replace(/,/g, ''), 10) || 0; | |
} | |
function sortItems(container, items) { | |
// Sort based on the count (descending) | |
items.sort((a, b) => getCountFromItem(b) - getCountFromItem(a)); | |
// Re-append items in the new sorted order | |
items.forEach(item => container.appendChild(item)); | |
console.log("Topics sorted by count (desc)."); | |
} | |
function restoreOriginalOrder(container, originalItems) { | |
// Append items back in their original order | |
originalItems.forEach(item => container.appendChild(item)); | |
console.log("Topics restored to original order."); | |
} | |
// --- Event Listener --- | |
document.addEventListener('keydown', function(event) { | |
// Check for Ctrl + Shift + S | |
if (event.ctrlKey && event.shiftKey && event.key.toUpperCase() === 'S') { | |
event.preventDefault(); // Prevent default browser action (like Save As) | |
const container = document.querySelector(containerSelector); | |
if (!container) { | |
console.warn("Sorter: Container element not found:", containerSelector); | |
return; // Exit if the main container isn't found | |
} | |
// --- Logic --- | |
// If it's the first time, store the original order | |
if (originalOrder === null) { | |
originalOrder = getSortableItems(container); | |
if (originalOrder.length === 0) { | |
console.warn("Sorter: No sortable items found within the container."); | |
originalOrder = null; // Reset if nothing was found | |
return; | |
} | |
} | |
// Toggle between sorted and original state | |
if (isSorted) { | |
// Currently sorted, restore original | |
restoreOriginalOrder(container, originalOrder); | |
isSorted = false; | |
} else { | |
// Currently original (or first run), sort it | |
const itemsToSort = getSortableItems(container); // Get current items in case DOM changed | |
sortItems(container, itemsToSort); | |
isSorted = true; | |
} | |
} | |
}); | |
console.log("ScienceDirect Topics Sorter script loaded."); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment