// ==UserScript==// @name           NPR Download As MP3// @namespace      http://edpad.com/npr-mp3/// @description    This adds a link to NPR story pages so that you can right-click and download the story MP3 file (instead of being forced to play their files through the Flash player). Please support your local NPR station!// @include        http://*.npr.org/templates/story/story.php*// ==/UserScript==function insertAfter(newNode, node) {return node.parentNode.insertBefore(newNode, node);}var con = document.getElementById("storybody").innerHTML;var i = con.indexOf("NPR.Player.openPlayer");var t = con.indexOf("(",i);var j = con.indexOf(",",t);var k = con.indexOf(",",j+1);var id1 = con.substring(t+1,j);var id2 = con.substring(j+2,k);  GM_xmlhttpRequest({   method:"GET",   url:"http://api.npr.org/query?fields=show,audio,multimedia,titles,dates,song,album&apiKey=MDAzMzQ2MjAyMDEyMzk4MTU1MDg3ZmM3MQ010&id=" + id1 +"&m="+id2+ "&t=1",       headers:{     "User-Agent": navigator.userAgent,            "Accept":"text/xml"   },   onload:function(response) {   var xmlobject = (new DOMParser()).parseFromString(response.responseText, "text/xml");var root = xmlobject.getElementsByTagName('mp3');var desc = root[0].firstChild.nodeValuedownloadDiv = document.createElement('div');downloadDiv.innerHTML = '<div><A href="' + desc + '">Right-click to Download MP3</a></div>';var link = document.getElementById("storybody");insertAfter(downloadDiv, link);var searchlink = document.createElement("div");searchlink.innerHTML = '<a href="search.php">simply search</a><br />';var allAs = document.evaluate(    "//a[contains(@href,'javascript:NPR.Player.openPlayer')]",    document,    null,    XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,    null);for (i = 0; i < allAs.snapshotLength;) {var thisA = allAs.snapshotItem(i);insertAfter(searchlink, thisA);i++;}   } });//===============================================================================//			- Weekly Auto-Update Check -//===============================================================================var script_title = "NPR Download As MP3";var source_location = "http://edpad.com/npr-mp3/nprmp3.user.js";var current_version = "0.2.0";var latest_version = " ";var gm_updateparam = "nprmp3_lastupdatecheck";var lastupdatecheck = GM_getValue(gm_updateparam, "never");// a google document is used to store the latest version number (If the version in that file does not match the current_version variable, an update will be triggered)var version_holder = "http://edpad.com/npr-mp3/latest.txt";//Add a command to the menu in case someone wants to manually check for an update.GM_registerMenuCommand("NPRMP3: Check for update", CheckVersion);//Initiate the download of the new script version.function GetNewVersion() {        var today = new Date();        GM_setValue(gm_updateparam, String(today));        window.location = source_location;}//Verify if it's time to updatefunction CheckForUpdate(){		var today = new Date();	var one_day = 24 * 60 * 60 * 1000; //One day in milliseconds	if(lastupdatecheck != "never")	{		today = today.getTime(); //Get today's date		var interval = (today - lastupdatecheck.getTime()) / one_day; //Find out how much days have passed				//If a week has passed since the last update check, check if a new version is available		if(interval >= 7)						CheckVersion();	}	else		CheckVersion();}//Make sure we don't have the latest versionfunction CheckVersion(){	GM_xmlhttpRequest({		    method: 'GET',		    url: version_holder,		    headers: {'Content-type':'application/x-www-form-urlencoded'},		    		    onload: function(responseDetails)			{				var line = String(responseDetails.responseText.match(/version=[0-9].[0-9]?[0-9].[0-9]?[0-9]/));												if(line != null)				{					var strSplit = new Array();					strSplit = line.split('=');										latest_version = strSplit[1];					if(current_version != latest_version && latest_version != "undefined")					{						if(confirm("A more recent version of " + script_title + " (" + latest_version + ") has been found.\r\nWould you like to get it now?"))							GetNewVersion();						else							AskForReminder();					} 					else if(current_version == latest_version)						alert("You have the latest version of " + script_title + ".");				}				else				{					alert("Could not locate the version holder file.\r\nThis should be reported to the script author.\r\nThank you!");					SkipWeeklyUpdateCheck();				}							    }		});}//Ask the user to be reminded in 24 hours or only next week.function AskForReminder(){	if(confirm("Would you like to be reminded in 24 hours ?\r\n(Cancel to be reminded next week only)"))	{		var today = new Date();		today = today.getTime();				var sixdays_ms = 6 * 24 * 60 * 60 * 1000;		var sda_ms = today - sixdays_ms;				var sixdaysago = new Date(sda_ms)		//Since we check for updates after 7 days, just make it seem like the last check was 6 days ago.		GM_setValue(gm_updateparam, String(sixdaysago));	}	else		SkipWeeklyUpdateCheck();}//Set the next update check in seven daysfunction SkipWeeklyUpdateCheck(){	var today = new Date();	//As if we've just updated the script, the next check will only be next week.	GM_setValue(gm_updateparam, String(today));}//===============================================================================//			- Weekly Auto-Update Check -//===============================================================================