Skip to content

Instantly share code, notes, and snippets.

@seleb
Created July 4, 2020 07:30
Show Gist options
  • Save seleb/bccc49cd727914bc9847c38e95adeb63 to your computer and use it in GitHub Desktop.
Save seleb/bccc49cd727914bc9847c38e95adeb63 to your computer and use it in GitHub Desktop.
itch sort collection
async function getCollectionUrls(collectionName) {
// find collection
const collection = Array.from(document.querySelectorAll('.game_collection')).find(i => {
const titleEl = i.querySelector('.collection_title_wrap');
return titleEl && titleEl.innerText.toLowerCase() === collectionName.toLowerCase();
});
if (!collection) {
throw new Error(`Could not find collection "${collectionName}`);
}
// large collections are lazy-loaded;
// ensure all entries are loaded by scrolling all the way to the right
const next = collection.querySelector('.paddle_next');
async function pollLoading() {
await new Promise(resolve => setTimeout(resolve, 300));
if (!collection.querySelector('.loading')) {
return;
}
await pollLoading();
}
while (!collection.querySelector('.on_right, .no_scrollbar, .loading')) {
next.click();
await pollLoading();
}
// get the list of games
const games = Array.from(collection.querySelectorAll('.game_cell'));
console.log(games.map(game => game.querySelector('.game_link').href));
}
getCollectionUrls('Collection Name');
// Note: must be run from same-origin,
// so only supports games from a single account
async function mapToDate(href) {
const page = await (await fetch(href)).text();
const match = page.match(/Published<\/td>.*?<abbr title=\"(.*?)\"/);
if (!match) {
throw new Error(`Could not get publish date for "${href}"`);
}
const date = Date.parse(match[1].replace('@', ''));
return { href, date };
}
async function getChronologicalOrder(urls) {
// get the target order of the list of games
const withDates = await urls.reduce(async (result, i) => {
const array = await result;
array.push(await mapToDate(i));
return array;
}, Promise.resolve([]));
withDates.sort(({ date: a }, { date: b }) => b - a);
console.log(withDates.map(({ href }) => href));
}
getChronologicalOrder([
// list of urls in collection goes here
]);
async function sortCollection(collectionName, mapFn, sortFn) {
// find collection
const collection = Array.from(document.querySelectorAll('.game_collection')).find(i => {
const titleEl = i.querySelector('.collection_title_wrap');
return titleEl && titleEl.innerText.toLowerCase() === collectionName.toLowerCase();
});
if (!collection) {
throw new Error(`Could not find collection "${collectionName}`);
}
// large collections are lazy-loaded;
// ensure all entries are loaded by scrolling all the way to the right
const next = collection.querySelector('.paddle_next');
async function pollLoading() {
await new Promise(resolve => setTimeout(resolve, 300));
if (!collection.querySelector('.loading')) {
return;
}
await pollLoading();
}
while (!collection.querySelector('.on_right, .no_scrollbar, .loading')) {
next.click();
await pollLoading();
}
// get the list of games
const games = Array.from(collection.querySelectorAll('.game_cell'));
console.log(games);
// get the target order of the list of games
const sortTarget = games.slice();
const sortBy = await Promise.all(games.map(mapFn));
const sortByMap = new Map();
sortBy.forEach((i, idx) => {
sortByMap.set(games[idx], i);
});
sortTarget.sort((a, b) => sortFn(sortByMap.get(a), sortByMap.get(b)));
console.log(sortTarget);
// reorder the games to match the target order
for (let i = 0; i < sortTarget.length; ++i) {
const game = sortTarget[i];
while (Array.from(game.parentElement.children).indexOf(game) !== i) {
const dir = Math.sign(Array.from(game.parentElement.children).indexOf(game) - i);
if (dir > 0) {
game.querySelector('.move_left_btn').click();
} else {
game.querySelector('.move_right_btn').click();
}
await new Promise(resolve => setTimeout(resolve, 200));
}
}
console.log('done!');
}
// alphabetical
// sortCollection('Collection Name', game => game.querySelector('.title').innerText, (a,b) => a.localeCompare(b));
// chronological
sortCollection(
'Collection Name',
game => {
return [
// copied urls from get chrono
].indexOf(game.querySelector('.game_link').href);
},
(a, b) => a - b
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment