Mercurial > addons > firefox-addons > feed-preview
annotate content_scripts/feed-probe.js @ 54:ede87e1004f9
Fix issues with feed detection
Query the feed probe content script for available feeds from the background
script instead of making the content script message the background script.
This solves a race condition between the message from the content script
sending any feeds associated with the current document and the tab's status
"complete" event signaling that a new document has been loaded and hiding
the page action. Sometimes that event would be triggered after the message
from the content script and thus hide the page action again. In addition,
navigating back to a previously visited page might not cause a reload which
means that the content script would not send a message if there were feeds
associated with the current document.
author | Guido Berhoerster <guido+feed-preview@berhoerster.name> |
---|---|
date | Thu, 26 Sep 2019 23:11:18 +0200 |
parents | 76e23b361e92 |
children |
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 |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
11 function getFeeds() { |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
12 let urlsFeeds = new Map(); |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
13 let elements = document.querySelectorAll(':-moz-any(link, a)[href]' + |
25
da483ce3832d
Fix selector for rel attributes
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
0
diff
changeset
|
14 '[rel~=alternate]:-moz-any([type="application/atom+xml"], ' + |
0
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
15 '[type="application/rss+xml"])'); |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
16 |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
17 for (let element of elements) { |
31
76e23b361e92
Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
25
diff
changeset
|
18 if (element.href === document.URL) { |
76e23b361e92
Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
25
diff
changeset
|
19 // do not indicate the availability of a feed if the current |
76e23b361e92
Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
25
diff
changeset
|
20 // document is already a feed preview |
76e23b361e92
Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
25
diff
changeset
|
21 continue; |
76e23b361e92
Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
25
diff
changeset
|
22 } |
76e23b361e92
Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
25
diff
changeset
|
23 |
0
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
24 if (!element.href.match(/^https?:\/\//)) { |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
25 continue; |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
26 } |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
27 |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
28 urlsFeeds.set(element.href, { |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
29 href: element.href, |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
30 title: element.title || browser.i18n.getMessage('defaultFeedTitle'), |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
31 type: element.type |
54
ede87e1004f9
Fix issues with feed detection
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
31
diff
changeset
|
32 }); |
0
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
33 } |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
34 |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
35 return Array.from(urlsFeeds.values()); |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
36 } |
bc5cc170163c
Initial revision
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
37 |
54
ede87e1004f9
Fix issues with feed detection
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
31
diff
changeset
|
38 browser.runtime.onMessage.addListener((request, sender, sendResponse) => { |
ede87e1004f9
Fix issues with feed detection
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
31
diff
changeset
|
39 // background page querying available feeds |
ede87e1004f9
Fix issues with feed detection
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
31
diff
changeset
|
40 sendResponse(getFeeds()); |
ede87e1004f9
Fix issues with feed detection
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
31
diff
changeset
|
41 }); |