Skip to content

Instantly share code, notes, and snippets.

@jimkang
Last active June 21, 2019 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimkang/957cc22c815669069d5ee98b21c5c454 to your computer and use it in GitHub Desktop.
Save jimkang/957cc22c815669069d5ee98b21c5c454 to your computer and use it in GitHub Desktop.
Extracting content from a Wikipedia table
// Run this in the console when you have a Wikipedia page open.
// First, get the wikitable element you want and add an id of `main-table` to it.
// Then:
var rows = document.querySelectorAll('#main-table tr');
var extracted = [];
function extractFromRow(row) {
if (row.children.length > 2) {
let event = {
yearsFromNow: row.children[1].textContent.replace(/\n/g, '').replace(/\[note \d+\]/g, ''),
contentHTML: row.children[2].innerHTML.replace(/\n/g, '')
};
event.yearsNumber = +(event.yearsFromNow.replace(/[A-Za-z,]/g, ''))
extracted.push(event);
}
}
for (var i = 1; i < rows.length; ++i) {
extractFromRow(rows[i]);
}
extracted.unshift({ yearsFromNow: 0, yearsNumber: 0, contentHTML: 'Now.' });
copy(extracted);
// Then, paste what's on the clipboard into a file or wherever you need it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment