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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
var wsc, injectInterval;
function init() {
// receive messages from webpage
chrome.runtime.onMessageExternal.addListener(function(msg, sender, sendResponse) {
if (msg.cmd == "injection_result") {
var obj = Injections.get(msg.id);
if (obj) {
obj.addResponse(sender.tab.id, msg.data);
}
}
if (msg.cmd == "to_app") {
// log('to_app received', msg.data);
wsc.send(msg.data);
}
});
// connect to the app
wsc = new WSClient("wss://vkpc-local.ch1p.com:56130", "signaling-protocol", {
onopen: function() {
Controller.clear();
this.send({command: 'setBrowser'});
},
onmessage: function(cmd) {
var json = JSON.parse(cmd);
switch (json.command) {
case 'set_sid':
Controller.sid = json.data;
break;
case 'set_playlist_id':
Controller.playlistId = json.data;
break;
case 'vkpc':
inject(json.data);
break;
}
// executeCommand(msg);
},
onerror: function() {
this.reconnect();
},
onclose: function() {
this.reconnect();
}
});
wsc.connect();
injectInterval = setInterval(function() {
inject('afterInjection');
}, 2000);
}
function sendClear() {
wsc.send({command: 'clearPlaylist', data: null});
}
function inject(command, callback) {
var injId = Injections.getNextId();
var data = {
extid: getExtensionId(),
injid: injId,
sid: Controller.sid,
command: command
};
var code_inj = "var el = document.createElement('script');" +
"el.src = chrome.extension.getURL('vkpc.js');" +
"document.body.appendChild(el);" +
"var el1 = document.createElement('script');" +
"el1.textContent = 'window.__vkpc_data = "+JSON.stringify(data)+"';" +
"document.body.appendChild(el1)";
var okTab_nowPlaying, okTab_playlistFound, okTab_lsSource, okTab_recentlyPlayed, okTab_havePlaylist,
activeTab, lastTab, outdatedTabs = [], tabsWithPlayingMusic = []/*, tabPlaylistIds = {}*/;
var lsSourceId, appPlaylistFound = false;
var injResponses, injResults;
function getCode(code) {
return "var el = document.createElement('script');" +
"el.textContent = '"+code.replace(/'/g, "\\'")+"';" +
"document.body.appendChild(el)";
}
function onDone(step) {
var results = injResponses.results;
var execCommand = getCode("VKPC.executeCommand('"+command+"', "+Controller.playlistId+")");
if (command == 'afterInjection') {
//log('[afterInjection onDone] results.length='+results.length);
for (var i = 0; i < results.length; i++) {
var data = results[i].data, tab = results[i].tab;
// tabPlaylistIds[tab] = data.playlistId;
if (data.playlistId != 0 && data.playlistId == Controller.playlistId) {
appPlaylistFound = true;
}
if (data.havePlaylist && data.playlistId != 0 && data.playlistId != Controller.playlistId) {
outdatedTabs.push(tab);
}
if (data.havePlaylist) {
okTab_havePlaylist = tab;
}
if (data.isPlaying) {
okTab_nowPlaying = tab;
}
}
if (!appPlaylistFound) {
var okTab = okTab_nowPlaying || okTab_havePlaylist;
if (okTab !== undefined) {
chrome.tabs.executeScript(okTab, {code: execCommand});
} else if (!appPlaylistFound) {
sendClear();
}
}
for (var i = 0; i < outdatedTabs.length; i++) {
chrome.tabs.executeScript(outdatedTabs[i], {code: getCode('VKPC.clearPlaylist(true, "as")')});
}
} else {
for (var i = 0; i < results.length; i++) {
var data = results[i].data;
if (!lsSourceId && data.lsSourceId) {
lsSourceId = data.lsSourceId;
break;
}
}
for (var i = 0; i < results.length; i++) {
var data = results[i].data, tab = results[i].tab;
if (data.playlistId == Controller.playlistId) {
okTab_playlistFound = tab;
}
if (data.havePlayer && (data.isPlaying || typeof data.trackId == 'string')) {
okTab_recentlyPlayed = tab;
}
if (data.isPlaying) {
okTab_nowPlaying = tab;
}
if (lsSourceId == data.tabId) {
okTab_lsSource = tab;
}
lastTab = tab;
}
var check = [okTab_nowPlaying, okTab_lsSource, okTab_recentlyPlayed, okTab_recentlyPlayed, okTab_havePlaylist, activeTab, lastTab];
//log('check[] =', check);
for (var i = 0; i < check.length; i++) {
if (check[i] !== undefined) {
chrome.tabs.executeScript(check[i], {code: execCommand});
break;
}
}
}
injResponses.unregister();
callback && callback();
}
getVKTabs(function(tabs) {
if (!tabs.length) {
sendClear();
return;
}
injResponses = new InjectionResponses(injId, tabs.length, onDone);
for (var i = 0; i < tabs.length; i++) {
if (tabs[i].active) {
activeTab = tabs[i].id;
}
chrome.tabs.executeScript(tabs[i].id, {
code: code_inj
});
}
});
};
var Controller = {
sid: 0,
playlistId: 0,
clear: function() {
this.sid = 0;
this.playlistId = 0;
}
};
DOMContentLoaded(init);
|