Compare commits

...

9 Commits

Author SHA1 Message Date
f515b9d969 excerpt -> content 2023-10-09 19:31:05 +02:00
f22e8199e4 adjustable post_type 2023-10-09 19:21:54 +02:00
85ff4c0c6c Docu update 2023-10-09 18:54:06 +02:00
d6f93f2f42 require entfernt 2023-10-09 18:44:51 +02:00
5d4d318a50 helper moved 2023-10-09 18:41:32 +02:00
b2166a8425 debugging messages 2023-10-09 18:33:43 +02:00
2a594e0974 readded docu 2023-10-09 18:24:17 +02:00
26e036c71b changed mechanism 2023-10-09 18:24:09 +02:00
6568ffb45c strip tags hinzugefügt 2023-10-09 18:19:01 +02:00
3 changed files with 101 additions and 50 deletions

View File

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

View File

@ -1,5 +1,28 @@
# Info
This module for Magic Mirror should display posts from a WordPress site.
# Install
```bash
git clone https://github.com/tbuss/MMM-Wordpress.git
cd MMM-Wordpress
npm install
```
# Config
Put this in your config.js
```js
{
module: 'MMM-Wordpress',
position: 'bottom_bar', // or any other position
config: {
updateInterval: 10*60*1000, // 10 minutes
url: 'https://www.your_wordpress.url',
numberOfPosts: 5
}
},
```

24
node_helper.js Normal file
View File

@ -0,0 +1,24 @@
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) {
console.log('Notification!');
if (notification === 'GET_POSTS') {
console.log('Notification: GET_POSTS');
const { url, posts, 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);
}
});
}
}
});