-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContentScript.js
78 lines (75 loc) · 2.36 KB
/
ContentScript.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
//Config Start
let minaudio = 0.55; // Go fast if audio is x or less
let threshold = 3; // Checks to verify
let fastplayback = 2;
let smoothingTimeConstant = 0.9;
let normalplayback = 1;
//Config END
chrome.runtime.onMessage.addListener( // Detect URL Changes
function(request, sender, sendResponse) {
if (request.message === 'update') { // Message from background script
AutoSkip();
}
if (request.message === 'changes') { // Message from background script
minaudio = request.minaudio;
threshold = request.threshold;
fastplayback = request.fastplayback;
smoothingTimeConstant = request.smoothingTimeConstant;
normalplayback = request.normalplayback;
}
if(request.message === 'settings'){
sendResponse({
minaudio: minaudio,
threshold: threshold,
fastplayback: fastplayback,
smoothingTimeConstant: smoothingTimeConstant,
normalplayback: normalplayback
});
}
});
// Main function
AutoSkip();
function RegisterAudio(){
window.context = new AudioContext();
video = context.createMediaElementSource(v);
analyser = context.createAnalyser(); //we create an analyser
analyser.smoothingTimeConstant = smoothingTimeConstant;
analyser.fftSize = 512; //the total samples are half the fft size.
video.connect(analyser);
analyser.connect(context.destination);
}
function CheckAudio(){
var array = new Uint8Array(analyser.fftSize);
analyser.getByteTimeDomainData(array);
var average = 0;
var max = 0;
for (i = 0; i < array.length; i++) {
a = Math.abs(array[i] - 128);
average += a;
max = Math.max(max, a);
}
average /= array.length;
return average;
}
function AutoSkip() {
v = document.querySelector('video'); // Video player
if(!v) return;
RegisterAudio();
v.ontimeupdate = function () { // If exists add event to run on the videos "ontimeupdate"
ASCheck();
};
}
counter = 1;
function ASCheck() { // Video skipping
if (CheckAudio() <= minaudio) {
if(counter == threshold){
v.playbackRate = fastplayback;
counter = normalplayback;
}else{
counter += 1;
}
}else{
v.playbackRate = normalplayback;
counter = 1;
}
}