forked from nazrhom/soundcloudController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
118 lines (97 loc) · 2.72 KB
/
background.js
File metadata and controls
118 lines (97 loc) · 2.72 KB
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
var lastActiveTab;
var queryInfo = {
url: '*://soundcloud.com/*'
};
var soundCloudStatus = {};
function prepareScript (scriptFile) {
var scriptDetail = {};
scriptDetail.file = '/js/' + scriptFile + '.js';
return scriptDetail;
}
function isTabPlaying (tab, callback) {
getTabInfo(tab.id, function(tabInfo) {
return callback(tabInfo.playing);
});
}
function getTabInfo (tabId, callback) {
chrome.tabs.executeScript(tabId, prepareScript('getTabInfo'), function(results) {
return callback(results[0]);
});
}
function findSuitableTab (tabs, callback) {
return async.detect(tabs, isTabPlaying, function(playingTab) {
if (playingTab) {
//update playing tab
lastActiveTab = playingTab;
return callback(playingTab);
} else {
// if no tab is playing return lastActiveTab
if (lastActiveTab) {
return callback(lastActiveTab);
} else {
//if all else fails just return first tab found
var pausedTab = tabs[0];
// save as lastplaying tab
lastActiveTab = pausedTab;
return callback(pausedTab);
}
}
});
}
function findSoundCloundTabId (callback) {
chrome.tabs.query(queryInfo, function(tabs) {
if (tabs.length === 0) return callback();
if (tabs.length === 1) {
var tab = tabs[0];
lastActiveTab = tab;
return callback(tab.id);
}
else {
findSuitableTab(tabs, function(tab) {
return callback(tab.id);
});
}
});
}
function parseCommand(command) {
return command.split('-').map(function(word, index) {
if (index !== 0) return word.charAt(0).toUpperCase().concat(word.slice(1));
return word;
}).join('');
}
function executeCommand(command) {
var parsedCommand = parseCommand(command);
findSoundCloundTabId(function(playingTabId) {
if (playingTabId) {
chrome.tabs.executeScript(playingTabId, prepareScript(parsedCommand), function() {
emitPageStatus();
});
} else if (lastActiveTab) {
var createProperties = {
url: lastActiveTab.url,
active: false
};
chrome.tabs.create(createProperties, function(newTab) {
});
}
else {
console.log('Not tab or lastActiveTab found :(');
}
});
}
function emitPageStatus () {
findSoundCloundTabId(function(tabId) {
if (tabId) {
getTabInfo(tabId, function(tabInfo) {
soundCloudStatus.playing = tabInfo.playing;
soundCloudStatus.repeating = tabInfo.repeating;
soundCloudStatus.muted = tabInfo.muted;
soundCloudStatus.title = tabInfo.title;
chrome.runtime.sendMessage(soundCloudStatus);
});
}
});
}
chrome.commands.onCommand.addListener(function(command) {
executeCommand(command);
});