diff js/background.js @ 6:5d7c13e998e9

Create feed previews using a stream filter Instead of replacing the feed document with an XHTML preview from a content script after it has already been rendered, create an XHTML preview using a stream filter before it is passed into the rendering engine and use an XSL style sheet to convert it to HTML. This has two advantages, firstly it results in an HTMLDocument with the full HTML DOM available and secondly it avoids rendering the document twice. Refactor the feed preview creation and split parsing and rendering into seperate modules.
author Guido Berhoerster <guido+feed-preview@berhoerster.name>
date Thu, 08 Nov 2018 16:30:34 +0100
parents background.js@bc5cc170163c
children ff5e5e3eba32
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/js/background.js	Thu Nov 08 16:30:34 2018 +0100
@@ -0,0 +1,161 @@
+/*
+ * Copyright (C) 2018 Guido Berhoerster <guido+feed-preview@berhoerster.name>
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+'use strict';
+
+import * as feedParser from './feed-parser.js';
+import {renderFeedPreview} from './feed-preview.js';
+
+const FEED_MAGIC = [
+    '<rss',
+    '<feed',
+    ...Object.values(feedParser.XMLNS)
+];
+var tabsFeeds = new Map();
+var fetchingFeedPreview = fetch('web_resources/feed-preview.xhtml')
+        .then(response => response.text());
+
+function parseContentType(header) {
+    let contentType = {
+        mediaType: '',
+        charset: 'utf-8'
+    };
+    let parts = header.toLowerCase().split(';');
+    contentType.mediaType = parts.shift().trim();
+    for (let parameter of parts) {
+        let [, name, value, ] = parameter.trim().split(/([^=]+)="?([^"]*)"?/);
+        if (name.toLowerCase() === 'charset') {
+            contentType.charset = value.toLowerCase();
+            break;
+        }
+    }
+
+    return contentType;
+}
+
+async function handleFeed(inputText, url) {
+    // fast-path: eliminate XML documents which cannot be Atom nor RSS feeds
+    let inputTextStart = inputText.substring(0, 512);
+    if (!FEED_MAGIC.some(element => inputTextStart.includes(element))) {
+        return inputText;
+    }
+
+    let feed;
+    try {
+        feed = (new feedParser.FeedParser).parseFromString(inputText, url);
+    } catch (e) {
+        if (e instanceof feedParser.ParserError ||
+                e instanceof feedParser.UnsupportedFeedTypeError) {
+            // let the browser deal with non-well formed XML or XML documents
+            // which are not supported Atom or RSS feeds
+            return inputText;
+        }
+        throw e;
+    }
+    console.log(`parsed feed ${url}:\n`, feed);
+
+    // render the preview document
+    let feedPreviewDocument = new DOMParser()
+            .parseFromString(await fetchingFeedPreview, 'text/html');
+    renderFeedPreview(feedPreviewDocument, feed);
+
+    return new XMLSerializer().serializeToString(feedPreviewDocument);
+}
+
+browser.webRequest.onHeadersReceived.addListener(details => {
+    if (details.statusCode !== 200) {
+        return {};
+    }
+
+    let contentTypeIndex = details.responseHeaders.findIndex(header =>
+            header.name.toLowerCase() === 'content-type' &&
+            typeof header.value !== 'undefined');
+    if (contentTypeIndex < 0) {
+        // no Content-Type header found
+        return {};
+    }
+    let headerValue = details.responseHeaders[contentTypeIndex].value
+    let contentType = parseContentType(headerValue);
+    // until content handlers become available to webextensions
+    // (https://bugzilla.mozilla.org/show_bug.cgi?id=1457500) intercept all
+    // responses and change the content type from application/atom+xml or
+    // application/rss+xml to application/xml which will then be probed for
+    // Atom or RSS content
+    switch (contentType.mediaType) {
+        case 'application/atom+xml':
+        case 'application/rss+xml':
+        case 'application/rdf+xml':
+        case 'application/xml':
+            break;
+        default:
+            // non-XML media type
+            return {};
+    }
+    console.log(`response is an XML document\n`,
+            `media type: ${contentType.mediaType}\n`,
+            `charset: ${contentType.charset}`);
+
+    let decoder;
+    try {
+        decoder = new TextDecoder(contentType.charset);
+    } catch (e) {
+        if (e instanceof RangeError) {
+            // unsupported charset
+            return {};
+        } else {
+            throw e;
+        }
+    }
+    let encoder = new TextEncoder();
+    let inputText = '';
+    let filter = browser.webRequest.filterResponseData(details.requestId);
+    filter.addEventListener('data', ev => {
+        inputText += decoder.decode(ev.data, {stream: true});
+    });
+    filter.addEventListener('stop', async ev => {
+        let result = await handleFeed(inputText, details.url);
+        filter.write(encoder.encode(result));
+        filter.close();
+    });
+
+    details.responseHeaders[contentTypeIndex] = {
+        name: 'Content-Type',
+        value: `application/xml;charset=${contentType.charset}`
+    };
+
+    return {responseHeaders: details.responseHeaders};
+},
+        {urls: ['http://*/*', 'https://*/*'], types: ['main_frame']},
+        ['blocking', 'responseHeaders']);
+
+browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
+    let tab = sender.tab;
+    if (typeof tab !== 'undefined') {
+        // content script sending feeds
+        tabsFeeds.set(tab.id, request);
+        browser.pageAction.show(tab.id);
+    } else {
+        // popup querying feeds
+        sendResponse(tabsFeeds.get(request));
+    }
+});
+
+browser.tabs.onUpdated.addListener((id, changeInfo, tab) => {
+    if (typeof changeInfo.url === 'undefined') {
+        // filter out updates which do not change the URL
+        return;
+    }
+
+    // hide the page action when the URL changes since it is no longer valid,
+    // it will be shown again if the content script detects a feed
+    browser.pageAction.hide(tab.id);
+});
+
+browser.tabs.onRemoved.addListener((tabId, removeInfo) => {
+    tabsFeeds.delete(tabId);
+});