Mercurial > addons > firefox-addons > feed-preview
annotate js/background.js @ 12:1e9aa12d7306 version-2
Release version 2
author | Guido Berhoerster <guido+feed-preview@berhoerster.name> |
---|---|
date | Sat, 08 Dec 2018 12:10:03 +0100 |
parents | a4590add4901 |
children | 376a0e415bba |
rev | line source |
---|---|
0
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
1 /* |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
2 * Copyright (C) 2018 Guido Berhoerster <guido+feed-preview@berhoerster.name> |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
3 * |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
4 * This Source Code Form is subject to the terms of the Mozilla Public |
bc5cc170163c
Initial revision
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 |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
7 */ |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
8 |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
9 'use strict'; |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
10 |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
11 import * as feedParser from './feed-parser.js'; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
12 import {renderFeedPreview} from './feed-preview.js'; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
13 |
11
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
14 const FEED_READERS_PRESET = [ |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
15 { |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
16 title: 'Feedly', |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
17 urlTemplate: 'https://feedly.com/#subscription/feed/%s' |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
18 }, |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
19 { |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
20 title: 'FlowReader', |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
21 urlTemplate: 'https://www.flowreader.com/subscribe?url=%s' |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
22 }, |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
23 { |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
24 title: 'InoReader', |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
25 urlTemplate: 'https://www.inoreader.com/feed/%s' |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
26 }, |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
27 { |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
28 title: 'Kouio', |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
29 urlTemplate: 'https://kouio.com/subscribe?url=%s' |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
30 }, |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
31 { |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
32 title: 'My Yahoo', |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
33 urlTemplate: 'https://add.my.yahoo.com/rss?url=%s' |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
34 }, |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
35 { |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
36 title: 'Netvibes', |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
37 urlTemplate: 'https://www.netvibes.com/subscribe.php?url=%s' |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
38 }, |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
39 { |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
40 title: 'NewsBlur', |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
41 urlTemplate: 'https://www.newsblur.com/?url=%s' |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
42 }, |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
43 { |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
44 title: 'The Old Reader', |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
45 urlTemplate: 'https://theoldreader.com/feeds/subscribe?url=%s' |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
46 } |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
47 ]; |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
48 const FEED_MAGIC = [ |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
49 '<rss', |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
50 '<feed', |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
51 ...Object.values(feedParser.XMLNS) |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
52 ]; |
0
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
53 var tabsFeeds = new Map(); |
10
ff5e5e3eba32
Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
6
diff
changeset
|
54 var tabsFeedPreviews = new Map(); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
55 var fetchingFeedPreview = fetch('web_resources/feed-preview.xhtml') |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
56 .then(response => response.text()); |
0
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
57 |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
58 function parseContentType(header) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
59 let contentType = { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
60 mediaType: '', |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
61 charset: 'utf-8' |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
62 }; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
63 let parts = header.toLowerCase().split(';'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
64 contentType.mediaType = parts.shift().trim(); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
65 for (let parameter of parts) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
66 let [, name, value, ] = parameter.trim().split(/([^=]+)="?([^"]*)"?/); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
67 if (name.toLowerCase() === 'charset') { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
68 contentType.charset = value.toLowerCase(); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
69 break; |
0
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
70 } |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
71 } |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
72 |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
73 return contentType; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
74 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
75 |
10
ff5e5e3eba32
Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
6
diff
changeset
|
76 async function handleFeed(inputText, tabId, url) { |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
77 // fast-path: eliminate XML documents which cannot be Atom nor RSS feeds |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
78 let inputTextStart = inputText.substring(0, 512); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
79 if (!FEED_MAGIC.some(element => inputTextStart.includes(element))) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
80 return inputText; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
81 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
82 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
83 let feed; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
84 try { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
85 feed = (new feedParser.FeedParser).parseFromString(inputText, url); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
86 } catch (e) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
87 if (e instanceof feedParser.ParserError || |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
88 e instanceof feedParser.UnsupportedFeedTypeError) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
89 // let the browser deal with non-well formed XML or XML documents |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
90 // which are not supported Atom or RSS feeds |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
91 return inputText; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
92 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
93 throw e; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
94 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
95 console.log(`parsed feed ${url}:\n`, feed); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
96 |
10
ff5e5e3eba32
Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
6
diff
changeset
|
97 // mark this feed preview for content script injection |
ff5e5e3eba32
Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
6
diff
changeset
|
98 tabsFeedPreviews.set(tabId, url); |
ff5e5e3eba32
Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
6
diff
changeset
|
99 |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
100 // render the preview document |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
101 let feedPreviewDocument = new DOMParser() |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
102 .parseFromString(await fetchingFeedPreview, 'text/html'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
103 renderFeedPreview(feedPreviewDocument, feed); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
104 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
105 return new XMLSerializer().serializeToString(feedPreviewDocument); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
106 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
107 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
108 browser.webRequest.onHeadersReceived.addListener(details => { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
109 if (details.statusCode !== 200) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
110 return {}; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
111 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
112 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
113 let contentTypeIndex = details.responseHeaders.findIndex(header => |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
114 header.name.toLowerCase() === 'content-type' && |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
115 typeof header.value !== 'undefined'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
116 if (contentTypeIndex < 0) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
117 // no Content-Type header found |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
118 return {}; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
119 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
120 let headerValue = details.responseHeaders[contentTypeIndex].value |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
121 let contentType = parseContentType(headerValue); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
122 // until content handlers become available to webextensions |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
123 // (https://bugzilla.mozilla.org/show_bug.cgi?id=1457500) intercept all |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
124 // responses and change the content type from application/atom+xml or |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
125 // application/rss+xml to application/xml which will then be probed for |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
126 // Atom or RSS content |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
127 switch (contentType.mediaType) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
128 case 'application/atom+xml': |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
129 case 'application/rss+xml': |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
130 case 'application/rdf+xml': |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
131 case 'application/xml': |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
132 break; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
133 default: |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
134 // non-XML media type |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
135 return {}; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
136 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
137 console.log(`response is an XML document\n`, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
138 `media type: ${contentType.mediaType}\n`, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
139 `charset: ${contentType.charset}`); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
140 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
141 let decoder; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
142 try { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
143 decoder = new TextDecoder(contentType.charset); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
144 } catch (e) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
145 if (e instanceof RangeError) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
146 // unsupported charset |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
147 return {}; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
148 } else { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
149 throw e; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
150 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
151 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
152 let encoder = new TextEncoder(); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
153 let inputText = ''; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
154 let filter = browser.webRequest.filterResponseData(details.requestId); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
155 filter.addEventListener('data', ev => { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
156 inputText += decoder.decode(ev.data, {stream: true}); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
157 }); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
158 filter.addEventListener('stop', async ev => { |
10
ff5e5e3eba32
Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
6
diff
changeset
|
159 let result = await handleFeed(inputText, details.tabId, details.url); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
160 filter.write(encoder.encode(result)); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
161 filter.close(); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
162 }); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
163 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
164 details.responseHeaders[contentTypeIndex] = { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
165 name: 'Content-Type', |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
166 value: `application/xml;charset=${contentType.charset}` |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
167 }; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
168 |
0
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
169 return {responseHeaders: details.responseHeaders}; |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
170 }, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
171 {urls: ['http://*/*', 'https://*/*'], types: ['main_frame']}, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
172 ['blocking', 'responseHeaders']); |
0
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
173 |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
174 browser.runtime.onMessage.addListener((request, sender, sendResponse) => { |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
175 let tab = sender.tab; |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
176 if (typeof tab !== 'undefined') { |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
177 // content script sending feeds |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
178 tabsFeeds.set(tab.id, request); |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
179 browser.pageAction.show(tab.id); |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
180 } else { |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
181 // popup querying feeds |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
182 sendResponse(tabsFeeds.get(request)); |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
183 } |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
184 }); |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
185 |
10
ff5e5e3eba32
Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
6
diff
changeset
|
186 browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { |
0
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
187 if (typeof changeInfo.url === 'undefined') { |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
188 // filter out updates which do not change the URL |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
189 return; |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
190 } |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
191 |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
192 // hide the page action when the URL changes since it is no longer valid, |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
193 // it will be shown again if the content script detects a feed |
10
ff5e5e3eba32
Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
6
diff
changeset
|
194 browser.pageAction.hide(tabId); |
ff5e5e3eba32
Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
6
diff
changeset
|
195 |
ff5e5e3eba32
Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
6
diff
changeset
|
196 // inject content script once if the requested URL is a feed preview |
ff5e5e3eba32
Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
6
diff
changeset
|
197 if (tabsFeedPreviews.get(tabId) === changeInfo.url) { |
ff5e5e3eba32
Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
6
diff
changeset
|
198 browser.tabs.executeScript(tabId, { |
ff5e5e3eba32
Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
6
diff
changeset
|
199 file: 'content_scripts/feed-readers.js' |
ff5e5e3eba32
Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
6
diff
changeset
|
200 }); |
ff5e5e3eba32
Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
6
diff
changeset
|
201 tabsFeedPreviews.delete(tabId); |
ff5e5e3eba32
Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
6
diff
changeset
|
202 } |
0
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
203 }); |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
204 |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
205 browser.tabs.onRemoved.addListener((tabId, removeInfo) => { |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
206 tabsFeeds.delete(tabId); |
10
ff5e5e3eba32
Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
6
diff
changeset
|
207 tabsFeedPreviews.delete(tabId); |
0
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
208 }); |
11
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
209 |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
210 browser.runtime.onInstalled.addListener(async details => { |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
211 if (details.reason === 'install' || |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
212 (details.reason === 'update' && details.previousVersion < 2)) { |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
213 let {feedReaders = []} = await browser.storage.sync.get('feedReaders'); |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
214 let feedReadersSet = |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
215 new Set(feedReaders.map(feedReader => feedReader.urlTemplate)); |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
216 for (let feedReader of FEED_READERS_PRESET) { |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
217 if (!feedReadersSet.has(feedReader.urlTemplate)) { |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
218 feedReaders.push(feedReader); |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
219 } |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
220 } |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
221 console.log('set feedReaders to', feedReaders); |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
222 browser.storage.sync.set({feedReaders}); |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
223 } |
a4590add4901
Add preset with common web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
10
diff
changeset
|
224 }); |