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);
})();
@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”
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
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.
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
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.