mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 11:50:00 +00:00
fix cors problems with newsfeed articles (#2940)
solves #2840 as far as possible. There could still be errors on the embedded iframe when the owner of the site has set `X-Frame-Options` or `Access-Control-Allow-Origin` headers (as already mentioned in the docs).
This commit is contained in:
parent
ad4dbd786a
commit
7bd944391e
@ -35,6 +35,7 @@ Special thanks to: @rejas, @sdetweil
|
|||||||
- Correctly show apparent temperature in SMHI weather provider
|
- Correctly show apparent temperature in SMHI weather provider
|
||||||
- Ensure updatenotification module isn't shown when local is _ahead_ of remote
|
- Ensure updatenotification module isn't shown when local is _ahead_ of remote
|
||||||
- Possibility to change FontAwesome class in calendar, so icons like `fab fa-facebook-square` works.
|
- Possibility to change FontAwesome class in calendar, so icons like `fab fa-facebook-square` works.
|
||||||
|
- Fix cors problems with newsfeed articles (as far as possible), allow disabling cors per feed with option `useCorsProxy: false` (#2840)
|
||||||
|
|
||||||
## [2.21.0] - 2022-10-01
|
## [2.21.0] - 2022-10-01
|
||||||
|
|
||||||
|
@ -42,6 +42,14 @@ Module.register("newsfeed", {
|
|||||||
dangerouslyDisableAutoEscaping: false
|
dangerouslyDisableAutoEscaping: false
|
||||||
},
|
},
|
||||||
|
|
||||||
|
getUrlPrefix: function (item) {
|
||||||
|
if (item.useCorsProxy) {
|
||||||
|
return location.protocol + "//" + location.host + "/cors?url=";
|
||||||
|
} else {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
// Define required scripts.
|
// Define required scripts.
|
||||||
getScripts: function () {
|
getScripts: function () {
|
||||||
return ["moment.js"];
|
return ["moment.js"];
|
||||||
@ -142,14 +150,19 @@ Module.register("newsfeed", {
|
|||||||
sourceTitle: item.sourceTitle,
|
sourceTitle: item.sourceTitle,
|
||||||
publishDate: moment(new Date(item.pubdate)).fromNow(),
|
publishDate: moment(new Date(item.pubdate)).fromNow(),
|
||||||
title: item.title,
|
title: item.title,
|
||||||
url: item.url,
|
url: this.getUrlPrefix(item) + item.url,
|
||||||
description: item.description,
|
description: item.description,
|
||||||
items: items
|
items: items
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
getActiveItemURL: function () {
|
getActiveItemURL: function () {
|
||||||
return typeof this.newsItems[this.activeItem].url === "string" ? this.newsItems[this.activeItem].url : this.newsItems[this.activeItem].url.href;
|
const item = this.newsItems[this.activeItem];
|
||||||
|
if (item) {
|
||||||
|
return typeof item.url === "string" ? this.getUrlPrefix(item) + item.url : this.getUrlPrefix(item) + item.url.href;
|
||||||
|
} else {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -18,9 +18,10 @@ const stream = require("stream");
|
|||||||
* @param {number} reloadInterval Reload interval in milliseconds.
|
* @param {number} reloadInterval Reload interval in milliseconds.
|
||||||
* @param {string} encoding Encoding of the feed.
|
* @param {string} encoding Encoding of the feed.
|
||||||
* @param {boolean} logFeedWarnings If true log warnings when there is an error parsing a news article.
|
* @param {boolean} logFeedWarnings If true log warnings when there is an error parsing a news article.
|
||||||
|
* @param {boolean} useCorsProxy If true cors proxy is used for article url's.
|
||||||
* @class
|
* @class
|
||||||
*/
|
*/
|
||||||
const NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings) {
|
const NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings, useCorsProxy) {
|
||||||
let reloadTimer = null;
|
let reloadTimer = null;
|
||||||
let items = [];
|
let items = [];
|
||||||
|
|
||||||
@ -57,7 +58,8 @@ const NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings
|
|||||||
title: title,
|
title: title,
|
||||||
description: description,
|
description: description,
|
||||||
pubdate: pubdate,
|
pubdate: pubdate,
|
||||||
url: url
|
url: url,
|
||||||
|
useCorsProxy: useCorsProxy
|
||||||
});
|
});
|
||||||
} else if (logFeedWarnings) {
|
} else if (logFeedWarnings) {
|
||||||
Log.warn("Can't parse feed item:");
|
Log.warn("Can't parse feed item:");
|
||||||
|
@ -34,6 +34,8 @@ module.exports = NodeHelper.create({
|
|||||||
const url = feed.url || "";
|
const url = feed.url || "";
|
||||||
const encoding = feed.encoding || "UTF-8";
|
const encoding = feed.encoding || "UTF-8";
|
||||||
const reloadInterval = feed.reloadInterval || config.reloadInterval || 5 * 60 * 1000;
|
const reloadInterval = feed.reloadInterval || config.reloadInterval || 5 * 60 * 1000;
|
||||||
|
let useCorsProxy = feed.useCorsProxy;
|
||||||
|
if (useCorsProxy === undefined) useCorsProxy = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
new URL(url);
|
new URL(url);
|
||||||
@ -46,7 +48,7 @@ module.exports = NodeHelper.create({
|
|||||||
let fetcher;
|
let fetcher;
|
||||||
if (typeof this.fetchers[url] === "undefined") {
|
if (typeof this.fetchers[url] === "undefined") {
|
||||||
Log.log("Create new newsfetcher for url: " + url + " - Interval: " + reloadInterval);
|
Log.log("Create new newsfetcher for url: " + url + " - Interval: " + reloadInterval);
|
||||||
fetcher = new NewsfeedFetcher(url, reloadInterval, encoding, config.logFeedWarnings);
|
fetcher = new NewsfeedFetcher(url, reloadInterval, encoding, config.logFeedWarnings, useCorsProxy);
|
||||||
|
|
||||||
fetcher.onReceive(() => {
|
fetcher.onReceive(() => {
|
||||||
this.broadcastFeeds();
|
this.broadcastFeeds();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user