obsidian-custom-js icon indicating copy to clipboard operation
obsidian-custom-js copied to clipboard

How add custom-js to my obsidian?

Open mxcdh opened this issue 2 years ago • 22 comments

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?

mxcdh avatar Sep 18 '23 08:09 mxcdh

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 avatar Sep 20 '23 08:09 mxcdh

@mxcdh

  1. Are your scripts detectable at all? If you execute customJS from the developer console in Obsidian, do you see your scripts there?
  2. Are you sure you have async invoke() function in your script?

mnaoumov avatar Sep 21 '23 15:09 mnaoumov

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

ghost avatar Sep 24 '23 06:09 ghost

@mnaoumov

  1. It is not detectable.
  2. 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 avatar Sep 25 '23 09:09 mxcdh

@mxcdh , I am using macOS Ventura 13.5.2 (22G91)

ghost avatar Sep 25 '23 13:09 ghost

@mxcdh have you tried removing the ./ from the start of your path?

saml-dev avatar Sep 25 '23 22:09 saml-dev

@mxcdh current plugin has a few use-cases

  1. customJS global variable, which you can use in Developer Tools Console, dataviewjs, Templater scripts etc. To check if it works you should open Developer Tools Console (via Command + Shift + I) and type customJS.dvTasks. If it doesn't show an error, then your config is working properly.

  2. Invocable scripts. dvTasks.js is not an invocable script, that's why you don't see it in the list on step 4 on your screenshot

mnaoumov avatar Sep 26 '23 01:09 mnaoumov

I'm having this issue too

YousufSSyed avatar Oct 21 '23 00:10 YousufSSyed

@YousufSSyed what happens if you go to the developer console and type window.forceLoadCustomJS()?

mnaoumov avatar Oct 21 '23 14:10 mnaoumov

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. Screenshot 2023-10-22 at 11 39 27 AM

Screenshot 2023-10-22 at 11 39 44 AM

YousufSSyed avatar Oct 22 '23 15:10 YousufSSyed

@YousufSSyed

  1. that's because of your Individual files setting

  2. It looks like your Scripts/script.js is not in the correct format. It should be something like

class Script {
  func1() {
  }
}

mnaoumov avatar Oct 22 '23 15:10 mnaoumov

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"]}`)
    }
}
Screenshot 2023-10-22 at 12 14 46 PM

YousufSSyed avatar Oct 22 '23 16:10 YousufSSyed

@YousufSSyed

  1. Remove the Individual files completely
  2. Add word async before the name of your function

mnaoumov avatar Oct 22 '23 16:10 mnaoumov

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 avatar Oct 22 '23 16:10 YousufSSyed

@YousufSSyed yes, but you made a typo

mnaoumov avatar Oct 22 '23 16:10 mnaoumov

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 avatar Oct 22 '23 16:10 YousufSSyed

@YousufSSyed you didn't make it invocable

https://github.com/saml-dev/obsidian-custom-js#invocable-scripts

mnaoumov avatar Oct 22 '23 16:10 mnaoumov

My Individual scripts is blank and I wrote Scripts for the folder. When I press "Register invocable script" nothing shows up.

YousufSSyed avatar Oct 22 '23 16:10 YousufSSyed

@YousufSSyed you didn't read properly what invocable script is

mnaoumov avatar Oct 22 '23 17:10 mnaoumov

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 avatar Oct 22 '23 18:10 YousufSSyed

@YousufSSyed Here are all the steps in order:

  1. 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.)
  2. 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"
  }
}
  1. Go back to CustomJS settings, and add click on the button "Register invocable script". You should see your script there.
  2. (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 avatar Oct 23 '23 16:10 thelegend09

@thelegend09 if you have suggestion how to make the documentation clearer, please make a PR

mnaoumov avatar Oct 23 '23 16:10 mnaoumov