Mercurial > addons > firefox-addons > feed-preview
comparison content_scripts/feed-probe.js @ 0:bc5cc170163c
Initial revision
author | Guido Berhoerster <guido+feed-preview@berhoerster.name> |
---|---|
date | Wed, 03 Oct 2018 23:40:57 +0200 |
parents | |
children | da483ce3832d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:bc5cc170163c |
---|---|
1 /* | |
2 * Copyright (C) 2018 Guido Berhoerster <guido+feed-preview@berhoerster.name> | |
3 * | |
4 * This Source Code Form is subject to the terms of the Mozilla Public | |
5 * License, v. 2.0. If a copy of the MPL was not distributed with this | |
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
7 */ | |
8 | |
9 'use strict'; | |
10 | |
11 const TIMEOUT_MAX = 800; // ms | |
12 | |
13 function getFeeds() { | |
14 let urlsFeeds = new Map(); | |
15 let elements = document.querySelectorAll(':-moz-any(link, a)[href]' + | |
16 '[rel=alternate]:-moz-any([type="application/atom+xml"], ' + | |
17 '[type="application/rss+xml"])'); | |
18 | |
19 for (let element of elements) { | |
20 if (!element.href.match(/^https?:\/\//)) { | |
21 continue; | |
22 } | |
23 | |
24 urlsFeeds.set(element.href, { | |
25 href: element.href, | |
26 title: element.title || browser.i18n.getMessage('defaultFeedTitle'), | |
27 type: element.type | |
28 }) | |
29 } | |
30 | |
31 return Array.from(urlsFeeds.values()); | |
32 } | |
33 | |
34 function probeLinkedFeeds() { | |
35 if (document.documentElement.nodeName.toUpperCase() !== 'HTML') { | |
36 return; | |
37 } | |
38 | |
39 let feeds = getFeeds(); | |
40 if (feeds.length === 0) { | |
41 return; | |
42 } | |
43 | |
44 // the listener on the background page might not be ready, keep trying to | |
45 // send the message with an exponential backoff until TIMEOUT_MAX is | |
46 // reached | |
47 let timeout = 0; | |
48 let sendFeeds = () => { | |
49 browser.runtime.sendMessage(feeds).catch(e => { | |
50 timeout = (timeout > 0) ? timeout * 2 : 100; | |
51 if (timeout > TIMEOUT_MAX) { | |
52 console.log(`Error: failed to message the background page: ` + | |
53 ` ${e.message}`); | |
54 return; | |
55 } | |
56 setTimeout(sendFeeds, timeout); | |
57 }); | |
58 }; | |
59 setTimeout(sendFeeds, timeout); | |
60 } | |
61 | |
62 probeLinkedFeeds(); |