-
Notifications
You must be signed in to change notification settings - Fork 8
/
extension.js
221 lines (184 loc) · 6.17 KB
/
extension.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
import St from 'gi://St';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import Gvc from 'gi://Gvc';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as Mpris from 'resource:///org/gnome/shell/ui/mpris.js';
import * as Volume from 'resource:///org/gnome/shell/ui/status/volume.js';
import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js';
let adBlocker;
const MPRIS_PLAYER = 'org.mpris.MediaPlayer2.spotify';
const NOT_MUTED = -1;
const WATCH_TIMEOUT = 3000;
var AdBlocker = class AdBlocker {
constructor(settings) {
this.media = new Mpris.MediaSection();
this.settings = settings;
this.player = null;
this.playerWatchTimeoutId = 0;
this.activated = false;
this.playerId = 0;
this.button = new St.Bin({ style_class: 'panel-button',
reactive: true,
can_focus: true,
track_hover: true });
this.music_icon = new St.Icon({
icon_name: 'folder-music-symbolic',
style_class: 'system-status-icon'
});
this.ad_icon = new St.Icon({
icon_name: 'tv-symbolic',
style_class: 'system-status-icon'
});
this.button.set_child(this.music_icon);
this.button.connect('button-press-event', this.toggle.bind(this));
this.muteTimeout = 0;
this.enable();
this.settings.connect('changed::show-indicator', () => {
if (this.settings.get_boolean('show-indicator')) {
Main.panel._rightBox.insert_child_at_index(this.button, 0);
} else {
Main.panel._rightBox.remove_child(this.button);
}
});
}
reloadPlayer() {
if (this.playerId) {
this.player.disconnect(this.playerId);
this.playerId = 0;
}
this.player = this.media._players.get(MPRIS_PLAYER);
if (this.player) {
// Update right away in case the 'changed' signal has already been emitted
this.update();
this.playerId = this.player.connect('changed', this.update.bind(this));
}
}
toggle() {
if (!this.activated) {
this.enable();
} else {
this.disable();
}
}
get muted() {
return this.volumeBeforeAds !== NOT_MUTED;
}
get streams() {
let mixer = Volume.getMixerControl();
let spotify = mixer.get_sink_inputs()
.filter(y => y.get_name() && y.get_name().toLowerCase() === 'spotify');
if (spotify.length)
return spotify;
// spotify not found
return [];
}
get volumeBeforeAds() {
return this.settings.get_int('volume-before-ads');
}
set volumeBeforeAds(newVolume) {
this.settings.set_int('volume-before-ads', newVolume);
}
mute() {
if (this.muted)
return;
if (this.muteTimeout) {
GLib.source_remove(this.muteTimeout);
this.muteTimeout = 0;
}
if (this.streams.length > 0) {
this.volumeBeforeAds = this.streams[0].get_volume();
this.streams.map(s => s.set_volume(this.volumeBeforeAds * this.settings.get_int('ad-volume-percentage') / 100));
// This needs to be called after changing the volume for it to take effect
this.streams.map(s => s.push_volume());
}
this.button.set_child(this.ad_icon);
}
unmute() {
if (!this.muted)
return;
// Wait a bit to unmute, there's a delay before the next song
// starts
this.muteTimeout = GLib.timeout_add(GLib.PRIORITY_DEFAULT, this.settings.get_int('unmute-delay'),
() => {
this.muteTimeout = 0;
if (this.muted && this.streams.length > 0) {
this.streams.map(s => s.set_volume(this.volumeBeforeAds));
this.streams.map(s => s.push_volume());
this.volumeBeforeAds = NOT_MUTED;
}
this.button.set_child(this.music_icon);
return GLib.SOURCE_REMOVE;
});
}
isAd() {
const blocklist = [
'spotify:ad',
'/com/spotify/ad/',
];
let trackId = this.player._playerProxy.Metadata['mpris:trackid'];
if (!trackId)
return false;
trackId = trackId.unpack();
return blocklist.some((b) => trackId.startsWith(b));
}
update() {
if (!this.activated)
return;
if (this.isAd()) {
this.mute();
} else {
this.unmute();
}
}
enable() {
this.activated = true;
this.button.opacity = 255;
this.reloadPlayer();
this.watch();
}
disable() {
this.activated = false;
this.button.opacity = 100;
if (this.playerId)
this.player.disconnect(this.playerId);
if (this.muteTimeout) {
GLib.source_remove(this.muteTimeout);
this.muteTimeout = 0;
}
this.playerId = 0;
this.stopWatch();
this.player = null;
}
watch() {
this.playerWatchTimeoutId = GLib.timeout_add(
GLib.PRIORITY_DEFAULT,
WATCH_TIMEOUT,
() => {
if (!this.player || !this.player._playerProxy) {
this.reloadPlayer();
}
return GLib.SOURCE_CONTINUE;
});
}
stopWatch() {
if (this.playerWatchTimeoutId) {
GLib.source_remove(this.playerWatchTimeoutId);
this.playerWatchTimeoutId = 0;
}
}
}
export default class SpoitifyAdBlockExtension extends Extension {
enable() {
let settings = this.getSettings();
adBlocker = new AdBlocker(settings);
if (adBlocker.settings.get_boolean('show-indicator')) {
Main.panel._rightBox.insert_child_at_index(adBlocker.button, 0);
}
}
disable() {
adBlocker.disable();
Main.panel._rightBox.remove_child(adBlocker.button);
adBlocker = null;
}
}