|
1 | | - |
2 | 1 | // Extract actors list on a keypress event. |
3 | | -document.addEventListener("keypress", function(){ |
4 | | - |
| 2 | +document.addEventListener("keypress", function() { |
5 | 3 | // Select the table element with cast_list CSS class |
6 | 4 | let table = document.querySelector("table.cast_list"); |
7 | 5 |
|
8 | 6 | // Select all character rows from the table |
9 | 7 | let rows = table.querySelectorAll("tr.odd, tr.even"); |
10 | | - |
11 | | - let i; |
| 8 | + |
| 9 | + // Clear console before printing the scrapped list |
| 10 | + console.clear(); |
| 11 | + |
12 | 12 | // Loop through all table rows. |
13 | | - for (i = 0; i < rows.length; i++) { |
| 13 | + for (let i = 0; i < rows.length; i++) { |
| 14 | + const row = rows[i]; |
| 15 | + |
14 | 16 | // Extract actor name from the row. The actor row are in span element with an attribute named itemprop |
15 | 17 | // The actor name is in the inner HTML. |
16 | | - let actor = rows[i].querySelector("span[itemprop]").innerHTML; |
17 | | - |
| 18 | + let actor = row.querySelector("[itemprop=actor] a span").innerHTML; |
| 19 | + |
18 | 20 | // Select the character row. The character row is a td element with CC class named character. |
19 | | - let characterTag = rows[i].querySelector("td.character"); |
| 21 | + let characterTag = row.querySelector("td.character"); |
20 | 22 | let character; |
21 | | - |
| 23 | + |
22 | 24 | // The character is either in an anchor tag or without the anchor. |
23 | 25 | let a = characterTag.querySelector("a"); |
24 | | - |
25 | | - if (a != null) |
26 | | - { |
| 26 | + |
| 27 | + if (a != null) { |
27 | 28 | // If the anchor element is present, the character name is within the anchor tag. |
28 | 29 | character = a.innerHTML; |
29 | | - } |
30 | | - else |
31 | | - { |
| 30 | + } else { |
32 | 31 | // Otherwise the character is directly within the character tag. |
33 | 32 | character = characterTag.innerHTML; |
34 | 33 | } |
35 | 34 |
|
36 | | - // Clear console before printing the scrapped list |
37 | | - console.clear(); |
38 | | - |
39 | 35 | // Print the Actor and character names |
40 | 36 | console.log("Actor: " + actor.trim() + ", Character: " + character.trim()); |
41 | 37 | } |
42 | | - |
43 | 38 | }); |
44 | | - |
0 commit comments