obsidian-custom-js
obsidian-custom-js copied to clipboard
How add custom-js to my obsidian?
I added the folder with custom.js, but the plugin did not detect it. https://share.cleanshot.com/CcYFj7q1 You can find the file structure of a .js file by referring to its format.
How should I correctly add the .js file?
I trying, and still not working, last trial: I using this documentation https://github.com/saml-dev/obsidian-custom-js/blob/master/README.md#invocable-scripts
This is now my config https://share.cleanshot.com/gt6sX57T
1 - I set this path 2 - this is my script from documentation https://github.com/saml-dev/obsidian-custom-js/blob/master/README.md#invocable-scripts
3 - I want register new invocable script
4 - Not found
btw I restart obsidian.
@mxcdh
- Are your scripts detectable at all? If you execute
customJSfrom the developer console in Obsidian, do you see your scripts there? - Are you sure you have
async invoke()function in your script?
Hi, I have a similar problem:
- I have defined the folder within the settings of the plugin (9 setup/Javascript) but none of the scripts are being loaded
- when I execute customJS from the console they are also not visible
- only when I add them individually in the "individual Files" option of the settings they are loaded and work
@mnaoumov
- It is not detectable.
- Can you explain? This is my example script. What should I rewrite? https://dsh.re/824459 @svenschuldt Which OS are you using? In my case, that is not working.
@mxcdh , I am using macOS Ventura 13.5.2 (22G91)
@mxcdh have you tried removing the ./ from the start of your path?
@mxcdh current plugin has a few use-cases
-
customJSglobal variable, which you can use inDeveloper Tools Console,dataviewjs,Templaterscripts etc. To check if it works you should openDeveloper Tools Console(viaCommand + Shift + I) and typecustomJS.dvTasks. If it doesn't show an error, then your config is working properly. -
Invocable scripts.
dvTasks.jsis not an invocable script, that's why you don't see it in the list on step 4 on your screenshot
I'm having this issue too
@YousufSSyed what happens if you go to the developer console and type window.forceLoadCustomJS()?
I have a file script.js inside the Scripts folder and this is the result, I don't know why it thinks it would be in the root vault folder if I specified the folder name.
@YousufSSyed
-
that's because of your Individual files setting
-
It looks like your
Scripts/script.jsis not in the correct format. It should be something like
class Script {
func1() {
}
}
I changed the file to Scripts/script.js and this is the script. Nothing shows up when I do add an invocable script.
class Script {
getQuoteOfTheday() {
let file = this.app.workspace.getActiveFile();
let currentTime = moment().format('YYYY-MM-DD HH-h:mm:ssA');
let quote = await fetch("https://api.quotable.io/random").then(response => response.json());
app.vault.append(file, `\n- **${currentTime}, ${quote["author"]}:** ${quote["content"]}`)
}
}
@YousufSSyed
- Remove the Individual files completely
- Add word
asyncbefore the name of your function
Like this?
class Script {
aync getQuoteOfTheday() {
let file = this.app.workspace.getActiveFile();
let currentTime = moment().format('YYYY-MM-DD HH-h:mm:ssA');
let quote = await fetch("https://api.quotable.io/random").then(response => response.json());
app.vault.append(file, `\n- **${currentTime}, ${quote["author"]}:** ${quote["content"]}`)
}
}
@YousufSSyed yes, but you made a typo
Oh I made that typo while pasting it in the comment. I wrote async in the script and it still doesn't show up when trying to add an invocable script.
@YousufSSyed you didn't make it invocable
https://github.com/saml-dev/obsidian-custom-js#invocable-scripts
My Individual scripts is blank and I wrote Scripts for the folder. When I press "Register invocable script" nothing shows up.
@YousufSSyed you didn't read properly what invocable script is
I'll admit, I didn't realize "Invocable Script is the class with the defined method" meant async invoke() specifically, and I thought you would've pointed that out if I asked if it looked right.
@mnaoumov What if you wrote "Invocable Script is the class with the defined method async invoke()"
@YousufSSyed Here are all the steps in order:
- In the CustomJS settings, you can set individual files as a script, or you can set a folder that contains several JS files. It is seperate—you don't need both for the same file. (Note: If you use an individual file, make sure to add the path as well.)
- Then, open your script file and make it fit this template:
class getFonts { // you can change the name of the class
async invoke() { // you cannot change the name of this
console.log("Hello World") // you write your code here, inside "invoke"
}
}
- Go back to CustomJS settings, and add click on the button "Register invocable script". You should see your script there.
- (optional) To make your script run at startup, click on the button "Add startup script", and you should see your script there.
I think the documentation could be clearer.
@thelegend09 if you have suggestion how to make the documentation clearer, please make a PR