Using custom protocol handler for plugins

I would like to develop a plugin and make use of the logseq:// protocol handler. Unfortunately it seems to me, that this is not supported yet.

All existing handlers seems not to be working, or at least I have no idea how to use them to pass it to my plugin (yet):

In src/electron/electron/url.cljs

(defn logseq-url-handler
  "win - the main window"
  [^js win parsed-url]
  (let [url-host (.-host parsed-url)] ;; return "" when no pathname provided
    (cond
      (= "x-callback-url" url-host)
      (x-callback-url-handler win parsed-url)

      ;; identifier of graph in local
      (= "graph" url-host)
      (local-url-handler win parsed-url false)

      (= "new-window" url-host)
      (local-url-handler win parsed-url true)

      (= "handbook" url-host)
      (send-to-renderer :handbook
                        {:key  (some-> (.-pathname parsed-url) (string/replace-first #"^[\/]+" ""))
                         :args (some-> (.-searchParams parsed-url) (js/Object.fromEntries))})

Looks like we would need to add a custom handler for plugins, so they can make use of it and execute functions. Anybody knows how to do it or if I can use existing handler? I tried several things on plugin-side so far, but nothing worked.

Thanks for your help!

More context is needed.

If you are developing a plugin, why are you using the x-callback-urls? Why not just use the @logseq/libs SDK?

If you are referring to an external plugin, eg a browser extension, then you need to find a way to open the URL, eg in a browser window.

I try to implement an OAUTH 2.0 login flow to X/Twitter API from within logseq, which would require that the API can redirect to my custom logseq handler to finish the login (to connect X with logseq). Otherwise I need to start a local http server if this is possible to process the request instead.

I don’t think you can do so from within a browser environment. Which endpoint are you trying to call?

Bookmarks at the moment. It requires OAuth 2.0 PKCE flow. Yeah the easiest would be if logseq plugin API could support custom handling of the logseq protocol.

I have not touched the Twitter API in a long time but have you tried the “confidential client” approach here: OAuth 2.0 Making requests on behalf of users | Docs | Twitter Developer Platform?

Thanks for your response! Unfortunately it seems that I dont get access to these endpoints without the proper user auth flow: Twitter API v2 authentication mapping | Docs | Twitter Developer Platform

I tried it a few times but the endpoints always fail. I can only imagine its because of the endpoint restrictions.

Ah sorry I misread. Yes, it is not possible to implement it purely client side without setting up something else.

I tried my best the last days to add this feature in logseq, but I’m giving up for now. CLJS and logseq architecture would require much more time to understand it better. Maybe someone can take this over and finish it. I gave it two tries but the last step Callback from CLJS → Calling callback of my custom extension always failed. No idea why and I’m happy if I can use the protocol handler one day for plugin to interact with other applications.

1st try (no need to check it):

2nd try:

Use in plugin would be:

logseq.onProtocolHandle('twitter-oauth-callback', (params: Record<string, string>) => {
   console.log('[TwitterModule] OAuth callback detected', params);
}); // or logseq.App would fit better maybe