I was creating my daily note template when I found it’s very boring without memes. So I try to create a random meme generator.
- Features: it randomly displays meme from a given list of subreddits (you can modify this list).
- Plugins required:
dataview
andCustomJS
. - Bugs (that I would fix if some of you guys love it): it re-generates the meme every time the note is modified or it is closed. For me a random meme is good enough so I haven’t fixed that bug
.
Here’s how to create your own meme generator (supposed you have all the required plugins):
- Choose a folder in your current Obsidian Vault and create a script named
meme.js
(any name will do I believe). Copy and paste this piece of code into the script:
class RandomMeme {
updateDetails(element, url) {element.setAttribute("src", url);};
generateMeme(element) {
// Find meme subreddits that you like, copy its pathname.
// https://www.reddit.com/r/depression_memes/ => "depression_memes"
var choices = ["depression_memes", "dankmemes", "ProgrammerHumor"]
var index = Math.floor(Math.random() * choices.length)
var subReddit = choices[index]
fetch(`https://meme-api.com/gimme/${subReddit}`)
.then((response) => response.json())
.then((data) => {
this.updateDetails(element, data.url);
});
};
}
-
Set your CustomJS folder to the folder contains your script, mine is Extra/Script.
-
Copy and paste this piece of code into your note.
```dataviewjs
const {RandomMeme} = customJS;
let img = this.container.createEl(‘img’, {cls: [“meme”]})
img.style.height = “300px”;
img.style.objectFit = “fill”
RandomMeme.generateMeme(img)
``` -
Enjoy fresh memes everyday.
In case you guys have any trouble when following the instruction, I’d love to help!