Problem gathering XML data
|
|
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? |
|
|
link, title and pubDate are not attributes, but elements.
|
|
|
I've changed those lines (and edited the original post to include them). However, if I do Also, if I move the |
|
|
|
|
|
That returns 'null'. The script still dies at |
|
|
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. |
|
|
As I said, I tried that and the the results of Edit: Hrm, it looks like I really screwed up writing that previous post. But yeah, I did add |
|
|
// ==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;
}
});
|
|
|
Thank you so much! |
