Automatic paste in note

Hi, I found here a bookmarklet script to fetch a webpage address in markdown link format to the clipboard.
I’d like to modify it so that it automatically pastes what it got inside the currently active note editor, but I can’t do it.
I’m not an expert on these things and I can’t find a uri command that identifies the currently active note… It is possible to identify a certain note, sure, but I need to identify the currently active tab or note and tell Obsidian to copy the obtained markdown link into it
Can anyone help me with this?

javascript:(function() {

function copyToClipboard(text) {
    if (window.clipboardData && window.clipboardData.setData) {
        /*IE specific code path to prevent textarea being shown while dialog is visible.*/
        return clipboardData.setData("Text", text); 

    } else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
        var textarea = document.createElement("textarea");
        textarea.textContent = text;
        textarea.style.position = "fixed";  /* Prevent scrolling to bottom of page in MS Edge.*/
        document.body.appendChild(textarea);
        textarea.select();
        try {
            return document.execCommand("copy");  /* Security exception may be thrown by some browsers.*/
        } catch (ex) {
            console.warn("Copy to clipboard failed.", ex);
            return false;
        } finally {
            document.body.removeChild(textarea);
        }
    }
}

var markdown = '[' + document.title + '](' + window.location.href + ')';
var selection = window.getSelection().toString();
if (selection.length != 0) {
    selection = '\n' + selection;
}
copyToClipboard(markdown + selection);
})();

You could look into the Advanced URI plugin, tho I suspect that doesn’t have it either.

@franksisco, I have some bad news and good news.
Like @CawlinTeffid said, and as is my own experience, active document will be an issue.

The good news, I am now invoking AlanG, who can tell you in two secs the Obsidian API + Templater js hooking version.

It’s a good a day to be alive. :slight_smile:

@CawlinTeffid I had already tried Advanced URI but could not find anything that could identify an active note regardless of the name.

@gino_m unfortunately I have no idea how to use Obsidian API, my skills in this field don’t go beyond stealing bits of scripts here and there that I understand 20% of maybe… But I’m staying tuned. As we say in Italy: “Tutto fa brodo” = “everything is useful to make broth”

I think we could be on the same level then, as far as coding skills are concerned.

In this post, AlanG writes how he fishes for code he can make use of:

I tried now, but you need to be on a correct page where a particular plugin is doing its thing.
Anyway, we’ll know it sooner or later.

I was searching for metadataCache and found this:

If you manage to cook something up, pass along a slice of the pizza, cheers :slight_smile:

Just to clarify that we are not at the same level as you think: I don’t know what metadataCache is and what it is for, I don’t understand that script. I only see one thing that sounds good to me: “app.workspace.getActiveFile” but I don’t know how to use it.
I am asking ChatGPT to explain it to me. Really sad isn’t it? I didn’t even understand if I need to install that plugin or not.
No, we are not at the same level at all :sweat_smile:

1 Like

To have the external bookmarklet put data into Obsidian, you can do it two ways:

  1. Easy: Send to a specific note, optionally appending the data.

  2. Harder: Register a custom protocol handler by creating a very simple plugin. This will let you paste the data in the active note, rather than a specific note.

Okay thank you very much. I was hoping there was a simple or even obvious change to make to the script to allow it to copy its contents to the currently active note. Yes that was the point, the active note, whatever its name is.
Most of all I hoped no plugins were needed but just a bookmarklet with the right code.
Unfortunately, due to my ignorance and inability, your solution sounds to me like showing a map to a lost dog…
But I will try to study it, thanks anyway

@AlanG Just a little help: if in some weeks or months I’ll figure out how to install that plugin, what should I type into the bookmarklet script to get it to tell ObsidianProtocolHandler to paste what it has captured into the editor of the currently active/open note?

There could be an option inbetween also, I think: What if the bookmarklet paste the info into a timestamped file, and then make a command (or template) in Obsidian picking up the latest timestamped file and moving it into the active note?

This approach would require an extra keystroke (or two) but it would still be better than manually typing whatever you need into the active note.

@holroy that’s quite brilliant, and definitely achievable with just the bookmarklet and Templater

Wait, if you’re doing another keystroke you can just use the “paste” keystroke :stuck_out_tongue:

I should have specified that I would like to be able to do this on both desktop and mobile. I can’t use keystroke on mobile. However, I’m studying Build a plugin - Developer Documentation
to figure out how to build this Plugin_2.registerObsidianProtocolHandler, I was able to install the sample plugin correctly but I just can’t figure out how to turn it into ObsidianProtocolHandler

Nice work!!

You can look at the protocol handler in this plugin of mine:

  1. It creates a handler called share-note.

  2. My external process (in your case the bookmarklet) sends the command:

window.open('obsidian://share-note?myprop=somevalue')
  1. Obsidian then opens, and the protocol handler receives the data. You would use data.myprop in the handler and it would contain the value somevalue.

I thank you very much, I’m having a hard time understanding but at least I don’t feel abandoned. The hardest thing for me to understand is how where to put this information. I just see that your code is taken from a main.ts file, and I see that inside obsidian-sample-plugin folder (which if I understand, maybe, is a base/template to create a plugin) there is a main.ts file.
Should I replace the code in my main.ts with the snippet from yours? Or should I add it to the end of it?

Just an addition to the previous message:
After hopefully solving the problem of where to write what for the plugin… To simplify, for better understanding, let’s minimize the bookmarklet’s task: what should be the bookmarklet’s code to just inset the word “Hello” to the current active note?
obsidian://share-note?data.myprop=Hello
maybe?

@AlanG since I couldn’t figure out how to create a plugin with just your main.ts snippet I thought to simply installing your obsidian-share plugin which certainly has that “share-note” handler working.
I installed it and activated the api but when I launch obsidian://share-note?myprop=somevalue in my browser no “somevalue” is written in my active note
So if it doesn’t work this way either, I can quit without remorse.

One needs to look around and look for bits of info one can make use of.

SImilar function here:

Another:

Thanks for the support gino_m, I really appreciate your patience. I’ve been reasoning about the links you sent me for 3 days but so far I haven’t figured out how to use that information for my case. Anyway I continue

I am actually cross-linking stuff* so not necessarily as a way to show I am so patient with anyone.
I saw another yesterday, these are all Templater commands, right and the big boys (who shoot a few bullets and then dust off their pants and boots and go back to their Wurlitzer and their whiskeys) know how to juggle them, but we don’t. But by God, we will.

* I also heavily bookmark posts for myself and name them so I can search in them.