Skip to content

Instantly share code, notes, and snippets.

@bmatsuo
Created October 10, 2010 02:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmatsuo/618845 to your computer and use it in GitHub Desktop.
Save bmatsuo/618845 to your computer and use it in GitHub Desktop.
Single/EP of the week that looks like the Featured Album box.
// ==UserScript==
// @name What.cd Single/EP of the Week
// @namespace http://google.com
// @description See the latest Single/EP of the week on the what.cd homepage.
// @include http*://*what.cd/index.php
// ==/UserScript==
//Thanks to dieselpowered for making it more like the Featured Album box!
function getElementsByClassName(classname, node) {
if(!node) node = document.getElementsByTagName("body")[0];
var a = [];
var re = new RegExp('\\b' + classname + '\\b');
var els = node.getElementsByTagName("*");
for(var i=0,j=els.length; i<j; i++)
if(re.test(els[i].className))a.push(els[i]);
return a;
}
//insert new sidebox
var sideBox = document.createElement("div");
sideBox.className = "box";
var statBox = getElementsByClassName("box", false)[1];
statBox.parentNode.insertBefore(sideBox, statBox);
sideBox.innerHTML = '<div class="head colhead_dark">\
<strong>Single/EP of the Week</strong></div>\
<p>Loading, please wait.</p>';
//Load RSS feed
GM_xmlhttpRequest({ method:'get',
url:'http://whatsingle.nfshost.com/rss.php',
onload: function(response) {
//Parse feed
var parser = new DOMParser();
var feed = parser.parseFromString(response.responseText,
"application/xml");
var latestPost = feed.getElementsByTagName("item")[0];
var title = latestPost.getElementsByTagName("title")[0].textContent;
var url = latestPost.getElementsByTagName("link")[0].textContent.split(
".cd/", 2)[1];
var cont=latestPost.getElementsByTagName("description")[0].textContent;
cont = cont.replace(/"http[s]?:\/\/(ssl\.)?what\.cd\//g,'"/')
var root = document.createElement("div");
root.innerHTML = cont
// Find the user quote's node and get the quote as a string.
var userQuote = root.getElementsByTagName('blockquote')[0]
var nonEmptyQuote = userQuote.innerHTML.length > 0
var blankQuote = userQuote.match(/\S/) == null
if (nonEmptyQuote && !blankQuote) {
userQuote.setAttribute('class','pad hidden')
userQuote.setAttribute('id', 'sotw_box')
var qStr = userQuote.innerHTML
qStr = qStr.replace(/<br\s*\/?>/g, " ")
if (qStr.length > 80) {
qStr = qStr.substr(0, 120).replace(/\S+$/,'') + '...'
}
// Create a link that toggles visibility of user's quote.
var showQuoteLink = document.createElement("div");
showQuoteLink.setAttribute('class','center')
showQuoteLink.innerHTML= '' +
'<a href="#" ' +
'onclick="$(\'#sotw_box\').toggle(); return false;"' +
'title="' + qStr + '"' +
">[Show/Hide quote]</a>"
userQuote.parentNode.insertBefore(showQuoteLink, userQuote)
}
else {
userQuote.parentNode.removeChild(userQuote)
}
cont = root.innerHTML
//scale image
cont = cont.replace(/<img/g, '<img width="100%" ');
//update box
sideBox.innerHTML = '' +
'<div class="head colhead_dark">'+
'<strong>Single/EP of the week</strong></div>' +
cont +
'<div class="center pad">' +
'<a href="/forums.php?action=viewthread&threadid=71081&post=1">' +
'<i>Discuss this single/EP here!</i></a></div>';
}});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment