所有文章

function replace_host(url){
let u = new URL(url);
u.host = window.location.host;
return u.toString();
}
function load_and_display_archive() {
let posts = [];
let url = “/feeds/posts/default?max-results=100&alt=json”;
while (url) {
const result = load_feeds(url);
if (result.error) {
document.write(“Error: ” + result.error);
return;
}
posts = posts.concat(result.posts);
url = result.next ? replace_host(result.next.href) : null;
}
display_toc(posts);
}
function parse_feeds(data) {
let posts = [];
if (“entry” in data) {
for (let i in data.entry) {
const post = data.entry[i];
posts.push({
title: post.title.$t,
year: parseInt(post.published.$t.substring(0, 4)),
month: parseInt(post.published.$t.substring(5, 7)),
day: parseInt(post.published.$t.substring(8, 10)),
labels: post.category ? post.category.map(x => x.term) : [],
url: post.link.find(x => x.rel == “alternate”),
});
}
}
return posts;
}
function load_feeds(url) {
let req = new XMLHttpRequest();
req.open(“GET”, url, false);
req.send();
if (req.status == 200) {
var data = JSON.parse(req.responseText);
return {
posts: parse_feeds(data.feed),
next: data.feed.link.find(x => x.rel == “next”),
}
}
return {
error: “unexpected response: ” + req.status,
}
}
function display_toc(posts) {
let currentYear = 0;
let first = true;
for (let i in posts) {
const post = posts[i];
if (currentYear != post.year) {
currentYear = post.year;
if (!first) {
document.write(‘

‘);
} else {
first = false;
}
document.write(‘

‘ + currentYear + ‘

‘);
document.write(‘

‘);
}
const monthPad = (“0” + post.month).slice(-2);
const dayPad = (“0” + post.day).slice(-2);
document.write(‘

‘);
document.write(‘

‘);
document.write(‘

‘);
}
document.write(‘

‘ + monthPad + ‘-‘ + dayPad + ‘ ‘ + post.title + ‘ ‘);
document.write(‘

‘);
for (let t in post.labels) {
const catName = post.labels[t];
document.write(‘‘ + catName + ‘ ‘);
}
document.write(‘

‘);
}
load_and_display_archive();

table.list-archive tr td:nth-child(1) {
white-space: nowrap;
width: 1%;
}

table.list-archive .label {
margin-left: 4px;
}