Create a quick plugin

I am trying to create a quick plugin to replace formatting in my notes. I found laurent's advice how to spin up a quick plugin by inserting js into JoplinProfile/plugins directory as described below.

I tried it with this bit of code, but it does not execute upon restarting Joplin, there is no log in Console.

/* joplin-manifest:
{
	"id": "com.example.JarvisPlugin",
	"manifest_version": 1,
	"app_min_version": "2.1",
	"name": "Jarvis Plugin",
	"version": "1.0.0"
}
*/

joplin.plugins.register({
	onStart: async function() {
		console.log('Jarvis formatting plugin has started!');

		const runJarvisScript = () => {
			let content = document.body.innerHTML;
			const jarvisStart = "<!-- jarvis-summary-start -->";
			const jarvisEnd = "<!-- jarvis-summary-end -->";

			// Use variables instead of direct string literals
			content = content.replace(
				new RegExp(jarvisStart + "([\\s\\S]*?)" + jarvisEnd, "g"),
				'<jarvis>$1</jarvis>'
			);

			document.body.innerHTML = content;
		};

		runJarvisScript();
	}
});

Is there something I am doing wrong? The file with the snippet is .js. I am running Joplin Portable under Windows 11.

document.body points to the plugin's background page (which isn't visible to the user). Consider using joplin.commands.execute or joplin.data to update the content of a note.

1 Like

Many thanks!

I ended up creating this plugin. It sort of works, although the jarvis html comments do not get replaced with tags right away, but I have to leave and return back to the note after I generate the annotation.

/* joplin-manifest:
{
	"id": "com.example.JarvisPlugin",
	"manifest_version": 1,
	"app_min_version": "2.1",
	"name": "Jarvis Plugin",
	"version": "1.0.0"
}
*/

joplin.plugins.register({
	onStart: async function() {
		console.log('Jarvis formatting plugin has started!');

		// Function to apply formatting
		const applyJarvisFormatting = async () => {
			const note = await joplin.workspace.selectedNote();
			if (note) {
				let content = note.body;
				const jarvisStart = "<!-- jarvis-summary-start -->";
				const jarvisEnd = "<!-- jarvis-summary-end -->";

				// Add newlines before and after the jarvis tag for proper Markdown rendering
				content = content.replace(
					new RegExp(jarvisStart + "([\\s\\S]*?)" + jarvisEnd, "g"),
					'<jarvis>\n$1\n</jarvis>'
				);

				// Update the note content
				await joplin.data.put(['notes', note.id], null, { body: content });

				console.log('Jarvis formatting applied to the selected note.');
			} else {
				console.log('No note selected.');
			}
		};

		// Apply formatting when a note is selected or changed
		await joplin.workspace.onNoteSelectionChange(applyJarvisFormatting);

		// Initial run for the note that is open when the plugin starts
		applyJarvisFormatting();
	}
});

Enough tinkering for today :slight_smile: