Running javascript to edit a note

Hey: Just migrated from other app, and I am struggling to do something that perhaphs is already done / way easier than how I think it is.

The idea would be to have some html5/js code within a note that allows me to run and update the note itself, such as:

<button onclick="changeNote()">CLICKME</button>

<script>
function changeNote() {
	noteAPI.replace("a", "b")
	}
</script>

Does this exist already or I just need to go through creating a plugin for my personal functions?

Things I have tried

I have checked all the posts mentioning javascript and Obsidian and could not find a speficif answer other than custom plugins.

1 Like

You could take a look at the buttons or templater or customJS plugins.

Thank you. Had already checked and just did again.
I understand some JS can be run using those, but I dont see how / if the obsidian API is exposed for that JS to be able to edit the current page text, for instance.
The buttons plugin comes close but does not have an option to replace in the current note, just add / remove lines.
I will most likely end up making some quick-dirty plugin to do the few things I need.
Thanks again!

After reading again I managed a way to, using a button and a template, replace the checked boxes with empty boxes.
But its very tricky and seems to work randomly (depending on the rendering speed I think).
So I need to press the button several times.

Template:

<%*

//get file content and replace checked boxes for unchecked
var noteContent = tp.file.content.replaceAll ("[x]","[ ]");

//select all in note
let cmEditorAct = this.app.workspace.activeLeaf.view.editor;
cmEditorAct.setSelection({ line: 0, ch: 0 }, { line: 9999, ch: 9999 });

//replace content + set cursor at the start
cursor = tp.file.cursor(0);
tR = cursor + noteContent.split("<%*")[0];

%>

Button:

name Uncheck checkboxes
type append template
action templater.uncheckCheckboxes
templater true

^button-1

5 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.