addons/firefox-addons/feed-preview

changeset 29:688d75e554e0

Add option to expand feed entries by default
author Guido Berhoerster <guido+feed-preview@berhoerster.name>
date Mon Jan 21 15:15:59 2019 +0100 (16 months ago)
parents 104a1faebd43
children b9dc94ce2eeb
files _locales/de/messages.json _locales/en/messages.json js/background.js js/feed-preview.js options/options.css options/options.html options/options.js
line diff
     1.1 --- a/_locales/de/messages.json	Mon Jan 21 08:22:52 2019 +0100
     1.2 +++ b/_locales/de/messages.json	Mon Jan 21 15:15:59 2019 +0100
     1.3 @@ -90,5 +90,13 @@
     1.4      "missingPlaceholderError": {
     1.5          "message": "Bitte die URL zum Abonnieren von Feeds eingeben, die einen Platzhalter \"%s\" für die URL das Feeds enthält.",
     1.6          "description": "Error message if the placholder is missing from the subscription URL template."
     1.7 +    },
     1.8 +    "feedPreviewTitle": {
     1.9 +        "message": "Feed-Vorschau",
    1.10 +        "description": "Title of the feed preview options."
    1.11 +    },
    1.12 +    "feedPreviewExpandItemLabel": {
    1.13 +        "message": "Feed-Einträge standardmäßig aufklappen",
    1.14 +        "description": "Label of the checkbox for enabling feed entry expansion by default."
    1.15      }
    1.16  }
     2.1 --- a/_locales/en/messages.json	Mon Jan 21 08:22:52 2019 +0100
     2.2 +++ b/_locales/en/messages.json	Mon Jan 21 15:15:59 2019 +0100
     2.3 @@ -90,5 +90,13 @@
     2.4      "missingPlaceholderError": {
     2.5          "message": "Please enter a subscription URL which contains a placeholder \"%s\" for the feed URL.",
     2.6          "description": "Error message if the placholder is missing from the subscription URL template."
     2.7 +    },
     2.8 +    "feedPreviewTitle": {
     2.9 +        "message": "Feed Preview",
    2.10 +        "description": "Title of the feed preview options."
    2.11 +    },
    2.12 +    "feedPreviewExpandItemLabel": {
    2.13 +        "message": "Expand feed entries by default",
    2.14 +        "description": "Label of the checkbox for enabling feed entry expansion by default."
    2.15      }
    2.16  }
     3.1 --- a/js/background.js	Mon Jan 21 08:22:52 2019 +0100
     3.2 +++ b/js/background.js	Mon Jan 21 15:15:59 2019 +0100
     3.3 @@ -57,6 +57,9 @@
     3.4  var tabsFeedPreviews = new Map();
     3.5  var fetchingFeedPreview = fetch('web_resources/feed-preview.xhtml')
     3.6          .then(response => response.text());
     3.7 +var feedPreviewOptions = {
     3.8 +    expandEntries: false
     3.9 +};
    3.10  
    3.11  function parseContentType(header) {
    3.12      let contentType = {
    3.13 @@ -103,7 +106,8 @@
    3.14      // render the preview document
    3.15      let feedPreviewDocument = new DOMParser()
    3.16              .parseFromString(await fetchingFeedPreview, 'text/html');
    3.17 -    renderFeedPreview(feedPreviewDocument, feed);
    3.18 +    renderFeedPreview(feedPreviewDocument, feed,
    3.19 +            feedPreviewOptions.expandEntries);
    3.20  
    3.21      return new XMLSerializer().serializeToString(feedPreviewDocument);
    3.22  }
    3.23 @@ -226,3 +230,20 @@
    3.24          browser.storage.sync.set({feedReaders});
    3.25      }
    3.26  });
    3.27 +
    3.28 +browser.storage.sync.get('feedPreview').then(({feedPreview}) => {
    3.29 +    if (typeof feedPreview !== 'undefined' &&
    3.30 +            feedPreview === Object(feedPreview)) {
    3.31 +        feedPreviewOptions.expandEntries = !!feedPreview.expandEntries;
    3.32 +    }
    3.33 +});
    3.34 +browser.storage.onChanged.addListener((changes, areaName) => {
    3.35 +    if (areaName !== 'sync' || typeof changes.feedPreview === 'undefined') {
    3.36 +        return;
    3.37 +    }
    3.38 +
    3.39 +    let newValue = changes.feedPreview.newValue;
    3.40 +    if (typeof newValue !== 'undefined' && newValue === Object(newValue)) {
    3.41 +        feedPreviewOptions.expandEntries = !!newValue.expandEntries;
    3.42 +    }
    3.43 +});
     4.1 --- a/js/feed-preview.js	Mon Jan 21 08:22:52 2019 +0100
     4.2 +++ b/js/feed-preview.js	Mon Jan 21 15:15:59 2019 +0100
     4.3 @@ -8,7 +8,8 @@
     4.4  
     4.5  'use strict';
     4.6  
     4.7 -export function renderFeedPreview(feedPreviewDocument, feed) {
     4.8 +export function renderFeedPreview(feedPreviewDocument, feed,
     4.9 +        expandEntriesByDefault) {
    4.10      // inject XSL stylesheet which transforms XHTML to HTML allowing the use of
    4.11      // the HTML DOM
    4.12      let xslFilename = browser.runtime.getURL('web_resources/xhtml-to-html.xsl');
    4.13 @@ -59,9 +60,10 @@
    4.14          let entryNode =
    4.15                  feedPreviewDocument.importNode(entryTemplateElement.content,
    4.16                  true);
    4.17 +        entryNode.querySelector('details.entry').open = expandEntriesByDefault;
    4.18 +
    4.19          let titleElement;
    4.20          let titleNode;
    4.21 -
    4.22          if (typeof entry.link !== 'undefined') {
    4.23              titleNode = feedPreviewDocument
    4.24                      .importNode(entryTitleLinkedTemplateElement.content, true);
     5.1 --- a/options/options.css	Mon Jan 21 08:22:52 2019 +0100
     5.2 +++ b/options/options.css	Mon Jan 21 15:15:59 2019 +0100
     5.3 @@ -35,6 +35,10 @@
     5.4    font-weight: bold;
     5.5  }
     5.6  
     5.7 +h1 ~ h1 {
     5.8 +  margin-top: 32px;
     5.9 +}
    5.10 +
    5.11  input[type="text"] {
    5.12    border-radius: 2px;
    5.13    font-size: 15px;
    5.14 @@ -42,6 +46,11 @@
    5.15    min-height: 32px;
    5.16  }
    5.17  
    5.18 +input[type="checkbox"],
    5.19 +input[type="checkbox"] + label {
    5.20 +  vertical-align: middle;
    5.21 +}
    5.22 +
    5.23  .browser-style > input[type=text] {
    5.24    border-color: var(--text-input-border-color);
    5.25    box-shadow: none;
     6.1 --- a/options/options.html	Mon Jan 21 08:22:52 2019 +0100
     6.2 +++ b/options/options.html	Mon Jan 21 15:15:59 2019 +0100
     6.3 @@ -50,5 +50,11 @@
     6.4          <button type="submit" name="add" class="browser-style">
     6.5        </div>
     6.6      </form>
     6.7 +    <h1 id="feed-preview-title"></h1>
     6.8 +    <form id="feed-preview">
     6.9 +      <input type="checkbox" id="expand-entries" name="expand-entries">
    6.10 +      <label for="expand-entries">
    6.11 +      </label>
    6.12 +    </form>
    6.13    </body>
    6.14  </html>
     7.1 --- a/options/options.js	Mon Jan 21 08:22:52 2019 +0100
     7.2 +++ b/options/options.js	Mon Jan 21 15:15:59 2019 +0100
     7.3 @@ -18,6 +18,8 @@
     7.4  
     7.5          document.querySelector('#feed-readers-title').textContent =
     7.6                  browser.i18n.getMessage('feedReadersTitle');
     7.7 +        document.querySelector('#feed-preview-title').textContent =
     7.8 +                browser.i18n.getMessage('feedPreviewTitle');
     7.9  
    7.10          let feedReadersForm = document.forms['feed-readers'];
    7.11          feedReadersForm.elements['move-up'].textContent =
    7.12 @@ -46,6 +48,11 @@
    7.13                  browser.i18n.getMessage('feedReaderUrlTemplateCaption');
    7.14          addFeedReaderForm.addEventListener('focusout', this);
    7.15  
    7.16 +        let feedPreviewForm = document.forms['feed-preview'];
    7.17 +        feedPreviewForm.elements['expand-entries'].labels[0].textContent =
    7.18 +                browser.i18n.getMessage('feedPreviewExpandItemLabel');
    7.19 +        feedPreviewForm.addEventListener('change', this);
    7.20 +
    7.21          document.addEventListener('submit', this);
    7.22  
    7.23          this.initOptions();
    7.24 @@ -58,6 +65,17 @@
    7.25              this.updateFeedReaders(feedReaders);
    7.26          }
    7.27  
    7.28 +        let {feedPreview: feedPreviewOptions} =
    7.29 +                await browser.storage.sync.get('feedPreview');
    7.30 +        if (typeof feedPreviewOptions !== 'undefined' &&
    7.31 +                feedPreviewOptions === Object(feedPreviewOptions)) {
    7.32 +            console.log('initialized feedPreviewOptions from storage',
    7.33 +                    feedPreviewOptions);
    7.34 +            this.updateFeedPreviewOptions({
    7.35 +                    expandEntries: !!feedPreviewOptions.expandEntries
    7.36 +            });
    7.37 +        }
    7.38 +
    7.39          browser.storage.onChanged.addListener(this.onStorageChanged.bind(this));
    7.40      }
    7.41  
    7.42 @@ -113,6 +131,11 @@
    7.43          feedReadersForm.elements['buttons'].disabled = true;
    7.44      }
    7.45  
    7.46 +    updateFeedPreviewOptions(feedPreviewOptions) {
    7.47 +        document.forms['feed-preview'].elements['expand-entries'].checked =
    7.48 +                feedPreviewOptions.expandEntries;
    7.49 +    }
    7.50 +
    7.51      getFeedReaders() {
    7.52          let feedReaderInput =
    7.53                  document.forms['feed-readers'].elements['feed-reader'];
    7.54 @@ -150,26 +173,42 @@
    7.55      }
    7.56  
    7.57      onStorageChanged(changes, areaName) {
    7.58 -        if (areaName !== 'sync' || typeof changes.feedReaders === 'undefined') {
    7.59 +        if (areaName !== 'sync') {
    7.60              return;
    7.61          }
    7.62  
    7.63 -        let feedReaders;
    7.64 -        if (typeof changes.feedReaders.newValue !== 'undefined' &&
    7.65 -                Array.isArray(changes.feedReaders.newValue)) {
    7.66 -            feedReaders = changes.feedReaders.newValue;
    7.67 -            console.log('feedReaders changed to', feedReaders);
    7.68 -        } else {
    7.69 -            // list of feed readers was removed or set to nonsensical value
    7.70 -            feedReaders = [];
    7.71 -            console.log('feedReaders was removed');
    7.72 +        if (typeof changes.feedReaders !== 'undefined') {
    7.73 +            let feedReaders;
    7.74 +            if (typeof changes.feedReaders.newValue !== 'undefined' &&
    7.75 +                    Array.isArray(changes.feedReaders.newValue)) {
    7.76 +                feedReaders = changes.feedReaders.newValue;
    7.77 +                console.log('feedReaders changed to', feedReaders);
    7.78 +            } else {
    7.79 +                // list of feed readers was removed or set to nonsensical value
    7.80 +                feedReaders = [];
    7.81 +                console.log('feedReaders was removed');
    7.82 +            }
    7.83 +            if (this.selectedFeedReader >= feedReaders.length) {
    7.84 +                // save selected feed reader is no longer valid
    7.85 +                this.selectedFeedReader = -1;
    7.86 +            }
    7.87 +            this.updateFeedReaders(feedReaders);
    7.88 +            this.selectFeedReader();
    7.89          }
    7.90 -        if (this.selectedFeedReader >= feedReaders.length) {
    7.91 -            // save selected feed reader is no longer valid
    7.92 -            this.selectedFeedReader = -1;
    7.93 +        if (typeof changes.feedPreview !== 'undefined') {
    7.94 +            let feedPreviewOptions;
    7.95 +            let newValue = changes.feedPreview.newValue;
    7.96 +            if (typeof newValue !== 'undefined' &&
    7.97 +                    newValue === Object(newValue)) {
    7.98 +                feedPreviewOptions = {expandEntries: !!newValue.expandEntries};
    7.99 +            } else {
   7.100 +                // feed preview preferences were removed or set to nonsensical
   7.101 +                // value
   7.102 +                feedPreviewOptions = {expandEntries: false};
   7.103 +                console.log('feedPreview was removed');
   7.104 +            }
   7.105 +            this.updateFeedPreviewOptions(feedPreviewOptions);
   7.106          }
   7.107 -        this.updateFeedReaders(feedReaders);
   7.108 -        this.selectFeedReader();
   7.109      }
   7.110  
   7.111      handleEvent(ev) {
   7.112 @@ -238,6 +277,11 @@
   7.113              console.log('set feedReaders to', feedReaders);
   7.114  
   7.115              document.forms['add-feed-reader'].reset();
   7.116 +        } else if (ev.type === 'change' && ev.target.id === 'expand-entries') {
   7.117 +            console.log('expand entries by default:', ev.target.checked);
   7.118 +            browser.storage.sync.set({
   7.119 +                feedPreview: {expandEntries: ev.target.checked}
   7.120 +            });
   7.121          }
   7.122      }
   7.123  }