Mercurial > addons > firefox-addons > feed-preview
annotate js/feed-parser.js @ 31:76e23b361e92
Allow feed reader addons to subscribe to the currently viewed feed
Add a link element to the feed preview that points to the currently viewed feed
in order to allow feed reader addons to subscribe to it.
author | Guido Berhoerster <guido+feed-preview@berhoerster.name> |
---|---|
date | Tue, 22 Jan 2019 14:40:06 +0100 |
parents | 104a1faebd43 |
children | b68880838990 |
rev | line source |
---|---|
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
1 /* |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
2 * Copyright (C) 2018 Guido Berhoerster <guido+feed-preview@berhoerster.name> |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
3 * |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
4 * This Source Code Form is subject to the terms of the Mozilla Public |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
5 * License, v. 2.0. If a copy of the MPL was not distributed with this |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
7 */ |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
8 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
9 'use strict'; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
10 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
11 export const XMLNS = { |
15
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
12 ATOM03: 'http://purl.org/atom/ns#', |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
13 ATOM10: 'http://www.w3.org/2005/Atom', |
14
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
14 RSS09: 'http://my.netscape.com/rdf/simple/0.9/', |
19
3fcd2209b39a
Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
18
diff
changeset
|
15 RSS10: 'http://purl.org/rss/1.0/', |
15
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
16 XHTML: 'http://www.w3.org/1999/xhtml', |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
17 PARSERERROR: 'http://www.mozilla.org/newlayout/xml/parsererror.xml' |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
18 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
19 const ALLOWED_LINK_PROTOCOLS = new Set(['http:', 'https:', 'ftp:']); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
20 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
21 function encodeXML(str) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
22 return str.replace(/[<>&'"]/g, c => { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
23 switch (c) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
24 case '<': return '<'; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
25 case '>': return '>'; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
26 case '&': return '&'; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
27 case '\'': return '''; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
28 case '"': return '"'; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
29 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
30 }); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
31 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
32 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
33 function parseDate(s) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
34 let date = new Date(s); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
35 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
36 return isNaN(date) ? new Date(0) : date; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
37 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
38 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
39 function parseURL(text, baseURL = '') { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
40 let url; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
41 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
42 try { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
43 url = new URL(text, baseURL); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
44 } catch (e) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
45 return null; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
46 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
47 if (!ALLOWED_LINK_PROTOCOLS.has(url.protocol)) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
48 return null; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
49 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
50 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
51 return url; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
52 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
53 |
15
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
54 function base64Decode(base64Str) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
55 let encodedText; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
56 try { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
57 encodedText = atob(base64Str); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
58 } catch (e) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
59 throw (e instanceof DOMException) ? new TypeError(e.message) : e; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
60 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
61 let byteBuffer = new Uint8Array(new ArrayBuffer(encodedText.length)); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
62 for (let i = 0; i < encodedText.length; i++) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
63 byteBuffer[i] = encodedText.charCodeAt(i); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
64 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
65 return new TextDecoder().decode(byteBuffer); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
66 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
67 |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
68 function feedNSResolver(prefix) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
69 switch (prefix) { |
15
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
70 case 'atom03': |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
71 return XMLNS.ATOM03; |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
72 case 'atom': |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
73 return XMLNS.ATOM10; |
19
3fcd2209b39a
Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
18
diff
changeset
|
74 case 'rss09': |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
75 return XMLNS.RSS09; |
19
3fcd2209b39a
Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
18
diff
changeset
|
76 case 'rss10': |
3fcd2209b39a
Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
18
diff
changeset
|
77 return XMLNS.RSS10; |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
78 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
79 return null; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
80 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
81 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
82 function feedQueryXPath(feedDocument, scopeElement, xpathQuery) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
83 return feedDocument.evaluate(xpathQuery, scopeElement, feedNSResolver, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
84 XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
85 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
86 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
87 function feedQueryXPathAll(feedDocument, scopeElement, xpathQuery) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
88 let result = feedDocument.evaluate(xpathQuery, scopeElement, feedNSResolver, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
89 XPathResult.ORDERED_NODE_ITERATOR_TYPE, null); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
90 let nodes = []; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
91 for (let node = result.iterateNext(); node !== null; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
92 node = result.iterateNext()) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
93 nodes.push(node); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
94 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
95 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
96 return nodes; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
97 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
98 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
99 export class ParserError extends Error { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
100 constructor() { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
101 super(...arguments); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
102 this.name = this.constructor.name; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
103 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
104 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
105 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
106 export class UnsupportedFeedTypeError extends Error { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
107 constructor(message = 'Document is not a supported feed', ...params) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
108 super(message, ...params); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
109 this.name = this.constructor.name; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
110 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
111 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
112 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
113 export class ProtocolError extends Error { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
114 constructor(url, status, statusText, ...params) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
115 let message = `Protocol error: Transfer of ${url} failed with: ` + |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
116 `${status} ${statusText}` |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
117 super(message, ...params); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
118 this.name = this.constructor.name; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
119 this.url = url; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
120 this.status = status; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
121 this.statusText = statusText; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
122 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
123 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
124 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
125 class FeedLogo { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
126 constructor(url, {title = ''} = {}) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
127 this.url = url; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
128 this.title = title; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
129 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
130 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
131 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
132 class FeedEntryFile { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
133 constructor(url, {type = browser.i18n.getMessage('defaultFileType'), |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
134 size = 0} = {}) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
135 this.filename = undefined; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
136 this._url = undefined; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
137 this.url = url; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
138 this.type = type; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
139 this.size = size; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
140 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
141 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
142 set url(url) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
143 this._url = url; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
144 let filename = url.pathname.split('/').pop(); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
145 this.filename = filename !== '' ? filename : |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
146 browser.i18n.getMessage('defaultFileName'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
147 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
148 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
149 get url() { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
150 return this._url; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
151 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
152 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
153 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
154 class FeedEntry { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
155 constructor({title = browser.i18n.getMessage('defaultFeedEntryTitle'), |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
156 link = undefined, date = new Date(0), content = '', |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
157 files = []} = {}) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
158 this.title = title; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
159 this.link = link; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
160 this.date = date; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
161 this._content = undefined; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
162 this.content = content; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
163 this.files = files; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
164 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
165 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
166 normalizeContent(text) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
167 if (typeof text === 'undefined') { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
168 return |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
169 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
170 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
171 let contentDocument = document.implementation.createHTMLDocument(); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
172 let parsedDocument = new DOMParser().parseFromString(text, 'text/html'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
173 contentDocument.body = contentDocument.adoptNode(parsedDocument.body); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
174 return new XMLSerializer().serializeToString(contentDocument); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
175 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
176 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
177 set content(content) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
178 this._content = this.normalizeContent(content); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
179 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
180 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
181 get content() { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
182 return this._content; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
183 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
184 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
185 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
186 class Feed { |
31
76e23b361e92
Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
28
diff
changeset
|
187 constructor(url, type, {title = browser.i18n.getMessage('defaultFeedTitle'), |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
188 subtitle = '', logo, entries = []} = {}) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
189 this.url = url; |
31
76e23b361e92
Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
28
diff
changeset
|
190 this.type = type; |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
191 this.title = title; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
192 this.subtitle = subtitle; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
193 this.logo = logo; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
194 this.entries = entries; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
195 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
196 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
197 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
198 export class FeedParser { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
199 static probeFeed(feedDocument) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
200 let documentElement = feedDocument.documentElement; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
201 if (documentElement.nodeName === 'feed' && |
15
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
202 documentElement.namespaceURI === XMLNS.ATOM03) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
203 return ['atom', '0.3']; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
204 } else if (documentElement.nodeName === 'feed' && |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
205 documentElement.namespaceURI === XMLNS.ATOM10) { |
15
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
206 return ['atom', '1.0']; |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
207 } else if (documentElement.nodeName === 'rss') { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
208 let version = documentElement.getAttribute('version'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
209 switch (version) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
210 case '0.90': |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
211 case '0.91': |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
212 case '0.92': |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
213 case '0.93': |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
214 case '0.94': |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
215 case '2.0': |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
216 return ['rss', version]; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
217 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
218 } else if (documentElement.localName.toLowerCase() === 'rdf' && |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
219 documentElement.getAttribute('xmlns') === XMLNS.RSS09) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
220 return ['rss', '0.9']; |
19
3fcd2209b39a
Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
18
diff
changeset
|
221 } else if (documentElement.localName.toLowerCase() === 'rdf' && |
3fcd2209b39a
Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
18
diff
changeset
|
222 documentElement.getAttribute('xmlns') === XMLNS.RSS10) { |
3fcd2209b39a
Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
18
diff
changeset
|
223 return ['rss', '1.0']; |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
224 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
225 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
226 return [undefined, undefined]; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
227 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
228 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
229 constructor() { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
230 this.url = undefined; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
231 this.document = undefined; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
232 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
233 |
15
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
234 parseAtom03ContentConstruct(containerElement, textOnly = true) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
235 let contentType = containerElement.getAttribute('type'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
236 let contentMode = containerElement.getAttribute('mode'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
237 if (contentType === null) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
238 contentType = 'text/plain'; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
239 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
240 if (contentMode === null) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
241 contentMode = 'xml'; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
242 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
243 if (contentType === 'application/xhtml+xml') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
244 let htmlText; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
245 if (contentMode === 'xml') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
246 return textOnly ? containerElement.textContent.trim() : |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
247 containerElement.innerHTML; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
248 } else if (contentMode === 'escaped') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
249 htmlText = containerElement.textContent; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
250 } else if (contentMode === 'base64') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
251 htmlText = base64Decode(containerElement.textContent); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
252 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
253 if (typeof htmlText === 'undefined') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
254 return; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
255 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
256 if (textOnly) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
257 let htmlDocument = new DOMParser().parseFromString(htmlText, |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
258 'application/xhtml+xml'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
259 if (htmlDocument.documentElement.namespaceURI === |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
260 XMLNS.PARSERERROR) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
261 return; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
262 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
263 return htmlDocument.body.textContent.trim(); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
264 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
265 return htmlText; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
266 } else if (contentType === 'text/html') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
267 let htmlText; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
268 if (contentMode === 'escaped') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
269 htmlText = containerElement.textContent; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
270 } else if (contentMode === 'base64') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
271 htmlText = base64Decode(containerElement.textContent); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
272 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
273 if (typeof htmlText === 'undefined') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
274 return; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
275 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
276 if (textOnly) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
277 let htmlDocument = new DOMParser().parseFromString(htmlText, |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
278 'text/html'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
279 return htmlDocument.body.textContent.trim(); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
280 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
281 return htmlText; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
282 } else if (contentType === 'text/plain') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
283 let text; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
284 if (contentMode === 'escaped') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
285 text = containerElement.textContent; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
286 } else if (contentMode === 'base64') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
287 text = base64Decode(containerElement.textContent); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
288 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
289 if (typeof text === 'undefined') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
290 return; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
291 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
292 return textOnly ? text : `<pre>${encodeXML(text)}</pre>`; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
293 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
294 return; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
295 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
296 |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
297 parseAtom03Content(contentElement) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
298 // ordered from lowest to highest preference |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
299 const contentTypes = [ |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
300 'text/plain', |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
301 'text/html', |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
302 'application/xhtml+xml' |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
303 ]; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
304 if (contentElement.getAttribute('type') === 'multipart/alternative' && |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
305 contentElement.getAttribute('mode') === null) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
306 // select alternative according to above preference |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
307 let selectedTypeIndex = -1; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
308 let selectedElement; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
309 for (let innerContentElement of contentElement.children) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
310 if (innerContentElement.localName !== 'content' || |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
311 innerContentElement.namespaceURI !== XMLNS.ATOM03) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
312 throw new TypeError('child elements of a multipart ' + |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
313 ' content elements must be content elements'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
314 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
315 let innerContentType = innerContentElement.getAttribute('type'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
316 if (innerContentType === null) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
317 innerContentType = 'text/plain'; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
318 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
319 let typeIndex = contentTypes.indexOf(innerContentType); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
320 if (typeIndex > selectedTypeIndex) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
321 selectedTypeIndex = typeIndex; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
322 selectedElement = innerContentElement; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
323 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
324 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
325 if (selectedTypeIndex >= 0) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
326 contentElement = selectedElement; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
327 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
328 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
329 |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
330 return this.parseAtom03ContentConstruct(contentElement, false); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
331 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
332 |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
333 parseAtom03Entry(entryElement) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
334 let title; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
335 let link; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
336 let date; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
337 let content; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
338 let titleElement = feedQueryXPath(this.document, entryElement, |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
339 './atom03:title'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
340 if (titleElement !== null) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
341 title = titleElement.textContent.trim(); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
342 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
343 |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
344 let linkElement = feedQueryXPath(this.document, entryElement, |
28
104a1faebd43
Fix XPath query syntax
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
25
diff
changeset
|
345 './atom03:link[@href][contains(concat(" ", ' + |
104a1faebd43
Fix XPath query syntax
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
25
diff
changeset
|
346 'normalize-space(@rel), " "), " alternate ")]'); |
15
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
347 if (linkElement !== null) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
348 link = parseURL(linkElement.getAttribute('href'), this.url); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
349 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
350 |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
351 let modifiedElement = feedQueryXPath(this.document, entryElement, |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
352 './atom03:modified'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
353 if (modifiedElement !== null) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
354 date = parseDate(modifiedElement.textContent); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
355 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
356 |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
357 let contentElement = feedQueryXPath(this.document, entryElement, |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
358 './atom03:content'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
359 if (contentElement !== null) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
360 try { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
361 content = this.parseAtom03Content(contentElement); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
362 } catch (e) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
363 if (!(e instanceof TypeError)) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
364 throw e; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
365 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
366 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
367 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
368 if (typeof content === 'undefined') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
369 let summaryElement = feedQueryXPath(this.document, entryElement, |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
370 './atom03:summary'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
371 if (summaryElement !== null) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
372 try { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
373 content = this.parseAtom03ContentConstruct(summaryElement, |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
374 false); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
375 } catch (e) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
376 if (!(e instanceof TypeError)) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
377 throw e; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
378 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
379 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
380 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
381 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
382 |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
383 return new FeedEntry({title, link, date, content}); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
384 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
385 |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
386 parseAtom03Feed() { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
387 let title; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
388 let subtitle; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
389 let logo; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
390 let entries = []; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
391 let documentElement = this.document.documentElement; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
392 |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
393 let titleElement = feedQueryXPath(this.document, documentElement, |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
394 './atom03:title'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
395 if (titleElement !== null) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
396 try { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
397 title = this.parseAtom03ContentConstruct(titleElement); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
398 } catch (e) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
399 if (!(e instanceof TypeError)) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
400 throw e; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
401 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
402 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
403 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
404 |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
405 let taglineElement = feedQueryXPath(this.document, documentElement, |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
406 './atom03:tagline'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
407 if (taglineElement !== null) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
408 try { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
409 title = this.parseAtom03ContentConstruct(taglineElement); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
410 } catch (e) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
411 if (!(e instanceof TypeError)) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
412 throw e; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
413 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
414 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
415 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
416 |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
417 let logoElement = feedQueryXPath(this.document, documentElement, |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
418 './atom03:logo'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
419 if (logoElement !== null) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
420 try { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
421 logo = this.parseAtomLogo(logoElement); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
422 } catch (e) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
423 if (!(e instanceof TypeError)) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
424 throw e; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
425 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
426 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
427 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
428 |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
429 let entryElements = feedQueryXPathAll(this.document, documentElement, |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
430 './atom03:entry'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
431 for (let entryElement of entryElements) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
432 entries.push(this.parseAtom03Entry(entryElement)); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
433 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
434 |
31
76e23b361e92
Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
28
diff
changeset
|
435 return new Feed(this.url, 'application/atom+xml', |
76e23b361e92
Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
28
diff
changeset
|
436 {title, subtitle, logo, entries}); |
15
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
437 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
438 |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
439 parseAtomLogo(logoElement) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
440 let url = parseURL(logoElement.textContent.trim(), this.url); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
441 if (url === null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
442 throw new TypeError('invalid URL in <logo> element'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
443 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
444 return new FeedLogo(url); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
445 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
446 |
14
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
447 parseAtomTextConstruct(containerElement, textOnly = true) { |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
448 let contentType = containerElement.getAttribute('type'); |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
449 if (contentType === null) { |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
450 contentType = 'text'; |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
451 } |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
452 |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
453 if (contentType === 'xhtml') { |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
454 let xhtmlRootElement = containerElement.firstElementChild; |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
455 if (xhtmlRootElement !== null && |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
456 xhtmlRootElement.localName === 'div' && |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
457 xhtmlRootElement.namespaceURI === XMLNS.XHTML) { |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
458 return textOnly ? xhtmlRootElement.textContent.trim() : |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
459 xhtmlRootElement.innerHTML; |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
460 } |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
461 } else if (contentType === 'html') { |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
462 let htmlText = containerElement.textContent; |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
463 if (textOnly) { |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
464 let htmlDocument = new DOMParser().parseFromString(htmlText, |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
465 'text/html'); |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
466 return htmlDocument.body.textContent.trim(); |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
467 } |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
468 return htmlText |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
469 } else if (contentType === 'text') { |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
470 let text = containerElement.textContent.trim(); |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
471 return textOnly ? text : `<pre>${encodeXML(text)}</pre>`; |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
472 } |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
473 |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
474 // unsupported content type |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
475 return; |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
476 } |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
477 |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
478 parseAtomContent(contentElement) { |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
479 let contentSrc = contentElement.getAttribute('src'); |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
480 if (contentSrc !== null) { |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
481 // externally referenced content is not supported |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
482 return; |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
483 } |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
484 return this.parseAtomTextConstruct(contentElement, false); |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
485 } |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
486 |
17
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
487 parseAtomEntryFile(enclosureElement) { |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
488 let type; |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
489 let size; |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
490 let url = parseURL(enclosureElement.getAttribute('href'), this.url); |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
491 if (url === null) { |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
492 throw new TypeError('invalid URL in enclosure href attribute'); |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
493 } |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
494 |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
495 let typeAttribute = enclosureElement.getAttribute('type'); |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
496 if (typeAttribute !== null) { |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
497 type = typeAttribute; |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
498 } |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
499 |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
500 let length = parseInt(enclosureElement.getAttribute('length'), 10); |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
501 if (!isNaN(length)) { |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
502 size = length; |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
503 } |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
504 |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
505 return new FeedEntryFile(url, {type, size}); |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
506 } |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
507 |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
508 parseAtomEntry(entryElement) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
509 let title; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
510 let link; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
511 let date; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
512 let content; |
17
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
513 let files = []; |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
514 let titleElement = feedQueryXPath(this.document, entryElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
515 './atom:title'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
516 if (titleElement !== null) { |
14
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
517 title = this.parseAtomTextConstruct(titleElement); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
518 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
519 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
520 let linkElement = feedQueryXPath(this.document, entryElement, |
28
104a1faebd43
Fix XPath query syntax
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
25
diff
changeset
|
521 './atom:link[@href][not(@rel) or contains(concat(" ", ' + |
104a1faebd43
Fix XPath query syntax
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
25
diff
changeset
|
522 'normalize-space(@rel), " "), " alternate ")]'); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
523 if (linkElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
524 link = parseURL(linkElement.getAttribute('href'), this.url); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
525 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
526 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
527 let updatedElement = feedQueryXPath(this.document, entryElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
528 './atom:updated'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
529 if (updatedElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
530 date = parseDate(updatedElement.textContent); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
531 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
532 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
533 let contentElement = feedQueryXPath(this.document, entryElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
534 './atom:content'); |
14
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
535 if (contentElement !== null) { |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
536 content = this.parseAtomContent(contentElement); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
537 } |
14
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
538 if (typeof content === 'undefined') { |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
539 let summaryElement = feedQueryXPath(this.document, entryElement, |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
540 './atom:summary'); |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
541 if (summaryElement !== null) { |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
542 content = this.parseAtomTextConstruct(summaryElement, false); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
543 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
544 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
545 |
17
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
546 for (let enclosureElement of feedQueryXPathAll(this.document, |
28
104a1faebd43
Fix XPath query syntax
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
25
diff
changeset
|
547 entryElement, './atom:link[@href]' + |
104a1faebd43
Fix XPath query syntax
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
25
diff
changeset
|
548 '[contains(concat(" ", normalize-space(@rel), " "), ' + |
104a1faebd43
Fix XPath query syntax
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
25
diff
changeset
|
549 '" enclosure ")]')) { |
17
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
550 try { |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
551 let entryFile = this.parseAtomEntryFile(enclosureElement); |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
552 files.push(entryFile); |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
553 } catch (e) { |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
554 if (!(e instanceof TypeError)) { |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
555 throw e; |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
556 } |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
557 } |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
558 } |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
559 |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
560 return new FeedEntry({title, link, date, content, files}); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
561 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
562 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
563 parseAtomFeed() { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
564 let title; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
565 let subtitle; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
566 let logo; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
567 let entries = []; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
568 let documentElement = this.document.documentElement; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
569 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
570 let titleElement = feedQueryXPath(this.document, documentElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
571 './atom:title'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
572 if (titleElement !== null) { |
14
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
573 title = this.parseAtomTextConstruct(titleElement); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
574 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
575 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
576 let subtitleElement = feedQueryXPath(this.document, documentElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
577 './atom:subtitle'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
578 if (subtitleElement !== null) { |
14
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
579 subtitle = this.parseAtomTextConstruct(subtitleElement); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
580 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
581 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
582 let logoElement = feedQueryXPath(this.document, documentElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
583 './atom:logo'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
584 if (logoElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
585 try { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
586 logo = this.parseAtomLogo(logoElement); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
587 } catch (e) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
588 if (!(e instanceof TypeError)) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
589 throw e; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
590 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
591 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
592 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
593 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
594 let entryElements = feedQueryXPathAll(this.document, documentElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
595 './atom:entry'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
596 for (let entryElement of entryElements) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
597 entries.push(this.parseAtomEntry(entryElement)); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
598 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
599 |
31
76e23b361e92
Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
28
diff
changeset
|
600 return new Feed(this.url, 'application/atom+xml', |
76e23b361e92
Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
28
diff
changeset
|
601 {title, subtitle, logo, entries}); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
602 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
603 |
19
3fcd2209b39a
Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
18
diff
changeset
|
604 parseRSS1Logo(imageElement, nsPrefix) { |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
605 let title; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
606 let urlElement = feedQueryXPath(this.document, imageElement, |
19
3fcd2209b39a
Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
18
diff
changeset
|
607 `./${nsPrefix}:url`); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
608 if (urlElement === null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
609 throw new TypeError('missing <url> element in <logo> element'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
610 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
611 let url = parseURL(urlElement.textContent.trim(), this.url); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
612 if (url === null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
613 throw new TypeError('invalid URL in <logo> element'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
614 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
615 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
616 let titleElement = feedQueryXPath(this.document, imageElement, |
19
3fcd2209b39a
Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
18
diff
changeset
|
617 `./${nsPrefix}:title`); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
618 if (titleElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
619 title = titleElement.textContent.trim(); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
620 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
621 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
622 return new FeedLogo(url, {title}); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
623 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
624 |
19
3fcd2209b39a
Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
18
diff
changeset
|
625 parseRSS1Entry(itemElement, nsPrefix) { |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
626 let title; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
627 let link; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
628 let titleElement = feedQueryXPath(this.document, itemElement, |
19
3fcd2209b39a
Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
18
diff
changeset
|
629 `./${nsPrefix}:title`); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
630 if (titleElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
631 title = titleElement.textContent; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
632 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
633 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
634 let linkElement = feedQueryXPath(this.document, itemElement, |
19
3fcd2209b39a
Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
18
diff
changeset
|
635 `./${nsPrefix}:link`); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
636 if (linkElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
637 link = parseURL(linkElement.textContent, this.url); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
638 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
639 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
640 return new FeedEntry({title, link}); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
641 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
642 |
19
3fcd2209b39a
Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
18
diff
changeset
|
643 parseRSS1Feed(version) { |
3fcd2209b39a
Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
18
diff
changeset
|
644 let nsPrefix = version === '0.9' ? 'rss09' : 'rss10'; |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
645 let title; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
646 let subtitle; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
647 let logo; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
648 let entries = []; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
649 let documentElement = this.document.documentElement; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
650 let titleElement = feedQueryXPath(this.document, documentElement, |
19
3fcd2209b39a
Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
18
diff
changeset
|
651 `./${nsPrefix}:channel/${nsPrefix}:title`); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
652 if (titleElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
653 title = titleElement.textContent; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
654 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
655 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
656 let descriptionElement = feedQueryXPath(this.document, documentElement, |
19
3fcd2209b39a
Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
18
diff
changeset
|
657 `./${nsPrefix}:channel/${nsPrefix}:description`); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
658 if (descriptionElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
659 subtitle = descriptionElement.textContent; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
660 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
661 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
662 let imageElement = feedQueryXPath(this.document, documentElement, |
19
3fcd2209b39a
Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
18
diff
changeset
|
663 `./${nsPrefix}:image`); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
664 if (imageElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
665 try { |
19
3fcd2209b39a
Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
18
diff
changeset
|
666 logo = this.parseRSS1Logo(imageElement, nsPrefix); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
667 } catch (e) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
668 if (!(e instanceof TypeError)) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
669 throw e; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
670 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
671 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
672 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
673 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
674 let itemElements = feedQueryXPathAll(this.document, documentElement, |
19
3fcd2209b39a
Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
18
diff
changeset
|
675 `./${nsPrefix}:item`); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
676 for (let itemElement of itemElements) { |
19
3fcd2209b39a
Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
18
diff
changeset
|
677 let entry = this.parseRSS1Entry(itemElement, nsPrefix); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
678 if (typeof entry !== 'undefined') { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
679 entries.push(entry); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
680 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
681 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
682 |
31
76e23b361e92
Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
28
diff
changeset
|
683 return new Feed(this.url, 'application/rss+xml', |
76e23b361e92
Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
28
diff
changeset
|
684 {title, subtitle, logo, entries}); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
685 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
686 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
687 parseRSS2Logo(imageElement) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
688 let title; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
689 let urlElement = feedQueryXPath(this.document, imageElement, './url'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
690 if (urlElement === null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
691 throw new TypeError('missing <url> element in <logo> element'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
692 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
693 let url = parseURL(urlElement.textContent.trim(), this.url); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
694 if (url === null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
695 throw new TypeError('invalid URL in <logo> element'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
696 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
697 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
698 let titleElement = feedQueryXPath(this.document, imageElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
699 './title'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
700 if (titleElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
701 title = titleElement.textContent.trim(); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
702 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
703 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
704 return new FeedLogo(url, {title}); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
705 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
706 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
707 parseRSS2EntryFile(enclosureElement) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
708 let type; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
709 let size; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
710 let url = parseURL(enclosureElement.getAttribute('url'), this.url); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
711 if (url === null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
712 throw new TypeError('invalid URL in <enclosure> element'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
713 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
714 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
715 let typeAttribute = enclosureElement.getAttribute('type'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
716 if (typeAttribute !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
717 type = typeAttribute; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
718 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
719 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
720 let length = parseInt(enclosureElement.getAttribute('length'), |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
721 10); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
722 if (!isNaN(length)) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
723 size = length; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
724 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
725 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
726 return new FeedEntryFile(url, {type, size}); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
727 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
728 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
729 parseRSS2Entry(itemElement) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
730 let title; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
731 let link; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
732 let date; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
733 let content; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
734 let files = []; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
735 let titleElement = feedQueryXPath(this.document, itemElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
736 './title'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
737 if (titleElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
738 title = titleElement.textContent; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
739 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
740 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
741 let linkElement = feedQueryXPath(this.document, itemElement, './link'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
742 if (linkElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
743 link = parseURL(linkElement.textContent, this.url); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
744 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
745 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
746 let pubDateElement = feedQueryXPath(this.document, itemElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
747 './pubDate'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
748 if (pubDateElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
749 date = parseDate(pubDateElement.textContent); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
750 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
751 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
752 let descriptionElement = feedQueryXPath(this.document, itemElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
753 './description'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
754 if (descriptionElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
755 content = descriptionElement.textContent.trim(); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
756 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
757 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
758 for (let enclosureElement of |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
759 feedQueryXPathAll(this.document, itemElement, './enclosure')) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
760 try { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
761 let entryFile = this.parseRSS2EntryFile(enclosureElement); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
762 files.push(entryFile); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
763 } catch (e) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
764 if (!(e instanceof TypeError)) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
765 throw e; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
766 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
767 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
768 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
769 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
770 return new FeedEntry({title, link, date, content, files}); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
771 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
772 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
773 parseRSS2Feed() { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
774 let title; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
775 let subtitle; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
776 let logo; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
777 let entries = []; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
778 let documentElement = this.document.documentElement; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
779 let titleElement = feedQueryXPath(this.document, documentElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
780 './channel/title'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
781 if (titleElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
782 title = titleElement.textContent; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
783 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
784 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
785 let descriptionElement = feedQueryXPath(this.document, documentElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
786 './channel/description'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
787 if (descriptionElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
788 subtitle = descriptionElement.textContent; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
789 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
790 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
791 let imageElement = feedQueryXPath(this.document, documentElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
792 './channel/image'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
793 if (imageElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
794 try { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
795 logo = this.parseRSS2Logo(imageElement); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
796 } catch (e) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
797 if (!(e instanceof TypeError)) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
798 throw e; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
799 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
800 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
801 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
802 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
803 let itemElements = feedQueryXPathAll(this.document, documentElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
804 './channel/item'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
805 for (let itemElement of itemElements) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
806 let entry = this.parseRSS2Entry(itemElement); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
807 if (typeof entry !== 'undefined') { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
808 entries.push(entry); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
809 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
810 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
811 |
31
76e23b361e92
Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
28
diff
changeset
|
812 return new Feed(this.url, 'application/rss+xml', |
76e23b361e92
Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
28
diff
changeset
|
813 {title, subtitle, logo, entries}); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
814 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
815 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
816 parseFromString(xmlString, url) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
817 this.url = url; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
818 this.document = new DOMParser().parseFromString(xmlString, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
819 'application/xml'); |
15
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
820 if (this.document.documentElement.namespaceURI === XMLNS.PARSERERROR) { |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
821 throw new ParserError(this.document.documentElement.textContent); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
822 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
823 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
824 let [type, version] = this.constructor.probeFeed(this.document); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
825 if (type === 'atom') { |
15
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
826 if (version === '0.3') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
827 return this.parseAtom03Feed(); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
828 } else if (version === '1.0') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
829 return this.parseAtomFeed(); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
830 } |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
831 } else if (type === 'rss') { |
19
3fcd2209b39a
Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
18
diff
changeset
|
832 if (version === '0.9' || version === '1.0') { |
3fcd2209b39a
Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
18
diff
changeset
|
833 return this.parseRSS1Feed(version); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
834 } else { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
835 return this.parseRSS2Feed(); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
836 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
837 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
838 throw new UnsupportedFeedTypeError(); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
839 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
840 } |