Skip to content

Commit

Permalink
avoid redeclaration of const/let; don't execute hasFeeds() call more …
Browse files Browse the repository at this point in the history
…often than necessary
  • Loading branch information
cadeyrn committed Dec 23, 2021
1 parent 360c05f commit c7679b2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 31 deletions.
6 changes: 4 additions & 2 deletions src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
36 changes: 20 additions & 16 deletions src/js/hasfeeds.js
Original file line number Diff line number Diff line change
@@ -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' });
}
})();
41 changes: 28 additions & 13 deletions src/js/subtome.js
Original file line number Diff line number Diff line change
@@ -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);
})();

0 comments on commit c7679b2

Please sign in to comment.