1
任何人都可以看到這個代碼的問題?我的應用程序的工作,直到我添加這些額外的線路:<description>標籤導致xml停止解析?
var descriptionNode = item.getElementsByTagName("description")[0];
var description = descriptionNode.firstChild.data;
var item = { ...
...
...
...
...
description: description,
...
};
,並查看頁面
var descriptionNode = document.createElement("td");
descriptionNode.setAttribute("class", "description");
descriptionNode.appendChild(document.createTextNode(item["description"]));
tr.appendChild(descriptionNode);
...
<th scope="col">Description</th>
,並在rss.xml文件
<description>This is a description</description>
全部片段:
<html>
<head>
<script type="text/javascript">
var items = []; // contains newest items
var unsorted = []; // contains all items
function init() {
if (!localStorage.updateInterval) {
localStorage.updateInterval = 5;
}
if (!localStorage.types) {
localStorage.types = "audio:true;art:true;literature:true;";
}
chrome.browserAction.setBadgeBackgroundColor({color:[255, 102, 0, 255]});
setTimeout(makeRequest, 3000);
}
function makeRequest() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
parseItems(xmlhttp.responseXML);
}
};
xmlhttp.open("GET", "http://url.com/rss.xml", true);
xmlhttp.send(null);
setTimeout(makeRequest, 1000 * 60 * localStorage.updateInterval);
}
function parseItems(data) {
var items = data.getElementsByTagName("item");
for (var i = 0; i < items.length; i++) {
var item = items[i];
var dateNode = item.getElementsByTagName("pubDate")[0];
var date = formatDate(dateNode.firstChild.data);
var titleNode = item.getElementsByTagName("title")[0];
var title = formatTitle(titleNode.firstChild.data);
var type = titleNode.firstChild.data.split("[")[1].split("]")[0];
var linkNode = item.getElementsByTagName("link")[0];
var link = trimSpaces(linkNode.firstChild.data);
var authorNode = item.getElementsByTagName("author")[0];
var author = authorNode.firstChild.data;
var descriptionNode = item.getElementsByTagName("description")[0];
var description = descriptionNode.firstChild.data;
var item = { date: date,
title: title,
featured: false,
type: type,
author: author,
description: description,
link: link };
unsorted[i] = items;
}
processItems();
}
...
查看
<script type="text/javascript">
var items = [];
var background;
function init() {
background = chrome.extension.getBackgroundPage();
items = background.items;
createItemTable();
}
function createitemTable() {
var table = document.getElementById("item-table");
var tbody = document.createElement("tbody");
for (x in items) {
var item = items[x];
var tr = document.createElement("tr");
if (item["featured"]) {
tr.setAttribute("class", "featured");
item["featured"] = false;
}
var dateNode = document.createElement("td");
dateNode.setAttribute("class", "date");
dateNode.appendChild(document.createTextNode(item["date"]));
tr.appendChild(dateNode);
var titleNode = document.createElement("td");
titleNode.setAttribute("class", "title");
titleNode.appendChild(document.createTextNode(item["title"]));
tr.appendChild(titleNode);
var typeNode = document.createElement("td");
typeNode.setAttribute("class", "type");
typeNode.appendChild(document.createTextNode(item["type"]));
tr.appendChild(typeNode);
var authorNode = document.createElement("td");
authorNode.setAttribute("class", "author");
authorNode.appendChild(document.createTextNode(item["author"]));
tr.appendChild(authorNode);
var descriptionNode = document.createElement("td");
descriptionNode.setAttribute("class", "description");
descriptionNode.appendChild(document.createTextNode(item["description"]));
tr.appendChild(descriptionNode);
tbody.appendChild(tr);
}
table.appendChild(tbody);
}
function openTab(url) {
chrome.tabs.create({url: url});
}
</script>
</head>
<body onload="init();" onunload="background.updateBadge();">
<img src="img/logo_popup.png" id="logo" alt="" title="" onclick="openTab('http://www.site.com/');" />
<table id="item-table">
<thead>
<tr>
<th scope="col">Date</th>
<th scope="col">Title</th>
<th scope="col">Type</th>
<th scope="col">Author</th>
<th scope="col">Description</th>
</tr>
</thead>
</table>
</body>
Rss.xml
<rss version="2.0">
<channel>
<title>Site TItle</title>
<link>http://www.site.com</link>
<language>en-us</language>
<pubDate>Sun, 03 Jul 2011 14:40:50 +0000</pubDate>
<lastBuildDate>Sun, 03 Jul 2011 14:40:50 +0000</lastBuildDate>
<copyright/>
<item>
<title>
[art] - This is a title
</title>
<link>http://www.site.com/id?=333</link>
<author>Author</author>
<description>This is a description</description>
<pubDate>Sun, 03 Jul 2011 12:38:12 +0000</pubDate>
</item>
<item>
<title>
[music] - This is a title
</title>
<link>http://www.site.com/id?=332</link>
<author>Author</author>
<description>This is a description</description>
<pubDate>Sun, 03 Jul 2011 12:14:53 +0000</pubDate>
</item>
</channel>
</rss>