From 86747dbadc29d9a432074e3021555fb16ebbecf0 Mon Sep 17 00:00:00 2001 From: Juraj Oravec Date: Sat, 12 Mar 2022 02:15:24 +0100 Subject: [PATCH] Move site js code to separate file "finder.js" Signed-off-by: Juraj Oravec --- rssfinder/finder.js | 50 ++++++++++++++++++++++++++++++++++++++ rssfinder/main.qml | 59 +++++++++------------------------------------ 2 files changed, 61 insertions(+), 48 deletions(-) create mode 100644 rssfinder/finder.js diff --git a/rssfinder/finder.js b/rssfinder/finder.js new file mode 100644 index 0000000..209af75 --- /dev/null +++ b/rssfinder/finder.js @@ -0,0 +1,50 @@ +(function() { + +var availableLinks = []; +var links = document.getElementsByTagName('link'); +var siteTitle = 'No title was found'; + +if (document.getElementsByTagName('title').lenght > 0) { + siteTitle = document.getElementsByTagName('title')[0].innerHTML +} + +for (link of links) { + if (link.getAttribute('rel') == 'alternate' && link.getAttribute('type') == 'application/rss+xml') { + var linkTitle; + var linkUrl; + + if (link.hasAttribute('title')) { + linkTitle = link.getAttribute('title'); + } else { + linkTitle = siteTitle; + } + + if (link.getAttribute('href').startsWith('/')) { + linkUrl = window.location.protocol + '//' + window.location.hostname + link.getAttribute('href'); + } else if (!link.getAttribute('href').includes('/')) { + linkUrl = window.location.protocol + '//' + window.location.hostname + '/' + link.getAttribute('href'); + } else { + linkUrl = link.getAttribute('href'); + } + + availableLinks.push({ + title: linkTitle, + url: linkUrl + }); + } +} + +if (links.length == 0) { + availableLinks.push({ + title: 'No RSS feed was found', + url: 'No URL is provided' + }); +} + +var out = { + links: availableLinks +}; + +return out; + +})() diff --git a/rssfinder/main.qml b/rssfinder/main.qml index da43543..06746ed 100644 --- a/rssfinder/main.qml +++ b/rssfinder/main.qml @@ -8,9 +8,15 @@ Falkon.PluginInterface { QtObject { id: rssFinderObject property ListModel feeds: ListModel {} + property string jsFinder } init: function(state, settingsPath){ + console.log(i18n('"RSSFinder" plugin is loading')) + rssFinderObject.jsFinder = Falkon.FileUtils.readAllFileContents('finder.js') + if (rssFinderObject.jsFinder.length == 0) { + console.log(i18n('"RSSFinder" Unable to load "finder.js""')) + } console.log(i18n('"RSSFinder" plugin loaded')) } @@ -31,56 +37,13 @@ Falkon.PluginInterface { } function findFeeds() { + if (rssFinderObject.jsFinder.length == 0) { + return + } + var window = Falkon.Windows.getCurrent() var currentTab = findCurrentTab(window) - var result = currentTab.execJavaScript(" - var availableLinks = []; - var links = document.getElementsByTagName('link'); - var siteTitle = 'No title was found'; - - if (document.getElementsByTagName('title').lenght > 0) { - siteTitle = document.getElementsByTagName('title')[0].innerHTML - } - - for (link of links) { - if (link.getAttribute('rel') == 'alternate' && link.getAttribute('type') == 'application/rss+xml') { - var linkTitle; - var linkUrl; - - if (link.hasAttribute('title')) { - linkTitle = link.getAttribute('title'); - } else { - linkTitle = siteTitle; - } - - if (link.getAttribute('href').startsWith('/')) { - linkUrl = window.location.protocol + '//' + window.location.hostname + link.getAttribute('href'); - } else if (!link.getAttribute('href').includes('/')) { - linkUrl = window.location.protocol + '//' + window.location.hostname + '/' + link.getAttribute('href'); - } else { - linkUrl = link.getAttribute('href'); - } - - availableLinks.push({ - title: linkTitle, - url: linkUrl - }); - } - } - - if (links.length == 0) { - availableLinks.push({ - title: 'No RSS feed was found', - url: 'No URL is provided' - }); - } - - var out = { - links: availableLinks - }; - - out; - ") + var result = currentTab.execJavaScript(rssFinderObject.jsFinder) rssFinderObject.feeds.clear()