I'm sure someone out there has already written a perfectly good Greasemonkey script that I could use, but where is the fun in that. It has been a while since I wrote anything in JavaScript so I figured this would be a great opportunity to clear away any cobwebs.
The first thing that needs to happen is to be notified of any changes to the DOM. This is quite easy using MutationObserver defined in the DOM4 specification. The callback is provided with a MutationRecord that contains the addedNodes as a property. We can then search the addedNodes for the "I'm still listening" button.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Yes, Pandora, I'm Still Listening | |
// @namespace http://dbknickerbocker.blogspot.com | |
// @description Automatically click "I'm still listening" | |
// @match http://www.pandora.com/* | |
// @match https://www.pandora.com/* | |
// @updateURL https://gist.github.com/dbknickerbocker/5300881/raw/pandora_user_script.js | |
// @version 0.2 | |
// ==/UserScript== | |
function checkMutationForStillListening(mutation) { | |
if ((mutation === null) || (mutation.addedNodes === null)) { | |
return false; | |
} | |
for (var index = 0; index < mutation.addedNodes.length; ++index) { | |
var addedNode = mutation.addedNodes[index]; | |
if (addedNode === null) { | |
continue; | |
} | |
var stillListeningNode = addedNode.querySelector('a.still_listening'); | |
if (stillListeningNode !== null) { | |
stillListeningNode.click(); | |
return true; | |
} | |
} | |
return false; | |
} | |
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver; | |
if (typeof MutationObserver !== 'undefined') { | |
var mutationObserver = new MutationObserver(function (mutations) { | |
mutations.some(checkMutationForStillListening); | |
}); | |
mutationObserver.observe(window.document, { childList: true, subtree: true }); | |
} |
My browser of choice is Chrome, and unfortunately it no longer allows users to install .user.js scripts just by opening them. You now have to open the Extensions window and drag/drop the file onto the page. Although I haven't tested it, the script should work in Firefox w/ the Greasemonkey extension.
No comments:
Post a Comment