index.md (2956B)
1 RSS/Atom feed detection 2 ======================= 3 4 Description 5 ----------- 6 7 This script looks for links to RSS or Atom feeds in the current web page. If it 8 finds feeds, it places an icon in the corner of the page which toggles showing 9 a list of the feeds. 10 11 To install, put the code in `~/.surf/script.js` 12 13 Author 14 ------ 15 16 Charles E. Lehner <https://celehner.com/> 17 18 Code 19 ---- 20 21 (function () { 22 var urls = {} 23 var feeds = [].slice.call(document.querySelectorAll( 24 "link[href][rel~=alternate][type$=xml]," + 25 " a[href][rel~=alternate][type$=xml]")) 26 .map(function (el) { 27 return { 28 href: el.href, 29 title: el.title || document.title, 30 type: /atom/i.test(el.type) ? 'Atom' : 'RSS' 31 }; 32 }).filter(function (feed) { 33 if (urls[feed.href]) return false 34 return urls[feed.href] = true 35 }); 36 if (!feeds.length) return; 37 38 var container = document.createElement('div'); 39 container.style.position = 'fixed'; 40 container.style.bottom = 0; 41 container.style.right = 0; 42 container.style.zIndex = 10000; 43 document.body.appendChild(container); 44 45 var feedList = document.createElement('div'); 46 feedList.style.display = 'none'; 47 feedList.style.backgroundColor = '#ddd'; 48 feedList.style.border = '1px solid #bbb'; 49 feedList.style.borderStyle = 'solid solid none'; 50 feedList.style.padding = '2px 4px'; 51 container.appendChild(feedList); 52 53 feeds.forEach(function (feed) { 54 var a = document.createElement('a'); 55 a.href = feed.href; 56 a.style.display = 'block'; 57 a.style.color = 'blue'; 58 var title = feed.title; 59 if (title.indexOf(feed.type) == -1) 60 title += ' (' + feed.type + ')'; 61 a.appendChild(document.createTextNode(title)); 62 feedList.appendChild(a); 63 }); 64 65 var toggleLink = document.createElement('a'); 66 toggleLink.href = ''; 67 toggleLink.style.display = 'inline-block'; 68 toggleLink.style.paddingRight = '3px'; 69 toggleLink.style.verticalAlign = 'bottom'; 70 toggleLink.addEventListener("click", toggleFeedList, true); 71 container.appendChild(toggleLink); 72 73 var img = new Image(); 74 img.style.padding = '4px'; 75 img.style.verticalAlign = 'bottom'; 76 img.src = 'data:image/gif;base64,' + 77 'R0lGODlhDAAMAPUzAPJoJvJqKvNtLfNuL/NvMfNwMvNyNPNzNvN0OPN1OfN3O/R4' + 78 'PfR5P/R6QPR7QvR+RvR/R/SASfSBS/SDTPWETvWFUPWGUvWJVfWLWfWMWvWNXPaW' + 79 'aPaYbPaabfebb/eccfeedPehePemf/ingPiphPiqhviui/ivjfiwjviykPm6nPm+' + 80 'ofzh1P3k2f3n3P7u5/7v6P738/749f///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' + 81 'AAAAAAAAAAAAAAAAACH5BAEAADQALAAAAAAMAAwAAAZ6QFqB4YBAJBLKZCEsbCDE' + 82 'I2WqQCBms9fqkqRIJg7EZ+WawTxeSAS6AklIMhknwjA6sC/SR/aSKBwSEBcpLzMk' + 83 'IjMoBwwTECEoGTAvDi8uBAhKMokmMxwqMwIIFhQsMRoZMyeIFgILFoEMCAcEAgEA' + 84 'BDQKRhAOsbICNEEAOw=='; 85 toggleLink.appendChild(img); 86 87 if (feeds.length > 1) { 88 toggleLink.appendChild(document.createTextNode(feeds.length)); 89 } 90 91 function toggleFeedList(e) { 92 e.preventDefault(); 93 feedList.style.display = (feedList.style.display == 'none') ? 94 'inline-block' : 'none'; 95 } 96 })();