Skip to content

Instantly share code, notes, and snippets.

@abdennebi
Forked from saiki-k/humbleKeysExtractor.js
Created November 12, 2023 20:34
Show Gist options
  • Save abdennebi/75e51d76e7e74f6bd5dab978857b83f0 to your computer and use it in GitHub Desktop.
Save abdennebi/75e51d76e7e74f6bd5dab978857b83f0 to your computer and use it in GitHub Desktop.
Humble Keys' Extractor
copy(createKeysString(['A title I already own', 'Got this other title too']));
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 createKeysString(
excludedTitles = [],
separator = '\n',
sectionSeparator = '***\n\n\n\n',
) {
// Run scrapeTitlesAndKeys to get the titles and keys
const titlesAndKeys = scrapeTitlesAndKeys();
// Print all the titles, and their keys in a table
console.log(`${titlesAndKeys.length} keys have been scraped from the page.`)
console.table(titlesAndKeys);
// Convert excludedTitles to lowercase for case-insensitive comparison
const lowercasedExcludedTitles = excludedTitles.map(
title => title.trim().toLowerCase()
);
// Initialize arrays to store extracted and excluded keys
const extractedKeys = [];
const excludedKeys = [];
// Iterate through titles and keys
titlesAndKeys.forEach(item => {
const lowercaseTitle = item.title.toLowerCase();
// Check if the title should be excluded
if (lowercasedExcludedTitles.includes(lowercaseTitle)) {
excludedKeys.push({ title: item.title, key: item.key });
} else {
extractedKeys.push({ title: item.title, key: item.key });
}
});
// Join all keys, and titles from the extractedKeys array into resp. strings
const extractedKeysString = extractedKeys.map(item => item.key).join(separator);
const extractedTitlesString = extractedKeys.map(item => item.title).join(separator);
if (excludedKeys.length >= 1) {
console.log(`${sectionSeparator}Following keys were excluded:`);
console.log(
excludedKeys.map(item => `${item.title}\n${item.key}\n`).join('\n')
);
}
if (extractedKeys.length >= 1) {
console.log(`${sectionSeparator}Following titles' keys form the result string:`);
console.log(extractedTitlesString);
}
console.log(`${sectionSeparator}Extracted keys' string:`);
console.log(extractedKeysString);
return extractedKeysString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment