-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
166 lines (140 loc) · 3.93 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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 findSoundCloundTab(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);
}
else {
findSuitableTab(tabs, function (tab) {
return callback(tab);
});
}
});
}
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 executeCommandOnTab(tab, command, backoff = 100) {
if (tab.status === 'loading') {
var delayed = setTimeout(() => {
executeCommandOnTab(tab, command, backoff * 2)
clearTimeout(delayed)
}, backoff)
} else {
runCommand(tab, command)
}
}
function runCommand(tab, command) {
chrome.tabs.executeScript(tab.id, prepareScript(command), () => {
emitPageStatus();
});
}
function runCommandInEnv(tab, command, env) {
chrome.tabs.executeScript(tab.id, {
code: env
}, function () {
runCommand(tab, command)
})
}
function moveTimeline(frac) {
findSoundCloundTab(function (playingTab) {
runCommandInEnv(playingTab, 'setTime', `var config = { frac: ${frac} }`)
})
}
function executeCommand(command) {
var parsedCommand = parseCommand(command);
findSoundCloundTab(function (playingTab) {
if (playingTab) {
executeCommandOnTab(playingTab, parsedCommand)
} else if (lastActiveTab) {
var createProperties = {
url: lastActiveTab.url,
active: true
};
chrome.tabs.create(createProperties);
}
else {
var createProperties = {
url: 'https://soundcloud.com/stream',
active: true
};
chrome.tabs.create(createProperties);
}
});
}
function emitPageStatus() {
findSoundCloundTab(function (tab) {
if (tab) {
getTabInfo(tab.id, function (tabInfo) {
soundCloudStatus.playing = tabInfo.playing;
soundCloudStatus.repeating = tabInfo.repeating;
soundCloudStatus.muted = tabInfo.muted;
soundCloudStatus.title = tabInfo.title;
soundCloudStatus.liking = tabInfo.liking;
soundCloudStatus.image = tabInfo.image;
soundCloudStatus.artist = tabInfo.artist;
soundCloudStatus.songLength = tabInfo.songLength;
soundCloudStatus.songCurrentTime = tabInfo.songCurrentTime;
chrome.runtime.sendMessage(soundCloudStatus);
});
}
});
}
function openShortcutsView() {
var createProperties = {
url: 'chrome://extensions/shortcuts',
active: true
};
chrome.tabs.create(createProperties, function (newTab) {
executeCommandOnTab(newTab, parsedCommand)
});
}
// Add listener for keyboard shortcuts
chrome.commands.onCommand.addListener(function (command) {
executeCommand(command);
});