Last active
June 21, 2019 01:05
-
-
Save jimkang/957cc22c815669069d5ee98b21c5c454 to your computer and use it in GitHub Desktop.
Extracting content from a Wikipedia table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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