This repository has been archived by the owner on Dec 7, 2020. It is now read-only.
/
base-connector.js
709 lines (656 loc) · 23.3 KB
/
base-connector.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
'use strict';
import Utils from 'content/utils';
import MetadataFilter from 'content/filter';
import _ from 'underscore';
import browser from 'webextension-polyfill';
const ids = new WeakMap();
const lastValue = new WeakMap();
const lastCallTime = new WeakMap();
const propertyNames = [
'controlsInfo',
'playbackStatus',
'trackInfo',
'volume',
'currentTime',
];
const throttleInterval = { currentTime: 2000 };
const port = new WeakMap();
const prefix = new WeakMap();
const displayedWarnings = new WeakMap();
class BaseConnector {
/**
* @constructor
* Initializes base for all connectors
*/
constructor() {
this.name = 'Web Media Controller';
this.playButtonSelector = null;
this.stopButtonSelector = null;
this.prevButtonSelector = null;
this.nextButtonSelector = null;
this.artistSelector = null;
this.artistsSelector = null;
this.albumSelector = null;
this.titleSelector = null;
this.artSelector = null;
this.progressSelector = null;
this.currentTimeSelector = null;
this.lengthSelector = null;
this.volumeSelector = null;
this.mediaSelector = null;
this.pageGetters = new Set();
this.pageSetters = new Set();
this.pageActions = new Set();
this.metadataFilter = new MetadataFilter({});
displayedWarnings.set(this, new Set());
ids.set(this, new Map());
lastValue.set(this, new Map([
['playbackStatus', 'stopped'],
]));
lastCallTime.set(this, new Map());
prefix.set(this, '/me/f1u77y/web_media_controller');
port.set(this, browser.runtime.connect());
port.get(this).onMessage.addListener((message) => {
switch (message.command) {
case 'play':
this.play();
break;
case 'pause':
this.pause();
break;
case 'playPause':
this.playPause();
break;
case 'stop':
this.stop();
break;
case 'previous':
this.previous();
break;
case 'next':
this.next();
break;
case 'seek':
this.seek(message.argument);
break;
case 'setPosition':
this.position = message.argument;
break;
case 'volume':
this.volume = message.argument;
break;
case 'reload':
for (let name of propertyNames) {
if (lastValue.get(this).has(name)) {
this.sendProperty(name, lastValue.get(this).get(name));
}
}
this.sendProperty('name', this.name);
break;
}
return false;
});
}
/**
* Add parameter to the connector object prefix. Should be called exactly
* one time!
* @param {string} prefix - Connector's prefix. In most cases should be
* derived from domain
*/
set prefix(addedPrefix) {
prefix.set(this, `${prefix.get(this)}${addedPrefix}`);
}
/**
* Send updated property to the background. Intended for internal usage
* @param {string} name - Property name
* @param {object} value - New property value
*/
sendProperty(name, value) {
let message = { name, value };
if (message.name === 'trackInfo') {
// XXX It surely needs to be deep clone for the common case but
// underscore.js does not have it
message.value = _(this.metadataFilter.filter(message.value)).defaults({
artist: '',
album: '',
title: '',
url: '',
length: 0,
artUrl: '',
trackId: '',
});
}
port.get(this).postMessage(message);
}
/**
* Get property from the injected script
* @param {string} property - Property name
* @returns {Promise} resolved with fetched value or rejected with timeout
*/
getFromPage(property) {
return new Promise((resolve, reject) => {
const currentId = ids.get(this).get(property) || 0;
ids.get(this).set(property, currentId + 1);
const tid = setTimeout(() => {
window.removeEventListener('message', handleResponse);
reject(`Timeout: ${property}`);
}, 2000);
function handleResponse({data}) {
if (data.sender !== 'wmc-page-getter' ||
data.property !== property ||
data.id !== currentId )
{
return;
}
clearTimeout(tid);
window.removeEventListener('message', handleResponse);
resolve(data.response);
}
window.addEventListener('message', handleResponse);
window.postMessage({
sender: 'wmc-connector-getter',
property,
id: currentId,
}, '*');
});
}
/**
* Send a command to injected script
* @param {string} command - Command name
* @param {object} [argument=null] - Single argument. Must be JSON-ifiable
*/
sendToPage(command, argument = null) {
window.postMessage({
sender: 'wmc-connector-command',
command,
argument,
}, '*');
}
/**
* Set a property using injected script
* @param {string} property - Property name
* @param {object} [argument=null] - Single argument. Must be JSON-ifiable
*/
setOnPage(property, argument) {{
window.postMessage({
sender: 'wmc-connector-setter',
property,
argument,
}, '*');
}}
/**
* Listen events from injected script and call `onStateChanged` when event fires.
* Use `propertyNames` param in injected script for explicitly stating which
* properties have changed
*/
listenPage() {
this.onStateChanged();
window.addEventListener('message', ({data}) => {
if (data.sender !== 'wmc-page-notifier') return;
this.onStateChanged(data.propertyNames);
});
}
/**
* Inject scripts from the specified URLs directly into page. Use it for getting
* info from page objects or calling its' control methods
* @param {string} urls - URLs of the scripts. Should be mentioned in
* web_accessible_resources manifest entry
* @returns {Promise} fullfilled when scripts is loaded. Use it for listening
* for page only when scripts are injected
*/
injectScripts(...urls) {
return new Promise((resolve) => {
let injectedNumber = 0;
for (let url of urls) {
const script = document.createElement('script');
script.src = browser.extension.getURL(url);
script.addEventListener('load', () => {
++injectedNumber;
if (injectedNumber === urls.length) {
resolve();
}
});
(document.head || document.documentElement).appendChild(script);
}
});
}
/**
* Calls `onStateChanged` when the `element` and/or its underlying subtree
* generates mutations.
* Note that it's not suitable for watching events generated by those elements
* @param {Node} element - The obseved element
*/
observe(element) {
const stateChangeObserver = new MutationObserver(() => {
this.onStateChanged();
});
stateChangeObserver.observe(element, {
childList: true,
subtree: true,
attributes: true,
characterData: true,
});
}
singleWarn(message) {
if (!displayedWarnings.get(this).has(message)) {
displayedWarnings.get(this).add(message);
console.warn(message);
}
}
/**
* Start media playback if not yet. It's intended to be overriden.
*/
play() {
if (this.pageActions.has('play')) {
this.sendToPage('play');
} else if (this.mediaSelector) {
Utils.query(this.mediaSelector).then(media => media.play());
} else {
this.playbackStatus.then(status => {
if (status !== 'playing') {
this.playPause();
}
});
}
}
/**
* Pause media playback if not yet. It's intended to be overriden.
*/
pause() {
if (this.pageActions.has('pause')) {
this.sendToPage('pause');
} else if (this.mediaSelector) {
Utils.query(this.mediaSelector).then(media => media.pause());
} else {
this.playbackStatus.then(status => {
if (status === 'playing') {
this.playPause();
}
});
}
}
/**
* Toggle media playback. It's intended to be overriden.
*/
playPause() {
if (this.pageActions.has('playPause')) {
this.sendToPage('playPause');
} else if (this.playButtonSelector) {
Utils.queryClick(this.playButtonSelector);
} else {
this.playbackStatus.then((status) => {
if (status === 'playing') {
this.pause();
} else {
this.play();
}
});
}
}
/**
* Stop media playback. It's intended to be overriden.
* When this method has no effect, `canStop` property should be false
*/
stop() {
if (this.pageActions.has('stop')) {
this.sendToPage('stop');
} else if (this.stopButtonSelector) {
Utils.queryClick(this.stopButtonSelector);
} else {
this.singleWarn('Connector.stop not implemented');
}
}
/**
* Go to the previous track. It's intended to be overriden.
* When this method has no effect, `canGoPrevious` property should be false
*/
previous() {
if (this.pageActions.has('previous')) {
this.sendToPage('previous');
} else if (this.prevButtonSelector) {
Utils.queryClick(this.prevButtonSelector);
} else {
this.singleWarn('Connector.previous not implemented');
}
}
/**
* Go to the next track. It's intended to be overriden.
* When this method has no effect, `canGoNext` property should be false
*/
next() {
if (this.pageActions.has('next')) {
this.sendToPage('next');
} else if (this.nextButtonSelector) {
Utils.queryClick(this.nextButtonSelector);
} else {
this.singleWarn('Connector.next not implemented');
}
}
/**
* Seek for the given offset. Negative value means seek backwards.
* @param {Number} offset - Offset in milliseconds
*/
seek(offset) {
if (this.pageActions.has('seek')) {
this.sendToPage('seek', offset);
} else if (this.mediaSelector) {
this.mediaSelector.currentTime += offset / 1000;
} else {
this.singleWarn('Connector.seek not implemented');
}
}
/**
* Internal method for setting position. Doesn't perform anything if
* given trackId does not match current one. If you want to override
* this behaviour, you should return `null` from `get uniqueId()` instead
* @param {string} trackId - ID of the track which was playing in the
* beginning of the action
* @param {Number} position - Position in the song in milliseconds.
* Should be in [0; length]
*/
set position({ trackId, position }) {
this.trackId.then(curTrackId => {
if (trackId !== curTrackId) return;
this.currentTime = position;
});
}
/**
* Set position for the media playback. It's intended to be overriden
* @param {Number} currentTime - Position in the song in milliseconds.
* Should be in [0; length]
*/
set currentTime(currentTime) {
if (this.pageSetters.has('currentTime')) {
this.setOnPage('currentTime', currentTime);
} else if (this.mediaSelector) {
Utils.query(this.mediaSelector)
.then(media => media.currentTime = currentTime / 1000);
} else {
this.singleWarn('Connector.set currentTime not implemented');
}
}
/**
* Set player volume. It's intended to be overriden
* @param {Number} volume - New volume. Should be in [0; 1]
*/
set volume(volume) {
if (this.pageSetters.has('volume')) {
this.setOnPage('volume', volume);
} else if (this.mediaSelector) {
Utils.query(this.mediaSelector)
.then(media => media.volume = volume);
} else {
this.singleWarn('Connector.set volume not implemented');
}
}
/**
* Get current playback status. It *must* be overriden for proper work.
* @returns {Promise} fullfilled with playback status
* Should be one of 'playing', 'paused' or 'stopped'
*/
get playbackStatus() {
if (this.pageGetters.has('playbackStatus')) {
return this.getFromPage('playbackStatus');
} else if (this.mediaSelector) {
return Utils.query(this.mediaSelector)
.then(media => media.paused ? 'paused' : 'playing');
} else {
this.singleWarn('Connector.get playbackStatus not implemented');
return Promise.resolve(undefined);
}
}
/**
* Get current position in milliseconds. It's intended to be overriden.
* @returns {Promise} fullfilled with position
* Should be in [0; length]
*/
get currentTime() {
if (this.pageGetters.has('currentTime')) {
return this.getFromPage('currentTime');
} else if (this.currentTimeSelector) {
return Utils.query(this.currentTimeSelector).then(node => {
let text = node.textContent;
return Math.floor(Utils.parseCurrentTime(text, { useFirstValue: true }) * 1000);
});
} else if (this.progressSelector) {
return Utils.query(this.progressSelector).then(node => {
if (node.hasAttribute('aria-valuenow')) {
let result = node.getAttribute('aria-valuenow');
result = parseFloat(result);
result *= (this.timeCoefficient || 1);
return Math.floor(result);
} else {
let text = node.textContent;
return Math.floor(Utils.parseCurrentTime(text) * 1000);
}
});
} else if (this.mediaSelector) {
// HTMLMediaElement.currentTime is always in seconds
return Utils.query(this.mediaSelector)
.then(node => Math.floor(node.currentTime * 1000));
} else {
this.singleWarn('Connector.get currentTime not implemented');
return undefined;
}
}
/**
* Get current volume.
* @returns {Promise} which fullfills with current volume
* Should be in [0; 1]
*/
get volume() {
if (this.pageGetters.has('volume')) {
return this.getFromPage('volume');
} else if (this.volumeSelector) {
return Utils.query(this.volumeSelector).then(node => {
let result = node.getAttribute('aria-valuenow');
result = parseFloat(result);
let max = node.getAttribute('aria-valuemax');
max = parseFloat(max);
return result / max;
});
} else if (this.mediaSelector) {
return Utils.query(this.mediaSelector)
.then(media => media.volume);
} else {
this.singleWarn('Connector.get volume not implemented');
return Promise.resolve(undefined);
}
}
/**
* Get current control abilities. Their names are self-documented
* @returns {Promise} which fullfills with the object.
*/
get controlsInfo() {
return Promise.resolve({
canGoNext: true,
canGoPrevious: true,
canPlay: true,
canPause: true,
canSeek: true,
canControl: true,
});
}
/**
* Get length of the current track. Should return undefined if
* it does not make sense (e.g. stream). This method is intended to be overriden.
* @returns {Number} length in milliseconds
*/
get length() {
if (this.pageGetters.has('length')) {
return this.getFromPage('length');
} else if (this.lengthSelector) {
return Utils.query(this.lengthSelector).then(node => {
let text = node.textContent;
return Utils.parseLength(text, { useFirstValue: true }) * 1000;
});
} else if (this.progressSelector) {
return Utils.query(this.progressSelector).then(node => {
if (node.hasAttribute('aria-valuemax')) {
let result = node.getAttribute('aria-valuemax');
result = parseFloat(result);
result *= (this.timeCoefficient || 1);
return result;
} else {
let text = node.textContent;
return Utils.parseLength(text) * 1000;
}
});
} else if (this.mediaSelector) {
// HTMLMediaElement.duration is always in seconds
return Utils.query(this.mediaSelector)
.then(node => node.duration * 1000);
} else {
this.singleWarn('Connector.get length not implemented');
return Promise.resolve(undefined);
}
}
/**
* Get artist(s) of the current track. This method is intended to be overridden.
* @returns {Promise} fullfilled with artist or Array of artists
*/
get artist() {
if (this.pageGetters.has('artist')) {
return this.getFromPage('artist');
} else if (this.artistsSelector) {
let artists = [];
for (let node of document.querySelectorAll(this.artistsSelector)) {
artists.push(node.textContent.trim());
}
return Promise.resolve(artists);
} else if (this.artistSelector) {
return Utils.query(this.artistSelector)
.then(node => node.textContent.trim());
} else {
this.singleWarn('Connector.get artist not implemented');
return Promise.resolve(undefined);
}
}
/**
* Get album of the current track. This method is intended to be overriden.
* @returns {Promise} fullfilled with album
*/
get album() {
if (this.pageGetters.has('album')) {
return this.getFromPage('album');
} else if (this.albumSelector) {
return Utils.query(this.albumSelector)
.then(node => node.textContent);
} else {
this.singleWarn('Connector.get album not implemented');
return Promise.resolve(undefined);
}
}
/**
* Get title of the current track. This method is intended to be overriden.
* @returns {Promise} fullfilled title
*/
get title() {
if (this.pageGetters.has('title')) {
return this.getFromPage('title');
} else if (this.titleSelector) {
return Utils.query(this.titleSelector)
.then(node => node.textContent.trim());
} else {
this.singleWarn('Connector.get title not implemented');
return Promise.resolve(undefined);
}
}
/**
* Get URL of the album cover. This method is intended to be overriden.
* @returns {Promise} fullfilled with URL of the album cover
*/
get artUrl() {
if (this.pageGetters.has('artUrl')) {
return this.getFromPage('artUrl');
} else if (this.artSelector) {
return Utils.query(this.artSelector).then(node => {
if (node.src) {
return node.src;
}
return Utils.extractUrlFromCssProperty(
node.style.backgroundImage || node.style.background
);
});
} else {
this.singleWarn('Connector.get artUrl not implemented');
return Promise.resolve(undefined);
}
}
/**
* Get unique ID of the current track. Should be overridden if we can do that
* and should be not overriden otherwise
* @returns {} ID
*/
get uniqueId() {
if (this.pageGetters.has('uniqueId')) {
return this.getFromPage('uniqueId');
} else {
this.singleWarn('Connector.get uniqueId not implemented');
return Promise.resolve(undefined);
}
}
/**
* Append unique ID to the prefix or get to common ID for undefined
* @returns {Promise} fullfilled with track ID
*/
get trackId() {
return this.uniqueId.then(uniqueId => {
if (!uniqueId) {
return '/me/f1u77y/web_media_controller/CurrentTrack';
} else {
return `${prefix.get(this)}/${uniqueId}`;
}
});
}
/**
* Get track info with other getters. May be overridden if you get info in other
* way then with the field getters
* @returns {Object} Track Info
*/
get trackInfo() {
return Promise.all([
this.length, this.artist, this.album, this.title, this.artUrl, this.trackId,
]).then(([length, artist, album, title, artUrl, trackId]) => {
return { length, artist, album, title, artUrl, trackId };
});
}
/**
* Should be called when property `name` was changed for sending this event
* to background. Should not be overridden and generally should be called only
* internally
* @param {Function} getter - A getter function for the property
* @param {string} name - The property name
*/
onPropertyChanged(getter, name) {
const throttle = throttleInterval[name];
if (throttle != null) {
const now = Date.now();
if (now - lastCallTime.get(this).get(name) < throttle) return;
lastCallTime.get(this).set(name, now);
}
getter.then(curValue => {
if (!_.isEqual(curValue, lastValue.get(this).get(name))) {
lastValue.get(this).set(name, curValue);
this.sendProperty(name, curValue);
}
});
}
/**
* Should be called when properties `propertyNames` were changed for sending
* these events to background. Should not be overridden
* @param {Array[string]} properties - The property names
*/
onStateChanged(properties = propertyNames) {
for (let name of properties) {
try {
this.onPropertyChanged(this[name], name);
}
catch (e) {
console.log(`${e.message}`);
console.log(`name = ${name}`);
}
}
}
}
export default BaseConnector;