/
playAudio.js
51 lines (40 loc) · 1.64 KB
/
playAudio.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
// Repeatedly searches for the target element according to the array of selectors
// Parameters:
// parent: element object, represents the element a search starts
// selectors: array of strings, represents CSS selectors in order to get the target element
function intervalForAudio(parent, selectors) {
// Base case: If all selectors used, click the element and return
if (selectors.length === 0) {
parent.click();
return;
}
// Store setInterval to a variable
// so that the interval can be stopped by 'clearInterval()'
let checkInterval = setInterval(() => {
// If the passed time exceeded the maxSeconds, stop searching.
if (new Date().getTime() - start.getTime() > maxSeconds) {
clearInterval(checkInterval);
// If the passed time didn't exceed the maxSeconds
} else {
let el = parent.querySelector(selectors[0]);
// If element found
if (el) {
clearInterval(checkInterval);
// Update the variables for the next search
selectors.shift();
parent = el;
// Begin the next search using updated selectors and parent
intervalForAudio(parent, selectors);
}
}
}, intervalSeconds);
}
// Miliseconds the next search occurs after a search failed.
let intervalSeconds = 50;
// Maximum miliseconds the searching interval stops after.
let maxSeconds = 20000;
// Search for the audio element then click it
let start = new Date();
chrome.storage.local.get('audioSelectors', stor => {
intervalForAudio(document, stor.audioSelectors);
});