Skip to content

Instantly share code, notes, and snippets.

@ghing
Last active April 19, 2024 02:16
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghing/21fd86632546363b4d668b47d3bf3166 to your computer and use it in GitHub Desktop.
Save ghing/21fd86632546363b4d668b47d3bf3166 to your computer and use it in GitHub Desktop.
User script and bookmarklet for copying current page title and URL as a Markdown link

Link copying bookmarklets and user scripts

I often want to copy the link to a page I'm viewing in the browser to the clipboard to paste it in a note or document that I'm editing. These are bookmarklets and user scripts to help with this task.

What's in here?

  • copy_markdown_link_bookmarklet.js: A bookmarklet that copies the the title and URL of the current page to the clipboard as a Markdown-formatted link. This works in Chrome, but not in Firefox, I think because "Writing to the clipboard is available without permission in secure contexts and browser extensions, but only from user-initiated event callbacks. Browser extensions with the "clipboardWrite" permission can write to the clipboard at any time." (See https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API)
  • copy_markdown_link_userscript.js: A user script that copies the the title and URL of the current page to the clipboard as a Markdown-formatted link. I wrote this because Firefox didn't seem to support providing clipboard access to a bookmarklet. I've successfully run this using the Tampermonkey user script manager.
  • copy_rendered_link_userscript.js: A user script that copies the title and URL of the current page to the clipboard as an HTML link. This script sets the mime type so if you paste the link into a Google doc or Evernote note, it will be pasted as a rendered link. I've successfully run this using the Tampermonkey user script manager.
@ghing
Copy link
Author

ghing commented Aug 14, 2023

The copy markdown link user script doesn't seem to work correctly on Google Docs when running under Tampermonkey on Firefox 116.0.2 (64-bit) on macOS. I haven't tested this on Firefox on other platforms. It has been working fine on Chrome. The user script also seems to work fine on pages that are not Google Docs.

Initially, the title would not be copied.

I noticed that the script was triggered multiple times when visiting a Google Docs URL. Adding the @noframes directive caused it only to be triggered on the main page, and the title is able to be accessed in the script via document.title, but then copying to the clipboard doesn't work.

I tried the script under Violentmonkey as well and it had the same issue.

@ghing
Copy link
Author

ghing commented Apr 19, 2024

I found the extension Copy as Markdown and it worked, even on Google Docs. I think using that description instead of the user script is the easiest option on Firefox.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment