diff --git a/src/js/background.js b/src/js/background.js index c6c1ebb..93f07d2 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -21,8 +21,10 @@ const subtome = { browser.browserAction.setTitle({ tabId : tabId, title : browser.i18n.getMessage('page_has_no_feed') }); }, - hasFeeds () { - browser.tabs.executeScript(null, { file : 'js/hasfeeds.js', runAt : 'document_end' }); + hasFeeds (tabId, changeInfo) { + if (changeInfo.url) { + browser.tabs.executeScript(tabId, { file : 'js/hasfeeds.js', runAt : 'document_end' }); + } }, handleResponse (response) { diff --git a/src/js/hasfeeds.js b/src/js/hasfeeds.js index 5ad96dd..2bfffaa 100644 --- a/src/js/hasfeeds.js +++ b/src/js/hasfeeds.js @@ -1,23 +1,27 @@ 'use strict'; -const feeds = []; -const links = document.getElementsByTagName('link'); -const linksLength = links.length; +// use IIFE to avoid redeclaration of const/let +(() => { + const links = document.getElementsByTagName('link'); + const linksLength = links.length; + let hasFeeds = false; -for (let i = 0; i < linksLength; i++) { - const link = links[i]; - if (link.type && link.rel && link.rel.split(' ').indexOf('alternate') >= 0) { - if (link.type.indexOf('rss') >= 0 || link.type.indexOf('atom') >= 0) { - if (link.href && link.href.length > 0) { - feeds.push(encodeURIComponent(link.href)); + for (let i = 0; i < linksLength; i++) { + const link = links[i]; + if (link.type && link.rel && link.rel.split(' ').indexOf('alternate') >= 0) { + if (link.type.indexOf('rss') >= 0 || link.type.indexOf('atom') >= 0) { + if (link.href && link.href.length > 0) { + hasFeeds = true; + break; + } } } } -} -if (feeds.length > 0) { - browser.runtime.sendMessage({ message : 'has_feed' }); -} -else { - browser.runtime.sendMessage({ message : 'has_no_feed' }); -} + if (hasFeeds) { + browser.runtime.sendMessage({ message : 'has_feed' }); + } + else { + browser.runtime.sendMessage({ message : 'has_no_feed' }); + } +})(); diff --git a/src/js/subtome.js b/src/js/subtome.js index 0625ffe..e623c4b 100644 --- a/src/js/subtome.js +++ b/src/js/subtome.js @@ -1,20 +1,35 @@ 'use strict'; -/* global feeds */ +// use IIFE to avoid redeclaration of const/let +(() => { + const feeds = []; + const links = document.getElementsByTagName('link'); + const linksLength = links.length; + const iframe = document.createElement('iframe'); + const resource = window.location.toString(); + let loaded = false; -const iframe = document.createElement('iframe'); -const resource = window.location.toString(); -let loaded = false; + for (let i = 0; i < linksLength; i++) { + const link = links[i]; + if (link.type && link.rel && link.rel.split(' ').indexOf('alternate') >= 0) { + if (link.type.indexOf('rss') >= 0 || link.type.indexOf('atom') >= 0) { + if (link.href && link.href.length > 0) { + feeds.push(encodeURIComponent(link.href)); + } + } + } + } -iframe.setAttribute('style', 'display:block; position:fixed; top:0px; left:0px; width:100%; height:100%; border:0px; background: transparent; z-index: 2147483647'); -iframe.setAttribute('src', 'https://www.subtome.com/?subs/#/subscribe?resource=' + encodeURIComponent(resource) + '&feeds=' + encodeURIComponent(feeds.join(','))); + iframe.setAttribute('style', 'display:block; position:fixed; top:0px; left:0px; width:100%; height:100%; border:0px; background: transparent; z-index: 2147483647'); + iframe.setAttribute('src', 'https://www.subtome.com/?subs/#/subscribe?resource=' + encodeURIComponent(resource) + '&feeds=' + encodeURIComponent(feeds.join(','))); -iframe.onload = function () { - if (loaded) { - document.getElementsByTagName('body')[0].removeChild(iframe); - } + iframe.onload = function () { + if (loaded) { + document.getElementsByTagName('body')[0].removeChild(iframe); + } - loaded = true; -}; + loaded = true; + }; -document.getElementsByTagName('body')[0].appendChild(iframe); + document.getElementsByTagName('body')[0].appendChild(iframe); +})();