Bookmarklet: Send to Obsidian

I made a quick bookmarklet to save content from a webpage to Obsidian.

If you like, you can grab it by dragging this link to your bookmarks bar:

Send to Obsidian

Then, edit it and change the link to the code below by copying and pasting.

javascript:(function(){

	const title = document.title;
	const selectedText = window.getSelection().toString();
	const url = document.location.href;
	const tag = '#saved_from_chrome';
	const content = `# ${title}
${selectedText != "" ? `${"\n"} > ${selectedText.replaceAll("\n", "\n> ")}` : ""	}
${"\n\n"}
- [${title}](${url})
${"\n\n"}
${tag}`;
	document.location.href = `obsidian://new?name=${encodeURIComponent(title)}&content=${encodeURIComponent(content)}`
})();
11 Likes

Great idea! Can you be more specific about how to make this work? I tried to replace changeme (the two hashtags were not removed) from the following link with the script, but it did not work for me.
https://forum.obsidian.md/t/bookmarklet-send-to-obsidian/13415#changeme#

Thanks for this! I also added &vault=my-vault to the script to be able to send to a specific vault.

1 Like

You need to delete the entire url and replace it with the code, not just the changeme part.

Additionally, as @gregxvx suggested, you can (should?) add your vault name.

See below (it’s at the end of the 2nd to last line that begins with document.location):

javascript:(function(){

	const title = document.title;
	const selectedText = window.getSelection().toString();
	const url = document.location.href;
	const tag = '#saved_from_chrome';
	const content = `# ${title}
${selectedText != "" ? `${"\n"} > ${selectedText.replaceAll("\n", "\n> ")}` : ""	}
${"\n\n"}
- [${title}](${url})
${"\n\n"}
${tag}`;
	document.location.href = `obsidian://new?name=${encodeURIComponent(title)}&content=${encodeURIComponent(content)}&vault=VAULTNAMENERE`
})();
2 Likes

Thanks for the help. I replaced the link with the code. However, after clicking the bookmark, it navigates to the active pane in Obsidian but did not paste anything into the note.

I tried to test this bookmark with the current post page, and it did not work. It’s working for other websites.

I had the same issue. It worked on other pages, but not this one.

I think it comes from the fact that selected / highlighted text here triggers the quote feature of the forum (and thus probably overriding what the bookmarklet tries to do).

1 Like

This is really cool. I’m gonna add this to my bookmark manager!

Great addition.

I think it may not work with this page if you’re using Obsidian in Windows because the file it’s trying to create contains a colon character (: ) as it’s obtained from the page title. And Windows doesn’t allow it.

To fix this minor issue I’ve just cleaned the filename replacing non-word characters from the title with a hyphen (-):

const file = title.replaceAll(/[^\w]/ig, '-').replaceAll(/[_-]+/g, '-');

The final bookmarklet looks like this:

javascript:(function(){

	const title = document.title;
        const file = title.replaceAll(/[^\w]/ig, '-').replaceAll(/[_-]+/g, '-');
	const selectedText = window.getSelection().toString();
	const url = document.location.href;
	const tag = '#saved_from_chrome';
	const content = `# ${title}
${selectedText != "" ? `${"\n"} > ${selectedText.replaceAll("\n", "\n> ")}` : ""	}
${"\n\n"}
- [${title}](${url})
${"\n\n"}
${tag}`;
	document.location.href = `obsidian://new?name=${encodeURIComponent(file)}&content=${encodeURIComponent(content)}&vault=VAULTNAMEHERE`
})();
4 Likes

Great! Now this version works for me on macOS when saving the current post page.

1 Like

Great stuff!

Here’s my version plucking up the parts that can be customized:

javascript: (function () {
  const vault = "zettelkasten";
  const prefix = "Clips/";
  const tag = "#saved_with_bookmarklet";

  const title = document.title;
  const file = title.replaceAll(/[^\w]/gi, "-").replaceAll(/[_-]+/g, "-");
  const selectedTextNotGoogleDocs = window.getSelection().toString().trim();
  const url = document.location.href;
  const content = `# ${title}${
    selectedTextNotGoogleDocs != ""
      ? `${"\n"} > ${selectedTextNotGoogleDocs.replaceAll("\n", "\n> ")}`
      : ""
  }${"\n\n"}- [${title}](${url})${"\n\n"}${tag}\n`;
  document.location.href = `obsidian://new?name=${prefix}${encodeURIComponent(
    file
  )}&content=${encodeURIComponent(content)}&vault=${vault}`;
})();

Note: quoting the selectedText doesn’t work in Google apps. See https://stackoverflow.com/a/11288003 for an explanation of why

1 Like