-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
148 lines (133 loc) · 3 KB
/
background.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
var player = (function() {
var RECITER_KEY = 'prev.reciter.id';
var rootUrl = 'http://download.quranicaudio.com/quran/';
var isSet = false;
var currReciter = store.get(RECITER_KEY);
console.log(store.get(RECITER_KEY));
var currSurahNum;
var onChange = [];
var onLoadstart = [];
var audioElt = new Audio();
audioElt.setAttribute('autoplay', 'autoplay');
chrome.runtime.onConnect.addListener(function(port) {
port.onDisconnect.addListener(function() {
onChange = [];
onLoadstart = [];
});
});
var callCallbacks = function(callbacks) {
for (var i = 0; i < callbacks.length; ++i) {
callbacks[i]();
}
};
var padZeroes = function(num) {
var s = "00" + num;
return s.substr(s.length-3);
};
var generateUrl = function(surahNum, reciterId) {
var surahCode = padZeroes(surahNum);
return rootUrl + reciterId + '/' + surahCode + '.mp3';
};
var sourceCallback = function() {
isSet = true;
callCallbacks(onChange);
audioElt.removeEventListener('playing', sourceCallback);
};
var setSource = function(surahNum, reciter) {
stop();
currReciter = reciter;
currSurahNum = surahNum;
var url = generateUrl(surahNum, reciter.path);
audioElt.src = url;
callCallbacks(onLoadstart);
audioElt.addEventListener('playing', sourceCallback);
};
var play = function() {
if (isSet) {
audioElt.play();
callCallbacks(onChange);
}
};
var pause = function() {
if (isSet) {
audioElt.pause();
callCallbacks(onChange);
}
};
var stop = function() {
isSet = false;
audioElt.pause();
audioElt.src = '';
};
var next = function() {
if (isSet && currSurahNum < 115) {
setSource(currSurahNum + 1, currReciter);
}
};
var prev = function() {
if (isSet && currSurahNum > 1) {
setSource(currSurahNum - 1, currReciter);
}
};
var setReciter = function(reciter) {
var paused = audioElt.paused;
store.set(RECITER_KEY, reciter);
console.log(store.get(RECITER_KEY));
if (isSet) {
setSource(currSurahNum, reciter);
if (paused) {
pause();
}
}
};
audioElt.addEventListener('ended', function() {
next();
});
return {
play: play,
pause: pause,
setSource: setSource,
getDuration: function() {
return Math.round(audioElt.duration);
},
getCurrentTime: function() {
return Math.round(audioElt.currentTime);
},
getSurahNum: function() {
return currSurahNum;
},
getReciter: function() {
return currReciter;
},
isPlaying: function() {
return !audioElt.paused;
},
isPaused: function() {
return audioElt.paused;
},
isInit: function() {
return isSet;
},
scrub: function(time) {
audioElt.currentTime = time;
},
onChange: function(callback) {
onChange.push(callback);
},
onLoadstart: function(callback) {
onLoadstart.push(callback);
},
getBuffered: function() {
return audioElt.buffered;
},
getPlayedPercentage: function() {
if (isSet) {
return Math.round((audioElt.currentTime/audioElt.duration) * 100);
}
return 0;
},
setReciter: setReciter,
next: next,
prev: prev
};
}());