Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mute Spotify during Ads #90

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class MprisLabel extends PanelMenu.Button {
if(!this.player || !this.player.identity)
return

const streamList = this.volumeControl.get_streams();
const streamList = this.volumeControl.get_sink_inputs();
this.stream = [];

streamList.forEach(stream => {
Expand Down Expand Up @@ -429,11 +429,40 @@ class MprisLabel extends PanelMenu.Button {

this._setText();
this._setIcon();
this._muteAds();

this._timeout = GLib.timeout_add(GLib.PRIORITY_DEFAULT,
REFRESH_RATE, this._refresh.bind(this));
}

_muteAds(){
if (!this.player || this.stream.length == 0)
return

const MUTE_SPOTIFY_ADS = this.settings.get_boolean('mute-spotify-ads');
const MUTE_SPOTIFY_ADS_DELAY = this.settings.get_int('mute-spotify-ads-delay');

if(this.player.identity == "Spotify" && MUTE_SPOTIFY_ADS){
const isAd=this.player.stringFromMetadata("mpris:trackid").startsWith('/com/spotify/ad/'); //identify ad using trackID

if (isAd && !this.stream[0].is_muted){ //mute if it's an ad and the player isn't already muted by user
GLib.timeout_add(GLib.PRIORITY_DEFAULT, MUTE_SPOTIFY_ADS_DELAY, () => {
this.player.mutedDuringAd = true;
this.stream.map(stream => stream.change_is_muted(true));
return GLib.SOURCE_REMOVE;
});
}

if (!isAd && this.player.mutedDuringAd){ //unmute if it's no longer an ad and it was muted by this extension
GLib.timeout_add(GLib.PRIORITY_DEFAULT, MUTE_SPOTIFY_ADS_DELAY, () => {
this.player.mutedDuringAd = false;
this.stream.map(stream => stream.change_is_muted(false));
return GLib.SOURCE_REMOVE;
});
}
}
}

_setIcon(){
const ICON_PLACE = this.settings.get_string('show-icon');
const ICON_PADDING = this.settings.get_int('icon-padding');
Expand Down Expand Up @@ -465,6 +494,13 @@ class MprisLabel extends PanelMenu.Button {
this.icon.set_style('-st-icon-style: symbolic;');
}

if (this.player.mutedDuringAd){ //replace icon with mute symbol if player muted during ad
this.icon = new St.Icon({
icon_name: 'audio-volume-muted-symbolic',
style_class: 'system-status-icon'
});
}

if (this.icon != null | undefined){
if (ICON_PLACE == "right"){
this.icon.set_style(this.icon.get_style() + "padding-left: " + ICON_PADDING + "px;padding-right: 0px;");
Expand Down Expand Up @@ -502,6 +538,20 @@ class MprisLabel extends PanelMenu.Button {
}

_disable(){
//optional: unmute any source which is currently muted by this extension on unload
//generic on purpose to allow adblock functionality to be extended to additional players if required
const currentPlayer = this.player
this.players.list.forEach(player => { //in case muted player is no longer the active player
if (player.mutedDuringAd){
this.player = player;
this._getStream();
if (this.stream.length > 0)
this.stream.map(stream => stream.change_is_muted(false));

this.player = currentPlayer;
}
});

if(this.icon)
this.box.remove_child(this.icon);

Expand Down
2 changes: 1 addition & 1 deletion patches/gnome45-compatibility.patch
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ index 856bd82..812ba72 100644
window.default_height = 950;

//panel page:
@@ -132,6 +135,7 @@ function fillPreferencesWindow(window){
@@ -139,6 +142,7 @@ function fillPreferencesWindow(window){
[doubleClickTime, doubleClickLabel, leftDoubleClickDropDown, middleDoubleClickDropDown, rightDoubleClickDropDown, thumbDoubleForwardDropDown, thumbDoubleBackwardDropDown]
.forEach(el => bindEnabled(settings, 'enable-double-clicks', el));
}
Expand Down
9 changes: 8 additions & 1 deletion prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,17 @@ function fillPreferencesWindow(window){
addSwitch(settings,group,'use-whitelisted-sources-only','Ignore all sources except allowed ones',"This option is ignored if the allow list is empty");
addEntry(settings,group,'album-blacklist','Players excluded from using album art','Separate entries with commas');

group = addGroup(page,'Ad-Block');
addSwitch(settings,group,'mute-spotify-ads','Mute Spotify app during ads','Mutes Spotify if the MPRIS Track ID Metadata identifies the song being played to be an ad.\nNote that this will work with the app, not when Spotify is played through a web browser');
let spotifyDelay = addSpinButton(settings,group,'mute-spotify-ads-delay','Delay before muting app (milliseconds)',0,9999,'As there tends to be a lag between mpris info update and ad/song starts, it is recommended \nto include some delay to avoid muting before the song ends or unmuting before the ad ends. \nNote that this may depend on your selected refresh rate.');

addResetButton(settings,group,'Reset Filters settings',[
'mpris-sources-blacklist','mpris-sources-whitelist','use-whitelisted-sources-only','album-blacklist']
'mpris-sources-blacklist','mpris-sources-whitelist','use-whitelisted-sources-only','album-blacklist','mute-spotify-ads','mute-spotify-ads-delay']
);

[spotifyDelay]
.forEach(el => bindEnabled(settings, 'mute-spotify-ads', el));

//controls page:
page = addPreferencesPage(window,'Controls','input-mouse-symbolic');

Expand Down
Binary file modified schemas/gschemas.compiled
Binary file not shown.
8 changes: 8 additions & 0 deletions schemas/org.gnome.shell.extensions.mpris-label.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@
<default>false</default>
</key>

<key name="mute-spotify-ads" type="b">
<default>true</default>
</key>

<key name="mute-spotify-ads-delay" type="i">
<default>1400</default>
</key>

<key name="enable-double-clicks" type="b">
<default>false</default>
</key>
Expand Down