Use let/const and arrow functions

This commit is contained in:
rejas 2020-07-12 12:45:32 +02:00
parent 8dc88fe64b
commit 476e52e004
3 changed files with 47 additions and 42 deletions

View File

@ -84,7 +84,7 @@ Module.register("newsfeed", {
// Override dom generator.
getDom: function () {
var wrapper = document.createElement("div");
const wrapper = document.createElement("div");
if (this.config.feedUrl) {
wrapper.className = "small bright";
@ -99,7 +99,7 @@ Module.register("newsfeed", {
if (this.newsItems.length > 0) {
// this.config.showFullArticle is a run-time configuration, triggered by optional notifications
if (!this.config.showFullArticle && (this.config.showSourceTitle || this.config.showPublishDate)) {
var sourceAndTimestamp = document.createElement("div");
const sourceAndTimestamp = document.createElement("div");
sourceAndTimestamp.className = "newsfeed-source light small dimmed";
if (this.config.showSourceTitle && this.newsItems[this.activeItem].sourceTitle !== "") {
@ -157,22 +157,22 @@ Module.register("newsfeed", {
}
if (!this.config.showFullArticle) {
var title = document.createElement("div");
const title = document.createElement("div");
title.className = "newsfeed-title bright medium light" + (!this.config.wrapTitle ? " no-wrap" : "");
title.innerHTML = this.newsItems[this.activeItem].title;
wrapper.appendChild(title);
}
if (this.isShowingDescription) {
var description = document.createElement("div");
const description = document.createElement("div");
description.className = "newsfeed-desc small light" + (!this.config.wrapDescription ? " no-wrap" : "");
var txtDesc = this.newsItems[this.activeItem].description;
const txtDesc = this.newsItems[this.activeItem].description;
description.innerHTML = this.config.truncDescription ? (txtDesc.length > this.config.lengthDescription ? txtDesc.substring(0, this.config.lengthDescription) + "..." : txtDesc) : txtDesc;
wrapper.appendChild(description);
}
if (this.config.showFullArticle) {
var fullArticle = document.createElement("iframe");
const fullArticle = document.createElement("iframe");
fullArticle.className = "";
fullArticle.style.width = "100vw";
// very large height value to allow scrolling
@ -345,7 +345,7 @@ Module.register("newsfeed", {
},
notificationReceived: function (notification, payload, sender) {
var before = this.activeItem;
const before = this.activeItem;
if (notification === "ARTICLE_NEXT") {
this.activeItem++;
if (this.activeItem >= this.newsItems.length) {

View File

@ -1,10 +1,9 @@
/* Magic Mirror
* Fetcher
* Node Helper: Newsfeed - NewsfeedFetcher
*
* By Michael Teeuw https://michaelteeuw.nl
* MIT Licensed.
*/
const Log = require("../../../js/logger.js");
const FeedMe = require("feedme");
const request = require("request");
@ -17,38 +16,39 @@ const iconv = require("iconv-lite");
* attribute reloadInterval number - Reload interval in milliseconds.
* attribute logFeedWarnings boolean - Log warnings when there is an error parsing a news article.
*/
var NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings) {
var self = this;
const NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings) {
const self = this;
let reloadTimer = null;
let items = [];
let fetchFailedCallback = function () {};
let itemsReceivedCallback = function () {};
if (reloadInterval < 1000) {
reloadInterval = 1000;
}
var reloadTimer = null;
var items = [];
var fetchFailedCallback = function () {};
var itemsReceivedCallback = function () {};
/* private methods */
/* fetchNews()
* Request the new items.
*/
var fetchNews = function () {
const fetchNews = function () {
clearTimeout(reloadTimer);
reloadTimer = null;
items = [];
var parser = new FeedMe();
const parser = new FeedMe();
parser.on("item", function (item) {
var title = item.title;
var description = item.description || item.summary || item.content || "";
var pubdate = item.pubdate || item.published || item.updated || item["dc:date"];
var url = item.url || item.link || "";
const title = item.title;
let description = item.description || item.summary || item.content || "";
const pubdate = item.pubdate || item.published || item.updated || item["dc:date"];
const url = item.url || item.link || "";
if (title && pubdate) {
var regex = /(<([^>]+)>)/gi;
const regex = /(<([^>]+)>)/gi;
description = description.toString().replace(regex, "");
items.push({
@ -76,10 +76,17 @@ var NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings)
scheduleTimer();
});
var nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
var headers = { "User-Agent": "Mozilla/5.0 (Node.js " + nodeVersion + ") MagicMirror/" + global.version + " (https://github.com/MichMich/MagicMirror/)", "Cache-Control": "max-age=0, no-cache, no-store, must-revalidate", Pragma: "no-cache" };
const nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
const opts = {
headers: {
"User-Agent": "Mozilla/5.0 (Node.js " + nodeVersion + ") MagicMirror/" + global.version + " (https://github.com/MichMich/MagicMirror/)",
"Cache-Control": "max-age=0, no-cache, no-store, must-revalidate",
Pragma: "no-cache"
},
encoding: null
};
request({ uri: url, encoding: null, headers: headers })
request(url, opts)
.on("error", function (error) {
fetchFailedCallback(self, error);
scheduleTimer();
@ -91,7 +98,7 @@ var NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings)
/* scheduleTimer()
* Schedule the timer for the next update.
*/
var scheduleTimer = function () {
const scheduleTimer = function () {
clearTimeout(reloadTimer);
reloadTimer = setTimeout(function () {
fetchNews();

View File

@ -32,37 +32,35 @@ module.exports = NodeHelper.create({
* attribute config object - A configuration object containing reload interval in milliseconds.
*/
createFetcher: function (feed, config) {
var self = this;
var url = feed.url || "";
var encoding = feed.encoding || "UTF-8";
var reloadInterval = feed.reloadInterval || config.reloadInterval || 5 * 60 * 1000;
const url = feed.url || "";
const encoding = feed.encoding || "UTF-8";
const reloadInterval = feed.reloadInterval || config.reloadInterval || 5 * 60 * 1000;
if (!validUrl.isUri(url)) {
self.sendSocketNotification("INCORRECT_URL", url);
this.sendSocketNotification("INCORRECT_URL", url);
return;
}
var fetcher;
if (typeof self.fetchers[url] === "undefined") {
let fetcher;
if (typeof this.fetchers[url] === "undefined") {
Log.log("Create new news fetcher for url: " + url + " - Interval: " + reloadInterval);
fetcher = new NewsfeedFetcher(url, reloadInterval, encoding, config.logFeedWarnings);
fetcher.onReceive(function (fetcher) {
self.broadcastFeeds();
fetcher.onReceive(() => {
this.broadcastFeeds();
});
fetcher.onError(function (fetcher, error) {
self.sendSocketNotification("FETCH_ERROR", {
fetcher.onError((fetcher, error) => {
this.sendSocketNotification("FETCH_ERROR", {
url: fetcher.url(),
error: error
});
});
self.fetchers[url] = fetcher;
this.fetchers[url] = fetcher;
} else {
Log.log("Use existing news fetcher for url: " + url);
fetcher = self.fetchers[url];
fetcher = this.fetchers[url];
fetcher.setReloadInterval(reloadInterval);
fetcher.broadcastItems();
}