-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
83 lines (66 loc) · 2.1 KB
/
popup.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
var spotifyUrlRegExp = "https://open.spotify.com/*";
var muteSwitch = document.getElementById('muteSwitch');
var pauseButton = document.getElementById('pauseButton');
var playButton = document.getElementById('playButton');
var backButton = document.getElementById('backButton');
var forwardButton = document.getElementById('forwardButton');
muteSwitch.checked = localStorage.getItem("activateMutedFunction") === "true";
localStorage.setItem('activateMutedFunction', muteSwitch.checked);
muteSwitch.addEventListener('click', function() {
localStorage.setItem('activateMutedFunction', muteSwitch.checked);
sendStateChange();
}, false);
function isPlayed(){
pauseButton.className = 'show';
playButton.className = 'hide';
localStorage.setItem('isPlaying', "true");
}
function isPaused(){
pauseButton.className = 'hide';
playButton.className = 'show';
localStorage.setItem('isPlaying', "false");
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function playerDecision(){
chrome.tabs.query({url: spotifyUrlRegExp}, function(tabs) {
console.log(tabs[0].title);
if(tabs[0].title.includes("Browse - Featured")){
isPaused();
}else{
isPlayed();
}
});
}
function responseMessages(request, sender, sendResponse) {
if( request.action === "ManualPlayerClick"){
sleep(1000);
playerDecision();
}
}
chrome.runtime.onMessage.addListener(responseMessages);
playerDecision();
pauseButton.addEventListener('click', function() {
isPaused();
sendAction2Spotify("pausePlayer");
}, false);
playButton.addEventListener('click', function() {
isPlayed();
sendAction2Spotify("playPlayer");
}, false);
backButton.addEventListener('click', function() {
sendAction2Spotify("backPlayer");
}, false);
forwardButton.addEventListener('click', function() {
isPlayed();
sendAction2Spotify("forwardPlayer");
}, false);
function sendAction2Spotify(playerAction){
chrome.tabs.query({url: spotifyUrlRegExp}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {action: playerAction}, null);
});
}
function sendStateChange(){
chrome.runtime.sendMessage({action: "StateChange"},null);
}