Skip to content

Instantly share code, notes, and snippets.

@saiki-k
Last active January 28, 2024 07:07
Show Gist options
  • Save saiki-k/44ef442c9690e8d0862926604dd50731 to your computer and use it in GitHub Desktop.
Save saiki-k/44ef442c9690e8d0862926604dd50731 to your computer and use it in GitHub Desktop.
Humble Keys' Extractor
const excludedTitles = ['A title I already own', 'Got this other title too'];
const { includedKeysCombined, csv } = run(excludedTitles);
copy(includedKeysCombined);
function scrapeTitlesAndKeys() {
const result = [];
// Get all elements with classes "key-redeemer" (Normal key pages), and "multiselect-nonimage-content" (Choice key pages)
const elements = document.querySelectorAll('.key-redeemer, .multiselect-nonimage-content');
// Iterate through each element and extract title and key
elements.forEach((element) => {
const keyElement = element.querySelector('.keyfield-value');
let titleElement;
// Check if the element has class "key-redeemer"
if (element.classList.contains('key-redeemer')) {
titleElement = element.querySelector('.heading-text h4');
}
// Check if the element has class "multiselect-nonimage-content"
else if (element.classList.contains('multiselect-nonimage-content')) {
titleElement = element.querySelector('.multiselect-title');
}
// Check if both title and key elements exist
if (titleElement && keyElement) {
const title = titleElement.textContent.trim();
const key = keyElement.textContent.trim();
// Push an object with title and key into the result array
result.push({ title, key });
}
});
return result;
}
function filterTitlesAndKeys(titlesAndKeys, excludedTitles = []) {
// Convert excludedTitles to lowercase for case-insensitive comparison
const lowercasedExcludedTitles = excludedTitles.map((title) => title.trim().toLowerCase());
// Initialize arrays to store included and excluded items
const includedItems = [];
const excludedItems = [];
// Iterate through titles and keys
titlesAndKeys.forEach((item) => {
const lowercaseTitle = item.title.toLowerCase();
// Check if the title should be excluded
if (lowercasedExcludedTitles.includes(lowercaseTitle)) {
excludedItems.push(item);
} else {
includedItems.push(item);
}
});
return { includedItems, excludedItems };
}
function run(excludedTitles = [], sectionSeparator = '***\n\n\n\n') {
// Run scrapeTitlesAndKeys to get the titles and keys
const allItems = scrapeTitlesAndKeys();
// Run filterTitlesAndKeys to get the included and excluded items
const { includedItems, excludedItems } = filterTitlesAndKeys(allItems, excludedTitles);
// Create a CSV string from an array of objects with title and key
const createCSV = (titlesAndKeys) => `Title,Key\n${titlesAndKeys.map((item) => Object.values(item)).join('\n')}`;
const data = {
allItems,
includedItems,
excludedItems,
csv: {
allItems: createCSV(allItems),
includedItems: createCSV(includedItems),
excludedItems: createCSV(excludedItems),
},
includedKeysCombined: includedItems.map((item) => item.key).join('\n'),
};
// Print all the titles, and their keys in a table
console.log(`${data.allItems.length} keys have been scraped from the page.`);
console.table(data.allItems);
// Print the CSV of all the titles and keys
console.log(`${sectionSeparator}The CSV of all the titles and keys:`);
console.log(data.csv.allItems);
if (data.excludedItems.length >= 1) {
console.log(`${sectionSeparator}Following keys were excluded:`);
console.log(data.excludedItems.map((item) => `${item.title}\n${item.key}\n`).join('\n'));
}
if (data.includedItems.length >= 1) {
console.log(`${sectionSeparator}Following included titles' keys form the result string:`);
console.log(data.includedItems.map((item) => item.title).join('\n'));
console.log('The keys for the above included games in order are:');
console.log(data.includedKeysCombined);
}
return data;
}
@saiki-k
Copy link
Author

saiki-k commented Nov 11, 2023

Hey fellow Humble Bundlers! ๐ŸŽฎ๐Ÿ”‘

This console script is meant for Humble Bundle key pages. It gathers all keys from a Humble Bundle key page, excluding the titles you specify, and compiles them into a single string that is copied to your clipboard. This makes redeeming a bunch of keys a breeze, especially if you have Augmented Steam installed (because who doesn't?).

With the Augmented Steam browser extension installed, after running this script, you can effortlessly redeem all your keys at once on the Steam key activator page (https://store.steampowered.com/account/registerkey) by pasting the clipboard content into the provided text area.

The copy function (line 5) (for copying content to clipboard) only works on Chromium based browsers, AFAIK!

How to use

  1. Copy the entire script. ๐Ÿ“‹
  2. Open your browser's console (only works on Chromium browsers, afaik, because of the copy function, on line 5)
  3. Paste the script into the console.
  4. Let's say you've got some titles you already own or don't want to redeem right now.
    • No worries!
    • Change the array on the first line to contain the titles whose keys you don't want in the final combined keys' string
    const excludedTitles = ['A title I already own', 'Got this other title too'];
    
  5. Press enter, and you're done. ๐ŸŽ‰
  6. Upon a successful execution of the script, your clipboard will have the combined key string of all the included titles.
    • You can change line 5 in the script, before executing it, to one of the following lines; to instead copy the CSV data of either all, or the excluded, or the included items, respectively.
    copy(csv.allItems);
    copy(csv.includedItems);
    copy(csv.excludedItems);
    
    • The above CSV data can be copied and pasted into a text file with a .csv extension (say keys.csv), suitable for opening with software like Excel for further organization.

Behind the scenes

  • The script identifies keys and titles from both normal (.key-redeemer) and choice (.multiselect-nonimage-content) Humble Bundle key pages.
  • It prints a neat table in the console with all the titles and keys for a quick check.
  • You can exclude specific titles from the final string. Handy if you've got games you already own, or simply don't want.
  • If any keys are excluded, they get their own little section in the console output, so you can copy and move them to a safe place.
  • Extracted titles โ€” to be redeemed โ€” are shown (in order) so you can double-check if everything's cool.
  • After you run the script, the final string of keys is ready for you to copy and paste into Steam key activator.

Feel free to tweak and modify the script based on your needs. Happy key redeeming! ๐Ÿš€๐ŸŽ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment