RANDOM MEME GENERATOR using dataview and customjs

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 and CustomJS.
  • 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 :slight_smile:.

Here’s how to create your own meme generator (supposed you have all the required plugins):

  1. 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);
			});
	};
}
  1. Set your CustomJS folder to the folder contains your script, mine is Extra/Script.

  2. 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)
    ```

  3. Enjoy fresh memes everyday.
    Cheers Drinking GIF - Cheers Drinking Thirsty - Discover & Share GIFs


In case you guys have any trouble when following the instruction, I’d love to help!

1 Like