-
Notifications
You must be signed in to change notification settings - Fork 2
/
contentScript.js
157 lines (133 loc) · 5.52 KB
/
contentScript.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
(function(window, document) {
function FeedPlaylist() {
if (!window.playerview || !window.Dom) {
console.error('[bandcampFeedPlaylist] player is not found.')
return null;
}
// Observable
this.playlist = window.playerview._playlist;
this._state = this.playlist._state;
this._track = this.playlist._track;
this._position = this.playlist._position;
this._duration = this.playlist._duration;
this.handlerNextTrack = function() { return this.next() }.bind(this);
this.handlerPreviousTrack = function() { return this.previous() }.bind(this);
this.$trackPlayWaypoint = window.playerview._waypoints[0];
if (this.$trackPlayWaypoint) {
this.$el = this.injectHtml();
this.$position = this.$el.querySelector('#track_play_waypoints_controls_position');
this.$duration = this.$el.querySelector('#track_play_waypoints_controls_duration');
this.observe();
this.registerMediaKeys();
console.debug('[bandcampFeedPlaylist] injected');
}
}
// Observe changes
FeedPlaylist.prototype.observe = function(e) {
const self = this;
const observers = [
{ 'obj': this.playlist, prop: '_track' },
{ 'obj': this.playlist, prop: '_state', callback: this.onStateUpdate.bind(this) },
{ 'obj': this.playlist, prop: '_duration', callback: this.updateDuration.bind(this) },
{ 'obj': this.playlist, prop: '_position', callback: this.updatePosition.bind(this) },
]
observers.map(observer => {
Object.defineProperty(observer.obj, observer.prop, {
get: function() { return self[observer.prop]; },
set: function(newValue) {
if (newValue === self[observer.prop]) { return }
self[observer.prop] = newValue;
if (typeof observer.callback == 'function') {
return observer.callback(newValue);
}
return newValue;
}
})
})
}
// Methods
FeedPlaylist.prototype.playpause = function(e) {
if (e) {
e.preventDefault();
}
return this.playlist.playpause();
}
FeedPlaylist.prototype.next = function(e) {
if (e) {
e.preventDefault();
}
return this.playlist.next_track()
}
FeedPlaylist.prototype.previous = function(e) {
if (e) {
e.preventDefault();
}
return this.playlist.prev_track();
}
FeedPlaylist.prototype.onStateUpdate = function(state) {
// TODO: replace setTimeout
if (state === "COMPLETED") {
let timer = setTimeout(this.handlerNextTrack, 1000);
timer = undefined;
}
// We try to force the waypoint offset to show 'activated' properly
const track = window.playerview._current_track_list_entry();
const $story = document.querySelector(`[data-trackid='${track.id}'`);
if (!$story) return
if (!window.Dom.inViewport($story)) {
// We push at the end of execution stack
setTimeout(() => {
this.$trackPlayWaypoint.classList.add('activated')
}, 0)
}
return;
}
// DOM
FeedPlaylist.prototype.updatePosition = function() {
return this.$position.innerText = window.Time.timeStr(this._position)
}
FeedPlaylist.prototype.updateDuration = function() {
return this.$duration.innerText = window.Time.timeStr(this._duration)
}
FeedPlaylist.prototype.injectHtml = function() {
const container = document.createElement('div');
container.id = "track_play_waypoint_controls";
const infos = [
{ text: "0:00", id: "track_play_waypoints_controls_position" },
{ text: "/" },
{ text: "0:00", id: "track_play_waypoints_controls_duration" },
];
const controls = [
{ text: 'Previous', action: this.previous.bind(this) },
{ text: 'Play/Pause', action: this.playpause.bind(this) },
{ text: 'Next', action: this.next.bind(this) }
];
infos.map(info => {
let element = document.createElement('span');
if (info.id) {
element.id = info.id;
}
element.innerText = info.text;
container.appendChild(element);
});
controls.map(control => {
let element = document.createElement('a');
element.href = "#";
element.innerText = control.text;
element.addEventListener('click', control.action)
container.appendChild(element);
});
return this.$trackPlayWaypoint.parentElement.appendChild(container);
}
// Support: Chrome Desktop 73+, Chrome Mobile 57+
// https://www.chromestatus.com/feature/5639924124483584
FeedPlaylist.prototype.registerMediaKeys = function() {
if (!('mediaSession' in navigator)) {
console.warn('[bandcampFeedPlaylist] MediaSession API not supported in this browser - https://www.chromestatus.com/feature/5639924124483584')
}
navigator.mediaSession.setActionHandler('previoustrack', this.handlerPreviousTrack)
navigator.mediaSession.setActionHandler('nexttrack', this.handlerNextTrack)
}
console.debug('[bandcampFeedPlaylist] loaded');
let feedPlaylist = new FeedPlaylist();
})(window, document);