Skip to content

Conversation

xyhp915
Copy link
Collaborator

@xyhp915 xyhp915 commented Oct 11, 2021

How to use custom.js?

0. Why custom.js?

  • Different from the execution environment of the plug-in code (in a separate sandbox), the code inside of custom.js will be executed on the main thread, which means you can access DOM.
  • Access low-level plugin API logseq.api.xxx, which means you can invoke api synchronously if supported.

1. Life & Security of custom.js?

  • Each graph can have a custom.js file, under the logseq/ folder, similar to custom.css.
  • When a graph is loaded, it will try to load custom.js asynchronously.
  • Before executing the code, the user will be asked for execution permission.
  • When the content of the custom.js file is modified, it needs to be restarted or refreshed to take effect.

2. Hello custom.js

  • Create custom.js file under logseq/ folder and write code as below.
logseq.api.show_msg('hello custom.js')
  • Reindex graph and reload (cmd+R) the app, then allow executing codes.
  • When the graph is loaded, a tip will be shown.

2021-10-13 14 44 46

@xyhp915 xyhp915 changed the title feat: support custom js [wip]feat: support custom js Oct 12, 2021
@Inviz
Copy link

Inviz commented Oct 12, 2021

Looking forward to this. I think it should have plugin API available in that file. The same API as in plugins (not the internal logseq.api)

Copy link

@Inviz Inviz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@xyhp915 xyhp915 changed the title [wip]feat: support custom js feat: support custom js Oct 13, 2021
@xyhp915 xyhp915 requested a review from tiensonqin October 13, 2021 06:07
@Inviz
Copy link

Inviz commented Oct 16, 2021

Any news on this?

@xyhp915
Copy link
Collaborator Author

xyhp915 commented Oct 16, 2021

Any news on this?

Just waiting for a reviewer. Will be processed soon 😃

@tiensonqin tiensonqin merged commit d16c7be into master Oct 18, 2021
@tiensonqin tiensonqin deleted the feat/support-custom-js branch October 18, 2021 05:11
@Inviz
Copy link

Inviz commented Oct 18, 2021

Ahhh yyyyesss. thanks

@prurph
Copy link

prurph commented Dec 21, 2021

I'm trying to use this feature but when I reload with custom.js Logseq doesn't alert me that it has found a custom.js file or ask to execute it. Any thoughts on how to fix this?

I'm running 0.5.4 on MacOS.

@cannibalox
Copy link

same issue here on 0.5.4 win10, custom.js doesn't seem to load and got no alert asking for permission

@jjaychen1e
Copy link

Same issue here

@xyhp915
Copy link
Collaborator Author

xyhp915 commented Mar 14, 2023

Same issue here

Hi @jjaychen1e, What's the version of your desktop app? I tested it, and it works for me on Mac & Windows.

@jjaychen1e
Copy link

jjaychen1e commented Mar 14, 2023

Same issue here

Hi @jjaychen1e, What's the version of your desktop app? I tested it, and it works for me on Mac & Windows.

I test this feature with a new created graph and it works (macOS, Logseq 0.8.18). But for my original graph, the prompt doesn't show up (using the same custom.js).

And then I move my original graph from iCloud (iCloud~com~logseq~logseq/Documents) to ~/Downloads/Logseq, it works well.

Then, I unlink the original graph and open it again, it doesn't work. Weeks ago, this feature works well, it seems it's broken in the recent versions. It seems the location of the graph can affect this feature. I notice there is a PR about enabling custom.js in the mobile platforms, I don't know if it's related: #5441

Update: ~/Library/Mobile Documents/iCloud~com~logseq~logseq/Documents doesn't work, but ~/Library/Mobile Documents/com~apple~CloudDocs/Logseq works well. The former is created by the logseq app, and the latter is created manually.

@xyhp915
Copy link
Collaborator Author

xyhp915 commented Mar 14, 2023

And then I move my original graph from iCloud (iCloudcomlogseq~logseq/Documents) to ~/Downloads/Logseq, it works well.

@jjaychen1e Thanks for your details! Will do more tests soon :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants