-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent_script.js
102 lines (79 loc) · 2.27 KB
/
content_script.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
102
var u = new SpeechSynthesisUtterance();
u.lang = 'en-US';
u.rate = 1.2;
/*
document.body.onmouseup = ()=>{
speakList(getSimilarToSelected());
}
*/
function speakList(list){
console.log(u)
var index = 0;
u.onend = (event)=>{
if(++index<list.length)
speak_text(list[index])
}
var speak_text = (text)=>{
u.text = text;
speechSynthesis.speak(u);
}
speak_text(list[0])
}
function speak_text(text){
u.text = text;
speechSynthesis.speak(u);
}
function getSelectedNode()
{
if (document.selection)
return document.selection.createRange().parentElement();
else
{
var selection = window.getSelection();
if (selection.rangeCount > 0)
return selection.getRangeAt(0).startContainer.parentNode;
}
}
function getParentAndClassList(node){
var list = [];
var classList = [node.classList];
var parentList = [node.tagName];
var currentNode = node;
while (currentNode.parentElement.tagName != "BODY")
{
currentNode = currentNode.parentElement;
parentList.push(currentNode.tagName)
classList.push(currentNode.classList)
}
list.push(parentList)
list.push(classList)
return list;
}
function ancestorListToNodes(list){
var children = null;
var currentNode = document.body;
var recurseSearch = (node,i)=>{
if (i == -1)
return [node.innerText];
var result = [];
if (node.children)
for (var c=0;c<node.children.length;c++){
if (/*node.children[c].classList == list[1][i]
&& */node.children[c].tagName == list[0][i])
{
var returnVal = recurseSearch(node.children[c],i-1)
if (returnVal.length)
result.push(...returnVal);
}
}
return result;
}
return recurseSearch(currentNode,list[0].length-1);
}
function getSimilarToSelected(){
var node = getSelectedNode()
var parent_list = getParentAndClassList(node);
var result = ancestorListToNodes(parent_list)
console.log(result)
return result
}