Skip to content

Instantly share code, notes, and snippets.

@neon-dev
Last active November 16, 2023 12:48
Show Gist options
  • Save neon-dev/8ad468508996a6fff17c8c237d689c71 to your computer and use it in GitHub Desktop.
Save neon-dev/8ad468508996a6fff17c8c237d689c71 to your computer and use it in GitHub Desktop.
Userscript that detects unwanted popups on the Amazon Music web player and closes them automatically (like asking "Hören Sie noch zu?" or trying to sell Amazon Music Unlimited memberships)
// ==UserScript==
// @name Amazon Music Dialoghandler
// @namespace https://gist.github.com/neon-dev
// @version 0.2
// @description Closes unwanted popups on the Amazon Music web player
// @author Neon
// @match https://music.amazon.de/*
// @match https://music.amazon.com/*
// @match https://music.amazon.co.uk/*
// @icon https://d5fx445wy2wpk.cloudfront.net/icons/amznMusic_favicon.png
// @updateURL https://gist.github.com/neon-dev/8ad468508996a6fff17c8c237d689c71/raw
// @downloadURL https://gist.github.com/neon-dev/8ad468508996a6fff17c8c237d689c71/raw
// @grant none
// ==/UserScript==
(function() {
'use strict';
const buttonQuerySelectorByDialogTitle = {
'Melden Sie sich bei Amazon Music Unlimited an': '#dialogCloseButton music-button',
'Sign Up for Amazon Music Unlimited': '#dialogCloseButton music-button',
'Dieses Feature ist nur mit Amazon Music Unlimited verfügbar': '#dialogCloseButton music-button',
'This feature is only available with Amazon Music Unlimited': '#dialogCloseButton music-button',
'Hörst du noch zu? ': '#dialogButton1',
'Are you still listening ? ': '#dialogButton1'
}
new MutationObserver((mutations, observer) => mutations.forEach(m => m.addedNodes.forEach(handle)))
.observe(document.querySelector('#overlay div'), { subtree: true, childList: true });
handle(document.getElementById('dialog')); // sometimes the popup is shown on page (re)load
function handle(dialog) {
const dialogTitle = dialog?.id === 'dialog' ? dialog.querySelector('#dialogHeader').textContent || dialog.querySelector('#dialogBodyText').textContent : '';
const buttonQuerySelector = dialogTitle ? buttonQuerySelectorByDialogTitle[dialogTitle] : null;
if (buttonQuerySelector) {
const button = dialog.querySelector(buttonQuerySelector);
if (button) {
button.click();
console.debug('[Amazon Music Dialoghandler] Clicked button ' + (button.textContent ? '"' + button.textContent + '" ' : '') + 'of dialog: "' + dialogTitle + '"');
} else {
console.debug('[Amazon Music Dialoghandler] No button matched configured query selector for dialog: "' + dialogTitle + '"');
}
} else if (dialogTitle) {
console.debug('[Amazon Music Dialoghandler] No action for dialog: ' + dialogTitle);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment