-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
85 lines (78 loc) · 2.06 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'use strict';
let proxy = 'https://pristine-voyageurs-98160.herokuapp.com/';
function extract(str, tag, afap='?') {
try {
let reStr = '<' + tag + '[^>]*?>(.|\n)*' + afap + '</' + tag + '>';
let reTag = new RegExp(reStr, 'g');
let reInner = new RegExp('>(.|\n)*<', 'g');
let matches = str.match(reTag);
return matches.map(s => s.match(reInner)[0].slice(1, -1));
} catch(e) {
return 'ExtractionError: ' + e;
}
}
let vm = new Vue({
el: '#app',
data: {
mode: 'loading',
items: [],
current: 0,
content: '',
},
created: function() {
let url = 'http://www.aaronsw.com/2002/feeds/pgessays.rss';
let archives = JSON.parse(localStorage.getItem('archives')) || [];
fetch(proxy + url)
.then(resp => resp.text())
.then(text => {
let list = extract(text, 'item');
for (let item of list) {
let title = extract(item, 'title')[0];
let link = extract(item, 'link')[0];
let archived = archives.indexOf(link) != -1;
this.items.push({title, link, archived});
}
this.mode = 'picking';
})
.catch(() => {
this.mode = 'failing';
});
},
methods: {
read: function(index) {
this.current = index;
this.mode = 'loading';
fetch(proxy + this.items[index].link + '?viewfullsite=1')
.then(resp => resp.text())
.then(text => {
this.content = extract(text, 'font', '')[0];
this.mode = 'reading';
});
},
pick: function({mark = false}) {
if (mark) {
let item = this.items[this.current];
Vue.set(this.items, this.current, {
title: item.title,
link: item.link,
archived: !item.archived
});
}
this.mode = 'picking';
},
},
watch: {
items: {
handler: function() {
let archives = [];
for (let item of this.items) {
if (item.archived) {
archives.push(item.link);
}
}
localStorage.setItem('archives', JSON.stringify(archives));
},
deep: true,
}
},
});