import * as https from 'https' import * as jsdom from 'jsdom' import * as $ from 'jquery' function getDocument(url: URL|string): Promise { return new Promise((resolve, reject) => { https.get(url, (res) => { // if failing to load page, reject if (!res.statusCode || res.statusCode < 200 || 300 <= res.statusCode) { reject(new Error(` Failed to load. Status Code: ${res.statusCode || 'no status code found'} `)) res.resume() return; } res.setEncoding('utf8') const body: string[] = [] res.on('data', (chunk) => { body.push(chunk) }) res.on('end', () => { let data: Document; // if failing to parse contents as Document, reject try { data = new jsdom.JSDOM(body.join('')).window.document } catch (err) { reject(err) return; } resolve(data) }) }).on('error', (err) => { reject(err) }) // if failing to get url, reject }) } function related(document: Document): Promise { return Promise.all( [...$('h2 > span#See_also', document).parent().nextUntil('h2').find('ul').addBack('ul')] .filter((ul) => [...ul.children].every((li) => li.children.length === 1 && li.children[0].matches('a[href]'))) .map((ul) => [...$(ul).find('a')]) .reduce((x, y) => x.concat(y)) // COMBAK `Array#flat` .map((a) => getDocument((a as HTMLAnchorElement).href)) ) }