Problem gathering XML data

Subscribe to Problem gathering XML data 9 posts, 3 voices

 
Cronje Scriptwright

I'm trying to make a script to modify one of my favorite fanfiction sites, using RSS feed data. I'm somewhat of a novice when it comes to javascript, but I think I understand everything I'm trying to do. What I don't understand is why it's not working. My code is based off of another script (http://www.thegrundleonline.com/KATG/greasemonk...). Here's my script:

// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          Fanficauthors.net Fic Listing
// @namespace     http://www.fanficauthors.net
// @description   Replaces the left-hand panel of authors with the last 20 fics uploaded to the site.
// @include       http://www.fanficauthors.net/*
// @include       http://fanficauthors.net/*
// ==/UserScript==

var ficLinks   = new Array();
var ficNames   = new Array();
var ficDates   = new Array();
var ficAuthors = new Array();

function replacePanel() {
var oldPanel    = document.getElementById('quicklinks');
var replaceWith;
for (var i=0; i<20; i++) {
replaceWith = replaceWith + "<b>" + ficAuthors[i] + "</b> (" + ficDates[i] +
  ")<br><a href=\"" + ficLinks[i] + "\" title=\"" +
  ficNames[i] + "\">" + ficNames[i] + "</a><p>";
}

oldPanel.innerHTML = replaceWith;
}

GM_xmlhttpRequest({
method: 'GET',
url: 'http://www.fanficauthors.net/rss.php',
headers: {
'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey/0.3',
'Accept': 'application/atom+xml,application/xml,text/xml',
},
onload: function(responseDetails) {
alert("Test");
var parser = new DOMParser();
var dom    = parser.parseFromString(responseDetails.responseText,"text/xml");
var root   = dom.getElementsByTagName('item');

for (var i=0; i<20; i++) {
var itemLink   = root[i].getElementsByTagName('link')[0];
var itemName   = root[i].getElementsByTagName('title')[0];
var itemDate   = root[i].getElementsByTagName('pubDate')[0];

var itemSplit  = itemName.indexOf(" - ");
var itemTitle  = itemName.substr(0,itemSplit);
var itemAuthor = itemName.substr(itemSplit+3);

ficLinks[i]   = itemLink;
ficNames[i]   = itemTitle;
ficDates[i]   = itemDate;
ficAuthors[i] = itemAuthor;
}

 replacePanel();
 }
});

Any thoughts?

 
Mikado Scriptwright

link, title and pubDate are not attributes, but elements.
var itemLink = root[i].getElementsByTagName('link')[0]; and so on.

 
Cronje Scriptwright

I've changed those lines (and edited the original post to include them). However, if I do alert(itemName) after those lines, I get this: [object XPCNative Wrapper [object Element]]. Any idea why?

Also, if I move the alert(itemName) to below the var itemAuthor line, nothing happens. The script seems to die after those three lines.

 
sizzlemctwizzle Scriptwright

alert(itemName.nodeValue)

 
Cronje Scriptwright

That returns 'null'. The script still dies at var itemSplit = itemName.indexOf(" - ");, even if I add .nodeValue to the end of the itemLink, itemName, and itemDate lines. I imagine this is because itemName.indexOf(" - ") finds nothing, though.

 
sizzlemctwizzle Scriptwright

Replace the respective lines with this.

var itemLink   = root[i].getElementsByTagName('link')[0].nodeValue;
var itemName   = root[i].getElementsByTagName('title')[0].nodeValue;
var itemDate   = root[i].getElementsByTagName('pubDate')[0].nodeValue;

That should fix it all.

 
Cronje Scriptwright

As I said, I tried that and the the results of alert(itemName); was null, which is definitely not what it should be.

Edit: Hrm, it looks like I really screwed up writing that previous post. But yeah, I did add .nodeValue to the end of each of the respective lines and when I would alert(itemName); or alert(itemDate), etc., it would return null.

 
Mikado Scriptwright

// ==UserScript==
// @name           ff
// @namespace      http://userscripts.org/users/31647
// @include        http://fanficauthors.net/*
// ==/UserScript==

GM_xmlhttpRequest({
	method: 'GET',
	url: 'http://www.fanficauthors.net/rss.php',
	onload: function(responseDetails) {
		var parser = new DOMParser(),
			dom = parser.parseFromString(responseDetails.responseText,"text/xml"),
			root = dom.getElementsByTagName('item'),
			replaceWith = '',
			oldPanel = document.getElementById('quicklinks');

		for (var i=0; i<20; i++) {
			var itemLink   = root[i].getElementsByTagName('link')[0].textContent;
			var itemName   = root[i].getElementsByTagName('title')[0].textContent, itemSplit  = itemName.indexOf(" - ");
			var itemDate   = root[i].getElementsByTagName('pubDate')[0].textContent;

			replaceWith += "<b>" + itemName.substr(itemSplit+3) + "</b> (" + itemDate + ")<br><a href=\"" + itemLink + "\" title=\"" + itemName.substr(0,itemSplit) + "\">" + itemName + "</a><p>";

		}

		oldPanel.innerHTML = replaceWith;
	}
});

 
Cronje Scriptwright

Thank you so much!