comparison background.js @ 0:bc5cc170163c

Initial revision
author Guido Berhoerster <guido+feed-preview@berhoerster.name>
date Wed, 03 Oct 2018 23:40:57 +0200
parents
children
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 var tabsFeeds = new Map();
12
13 // until content handlers become available to webextensions
14 // (https://bugzilla.mozilla.org/show_bug.cgi?id=1457500) intercept all
15 // responses and change the content type from application/atom+xml or
16 // application/rss+xml to application/xml which will then be handled by a
17 // content script
18 browser.webRequest.onHeadersReceived.addListener(details => {
19 if (details.statusCode != 200 ||
20 typeof details.responseHeaders === 'undefined') {
21 return;
22 }
23
24 let contentTypeHeader = details.responseHeaders.find(element => {
25 return element.name.toLowerCase() === 'content-type';
26 });
27 if (typeof contentTypeHeader !== 'undefined') {
28 let contentType = contentTypeHeader.value.split(';');
29 let mediaType = contentType[0].trim().toLowerCase();
30 if (mediaType === 'application/atom+xml' ||
31 mediaType === 'application/rss+xml') {
32 contentType[0] = 'application/xml';
33 contentTypeHeader.value = contentType.join(';');
34 }
35 }
36
37 return {responseHeaders: details.responseHeaders};
38 }, {urls: ['http://*/*', 'https://*/*'], types: ['main_frame']},
39 ['blocking', 'responseHeaders']);
40
41 browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
42 let tab = sender.tab;
43 if (typeof tab !== 'undefined') {
44 // content script sending feeds
45 tabsFeeds.set(tab.id, request);
46 browser.pageAction.show(tab.id);
47 } else {
48 let response = tabsFeeds.get(request);
49 // popup querying feeds
50 sendResponse(tabsFeeds.get(request));
51 }
52 });
53
54 browser.tabs.onUpdated.addListener((id, changeInfo, tab) => {
55 if (typeof changeInfo.url === 'undefined') {
56 // filter out updates which do not change the URL
57 return;
58 }
59
60 // hide the page action when the URL changes since it is no longer valid,
61 // it will be shown again if the content script detects a feed
62 browser.pageAction.hide(tab.id);
63 });
64
65 browser.tabs.onRemoved.addListener((tabId, removeInfo) => {
66 tabsFeeds.delete(tabId);
67 });