Skip to content

Instantly share code, notes, and snippets.

@Pezmc
Created August 1, 2018 16:06
Show Gist options
  • Save Pezmc/cefce09c9672dc0f124b6015a209448d to your computer and use it in GitHub Desktop.
Save Pezmc/cefce09c9672dc0f124b6015a209448d to your computer and use it in GitHub Desktop.
Automatically add a list of games to a Board Game Geek GeekList
//= Usage
// Update the config below
// Paste this file into the console on the edit geeklist page
// Call addAllGames()
// Config
let gamelist = []; // Set this to an array containing the names of games you wish to add
// e.g. let gamelist = ["Azul", "Carcassonne", "Codenames"];
// Exposed so they can be logged
let failedToFind = [];
let listOfGamesInGeeklist = [];
let listOfDuplicates = [];
let uniq = [];
function addAllGames() {
if (!gamelist.length) {
console.log("COMPLETE");
if (failedToFind.length) {
console.warn(`Failed to find ${failedToFind.length} games`);
console.log(failedToFind);
}
return checkForDuplicates();
}
gameName = gamelist.shift();
if (jQuery(`.geeklist_ajax_table a:containsIN("${gameName}")`).length) {
console.log(`Skipping already added game ${gameName}`);
setTimeout(addAllGames, 100);
} else {
console.log(`Trying to add ${gameName}`);
openAddForm()
.then(searchForGame.bind(this, gameName))
.then(submitForm)
.catch(() => {})
.finally(addAllGames);
}
}
function openAddForm() {
return new Promise((resolve, reject) => {
jQuery('#itemtypes a:contains("Board Game")').first().click();
let int = setInterval(() => {
if (!jQuery("#newitem:contains('Loading')").length) {
clearInterval(int);
resolve();
} else {
console.info('Still loading...');
}
}, 500);
});
}
function searchForGame(gameName) {
return new Promise((resolve, reject) => {
jQuery('[name="geekitemname"]').val(gameName).keydown();
let int = setInterval(() => {
if (jQuery('.searchbox_results:contains("No Results")').length) {
console.warn(`No results for ${gameName}`);
failedToFind.push(gameName);
jQuery('input[type="button"][value="Cancel"]').click();
clearInterval(int);
return reject();
} else if (jQuery('.searchbox_results:contains("Select one")').length) {
jQuery('.searchbox_results div div').first().click();
clearInterval(int);
resolve();
}
}, 500);
});
}
function submitForm() {
return new Promise((resolve, reject) => {
jQuery('#savebutton0').click();
let int = setInterval(() => {
if (!jQuery("#status:contains('Saving')").length) {
clearInterval(int);
console.log("Added game");
resolve();
} else {
console.info('Still saving...');
}
}, 500);
});
}
function checkForDuplicates() {
listOfGamesInGeeklist = jQuery.find(".geeklist_ajax_table a[href^='/boardgame/']")
.map((el, i) => el.innerText)
.sort();
uniq = listOfGamesInGeeklist.map((name) => {
return {count: 1, name: name}
})
.reduce((a, b) => {
a[b.name] = (a[b.name] || 0) + b.count
return a
}, {});
listOfDuplicates = Object.keys(uniq).filter((a) => uniq[a] > 1)
if (listOfDuplicates.length > 0) {
console.warn(`Found ${listOfDuplicates.length} duplicate entries, manually delete them`);
console.log(listOfDuplicates);
console.warn(`You can call checkForDuplicates() to re-check`);
}
}
(function injectjQuery() {
const jq = document.createElement('script');
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
setTimeout(() => {
jQuery.noConflict();
jQuery.extend(jQuery.expr[":"], {
"containsIN"(elem, i, match, array) {
return (elem.textContent || elem.innerText || "").toLowerCase().includes((match[3] || "").toLowerCase());
}
});
console.info("jQuery Injected");
}, 1000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment