Compare commits

..

No commits in common. "2a594e0974f3a812e679ae9ac4de08d60950fb3f" and "814694c97ee1857942365575e9e450a4217107c8" have entirely different histories.

2 changed files with 45 additions and 71 deletions

View File

@ -9,57 +9,53 @@
Module.register('MMM-Wordpress', { Module.register('MMM-Wordpress', {
defaults: { defaults: {
updateInterval: 600000, // 10 minutes updateInterval: 600000, // 10 minutes
url: 'YOUR_WORDPRESS_API_URL', // replace with your WordPress API endpoint url: 'YOUR_WORDPRESS_API_URL', // replace with your WordPress API endpoint
numberOfPosts: 5, // number of posts to display numberOfPosts: 5, // number of posts to display
posts: 'posts', //Endpoint ("posts" for normal posts, can be changed to custom post type) posts: 'posts', //Endpoint ("posts" for normal posts, can be changed to custom post type)
}, },
start: function() { start: function() {
this.posts = []; this.posts = [];
this.getPosts();
setInterval(() => {
this.getPosts(); this.getPosts();
}, this.config.updateInterval); setInterval(() => {
}, this.getPosts();
}, this.config.updateInterval);
},
getPosts: function() { getPosts: function() {
const url = this.config.url; const url = this.config.url;
const posts = this.config.posts; const posts = this.config.posts;
const numberOfPosts = this.config.numberOfPosts; const numberOfPosts = this.config.numberOfPosts;
//hier müssen die posts noch rein! const request = require('request');
this.sendSocketNotification('GET_POSTS', { url, numberOfPosts });
},
socketNotificationReceived: function(notification, payload) { request.get(`${url}/wp-json/wp/v2/${posts}?per_page=${numberOfPosts}`, (error, response, body) => {
if (notification === 'POSTS_RESULT' && payload) { if (!error && response.statusCode == 200) {
this.posts = payload; this.posts = JSON.parse(body);
this.updateDom(); this.updateDom();
} }
}, });
},
getDom: function() { getDom: function() {
const wrapper = document.createElement('div'); const wrapper = document.createElement('div');
const posts = this.posts; const posts = this.posts;
if (posts.length > 0) { if (posts.length > 0) {
for (let i = 0; i < posts.length; i++) { for (let i = 0; i < posts.length; i++) {
const post = posts[i]; const post = posts[i];
const postElement = document.createElement('div'); const postElement = document.createElement('div');
postElement.innerHTML = `<h2>${this.stripTags(post.title.rendered)}</h2><p>${this.stripTags(post.excerpt.rendered)}</p>`; postElement.innerHTML = `<h2>${post.title.rendered}</h2><p>${post.excerpt.rendered}</p>`;
wrapper.appendChild(postElement); wrapper.appendChild(postElement);
}
} else {
wrapper.innerHTML = 'No posts found';
} }
} else {
wrapper.innerHTML = 'Keine Beiträge gefunden'; return wrapper;
} }
});
return wrapper;
},
stripTags: function(html) {
const doc = new DOMParser().parseFromString(html, 'text/html');
return doc.body.textContent || "";
}
});

View File

@ -1,22 +0,0 @@
const NodeHelper = require('node_helper');
const request = require('request');
module.exports = NodeHelper.create({
start: function() {
console.log('MMM-Wordpress helper started...');
},
socketNotificationReceived: function(notification, payload) {
if (notification === 'GET_POSTS') {
const { url, numberOfPosts } = payload;
const apiUrl = `${url}/wp-json/wp/v2/posts?per_page=${numberOfPosts}`;
request.get(apiUrl, (error, response, body) => {
if (!error && response.statusCode == 200) {
const posts = JSON.parse(body);
this.sendSocketNotification('POSTS_RESULT', posts);
}
});
}
}
});