├── license.md ├── readme.md └── simple-rss.js /license.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Nick Moreton 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Simple RSS Embed Plugin 2 | 3 | ## About 4 | 5 | This plugin allows you to embed content from an RSS feed on to your website using only `data` attributes directly in the HTML. 6 | 7 | ## Usage 8 | 9 | Add the plugin to your page, just before the `` tag. 10 | 11 | ```html 12 | 13 | ``` 14 | 15 | Create the container for the RSS feed to show in, and pass options in via `data` attributes. 16 | 17 | ```html 18 |
23 |
24 | ``` 25 | 26 | [Example](http://codepen.io/nickmoreton/pen/pEZggP) 27 | 28 | ### Options 29 | 30 | #### data-rss-feed (required) 31 | 32 | Specifies the RSS feed to pull content from 33 | 34 | #### data-rss-link-titles (optional) 35 | 36 | Specifies whether to link the titles to the original post. 37 | 38 | _default: true_ 39 | 40 | #### data-rss-title-wrapper (optional) 41 | 42 | Specifies the HTML element to wrap the titles. 43 | 44 | _default: h2_ 45 | 46 | #### data-rss-max (optional) 47 | 48 | Specifies the number of entries to output 49 | 50 | _default: 10_ 51 | -------------------------------------------------------------------------------- /simple-rss.js: -------------------------------------------------------------------------------- 1 | var simpleRSSPlugin = (function() { 2 | // get all the feed containers 3 | var feedsNodes = document.querySelectorAll('[data-rss-feed]'); 4 | // Convert to array 5 | var feeds = [].slice.call(feedsNodes); 6 | for (var i = 0; i < feeds.length; i++) { 7 | var container = feedsNodes[i]; 8 | 9 | // get feed URL 10 | var url = container.getAttribute('data-rss-feed'); 11 | // get whether to link titles 12 | var addLink = container.getAttribute('data-rss-link-titles') || 'true'; 13 | 14 | // get title wrapper element 15 | var titleWrapper = container.getAttribute('data-rss-title-wrapper') || 'h2'; 16 | // Max outputs 17 | var max = container.getAttribute('data-rss-max') || 10; 18 | // Get data - append as script with callback to avoid CORS 19 | var script = document.createElement('script'); 20 | script.src = document.location.protocol + '//api.rss2json.com/v1/api.json?callback=simpleRSSPlugin.handleJSON&rss_url=' + encodeURIComponent(url); 21 | document.querySelector('head').appendChild(script); 22 | 23 | // Remove script 24 | script.parentNode.removeChild(script); 25 | } 26 | // Callback function 27 | var loops = 0; 28 | function handleJSON(data) { 29 | if (data.feed && data.items) { 30 | 31 | var docFrag = document.createDocumentFragment(); 32 | for (var i = 0; i < data.items.length; i++) { 33 | var e = data.items[i]; 34 | var tempNode = document.createElement('div'); 35 | var template = '<' + titleWrapper + '>' + e.title + '' + e.content; 36 | if (addLink === 'false') { 37 | template = '<' + titleWrapper + '>' + e.title + '' + e.content; 38 | } 39 | if (i < max) { 40 | 41 | tempNode.innerHTML = template; 42 | 43 | docFrag.appendChild(tempNode); 44 | } 45 | } 46 | container = feedsNodes[loops]; 47 | container.appendChild(docFrag); 48 | loops++; 49 | } 50 | } 51 | // Return function for use in global scope 52 | return { 53 | handleJSON:handleJSON 54 | } 55 | })(); 56 | --------------------------------------------------------------------------------