-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape-linkedin-chapters.js
101 lines (68 loc) · 2.43 KB
/
scrape-linkedin-chapters.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
Project Name: EHW APP: Scrape LinkedIn Learning Course Chapter List
This Filename: scrape-linkedin-chapters.js
Date Created: 11/03/22
Date Updated: --
Programmer: Eric L. Hepperle
File Version: 1.00.00
File Purpose:
This ES6 JavaScript file contains the main "business logic" and primary
mechanics of the current project. This project scrapes LinkedIn Learning
course chapter/subchapters list from a LinkedIn Learning page. The data
is then is formatted for markdown (.MD), chapter numbers added where
missing, and subchapter numbers are added before being output to the console.
TAGS: JavaScript, ES6, DOM Manipulation, App, Eric L. Hepperle, Eric Hepperle,
console.log formatting
Usage:
Navigate to any LinkedIn Learning course page and paste this code into the
browser developer console and hit enter to run.
Sample results:
--
Requires:
* Browser
* LinkedIn Learning account
* Logged into LinkedIn on a course page
* #GOTCHA: You must manually open each chapter accordion
Demonstrates:
* Vanilla JavaScript
* JavaScript ES6 / ECMAScript2015
* Web Scraping
* DOM Traversal
* Arrow Functions
*/
/* _____________ VARIABLES __________________ */
// Selector Variables
sel_chGroups = '.classroom-toc-section'
// sel_subchGroup = '.classroom-toc-section__items'
sel_subchapter = '.classroom-toc-item__title'
/* _____________ ALGORITHM __________________ */
/*
Find all chapter groups
Foreach chapter group
Find chapter name. Print
Find the subchapter group (UL?)
Find all subchapter names. Print
*/
var colorChap = 'cadetblue'
var colorSubch = 'forestgreen'
var chStyle = `color: ${colorChap}; font-weight:bold`
var subStyle = `color: ${colorSubch}`
chGroups = document.querySelectorAll(sel_chGroups)
chGroups.forEach(function (chGroup, c) {
ch_name = chGroup.querySelector('h2').innerText
// Add numbers to chapter titles without numbers
if (c === 0) {
console.log(`%c### ${c}. ${ch_name}`, chStyle)
} else if (c === chGroups.length-1) {
console.log(`%c### ${chGroups.length-1}. ${ch_name}`, chStyle)
} else {
console.log(`%c### ${ch_name}`, chStyle)
}
subchGroup = chGroup.querySelectorAll(sel_subchapter)
subchGroup.forEach((subchapter, s) => {
subchName = subchapter.firstChild.wholeText.trim()
if (subchName != '' && subchName != null) {
console.log(`%c #### ${c}.${s} ${subchName}`, subStyle)
}
})
})