Move site js code to separate file "finder.js"

Signed-off-by: Juraj Oravec <jurajoravec@mailo.com>
This commit is contained in:
Juraj Oravec 2022-03-12 02:15:24 +01:00
parent 0a857ba5e6
commit 86747dbadc
Signed by: SGOrava
GPG Key ID: 13660A3F1D9F093B
2 changed files with 61 additions and 48 deletions

50
rssfinder/finder.js Normal file
View File

@ -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;
})()

View File

@ -8,9 +8,15 @@ Falkon.PluginInterface {
QtObject { QtObject {
id: rssFinderObject id: rssFinderObject
property ListModel feeds: ListModel {} property ListModel feeds: ListModel {}
property string jsFinder
} }
init: function(state, settingsPath){ 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')) console.log(i18n('"RSSFinder" plugin loaded'))
} }
@ -31,56 +37,13 @@ Falkon.PluginInterface {
} }
function findFeeds() { function findFeeds() {
if (rssFinderObject.jsFinder.length == 0) {
return
}
var window = Falkon.Windows.getCurrent() var window = Falkon.Windows.getCurrent()
var currentTab = findCurrentTab(window) var currentTab = findCurrentTab(window)
var result = currentTab.execJavaScript(" var result = currentTab.execJavaScript(rssFinderObject.jsFinder)
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;
")
rssFinderObject.feeds.clear() rssFinderObject.feeds.clear()