-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
58 lines (47 loc) · 2.61 KB
/
Copy pathmain.js
File metadata and controls
58 lines (47 loc) · 2.61 KB
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
// store under the 'text' variable an array-like collection - each element of this collection is each HTML element that makes up our current web page
const text = document.querySelectorAll(
'h1, h2, h3, h4, h6, p, li, td, caption, span, a'
);
// loop over each HTML element on the page
for (let i = 0; i < text.length; i++) {
// store in 'arrOfWords' variable an array of substrings, or in other words, an array of each word contained text[i] delimited by spaces
let iText = text[i].innerHTML;
let arrOfWords = iText.split(' ');
// // pass arrOfWords into removeHTML:
// const removeHTML = function(input) {
// // check in an if conditional to make sure the current word is not an HTML tag
// // if current word starts with '<', skip it, and keep skipping subsequent words until we reach the closing HTML tag
// // detect array elements that contain opening HTML tags (i.e. starts '<') and array elements that contain closing HTML tags (i.e. '</')
// // slice up to the opening element, and concat this with the slice
// for (k = 0; k < input.length; k++) {
// if (input[k][0] === '<') {
// }
// }
// }
// iterate over arrOfWords and 'bionicize' each element of arrOfWords- mutate each of current's elements, adding <b> tags to the first half of each word
for (let j = 0; j < arrOfWords.length; j++) {
// add handling of edge case of 1-character strings
// store under a variable all the kinds of HTML elements we don't want: img, a, link
const toRemove = document.querySelectorAll('a','img','link')
// use removeChild DOM manipulation to remove these elements from
if(arrOfWords[j][0] !== "<" && !isLinkOrImage(text[i])) {
let bionicWord = Math.floor(arrOfWords[j].length / 2) + 1;
let bionic =
'<b>' +
arrOfWords[j].slice(0, bionicWord) +
'</b>' +
arrOfWords[j].slice(bionicWord);
// console.log(bionic);
arrOfWords[j] = bionic;
}
}
// construct a new string by doing the following:
// to an empty string, append each element of arrOfWords, concatting a space in between each one
text[i].innerHTML = arrOfWords.join(' ')
}
//create a function with the getElementsByTagName() by passing in an ellement from arrOfWords and returning all elements with the ta tag name
//with "img" or "embed" (make case insensitive)
//after returning those elements then we add in line 30 to arrOfWords[0][j] !== "<" && !isLinkOrImage(text[i])
function isLinkOrImage (element){
return element.tagName.toLowerCase() === 'img' || element.tagName.toLowerCase() === 'link' || element.tagName.toLowerCase() === 'a'|| element.tagName.toLowerCase() === 'href';
}