惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
量子位
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog
A
About on SuperTechFans
腾讯CDC
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
雷峰网
雷峰网
Last Week in AI
Last Week in AI
H
Help Net Security
WordPress大学
WordPress大学
博客园 - 司徒正美
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Tailwind CSS Blog
博客园 - 【当耐特】
S
SegmentFault 最新的问题
美团技术团队
M
MIT News - Artificial intelligence
L
LangChain Blog
博客园 - 聂微东

Darek Kay

Sabbatical #18: Great Ocean Road Sabbatical #17: Melbourne Sabbatical #16: Kaikōura Sabbatical #15: Tasman-Marlborough Sabbatical #14: West Coast Sabbatical #13: Wānaka Sabbatical #12: Milford Sound Sabbatical #11: Queenstown Sabbatical #10: Mackenzie Basin Sabbatical #09: Dunedin Sabbatical #08: Christchurch Sabbatical #07: Waitomo Sabbatical #06: Tongariro National Park Sabbatical #05: Rotorua Lakes Sabbatical #04: Coromandel Peninsula Sabbatical #03: Auckland Sabbatical #02: Doha Sabbatical #01: Getting ready Open Graph images: Format compatibility across platforms
Grab browser links and titles in one click
Darek Kay · 2025-01-03 · via Darek Kay

When I copy a browser tab URL, I often want to also keep the title. Sometimes I want to use the link as rich text (e.g., when pasting the link into OneNote or Jira). Sometimes I prefer a Markdown link. There are browser extensions to achieve this task, but I don't want to introduce potential security issues. Instead, I've written a bookmarklet based on this example extension.

To use it, drag the following link onto your browser bookmarks bar:

Copy Tab

When you click the bookmark(let), the current page including its title will be copied into your clipboard. You don't even have to choose the output format: the link is copied both as rich text and plain text (Markdown). This works because it's possible to write multiple values into the clipboard with different content types.

Here's the source code:

function escapeHTML(str) {
  return String(str)
    .replace(/&/g, "&")
    .replace(/"/g, """)
    .replace(/'/g, "'")
    .replace(/</g, "&lt;")
    .replace(/>/g, "&gt;");
}

function copyToClipboard({ url, title }) {
  function onCopy(event) {
    document.removeEventListener("copy", onCopy, true);
    // hide the event from the page to prevent tampering
    event.stopImmediatePropagation();
    event.preventDefault();

    const linkAsMarkdown = `[${title}](${url})`;
    event.clipboardData.setData("text/plain", linkAsMarkdown);

    const linkAsHtml = `<a href="${escapeHTML(url)}">${title}</a>`;
    event.clipboardData.setData("text/html", linkAsHtml);
  }
  document.addEventListener("copy", onCopy, true);
  document.execCommand("copy");
}

copyToClipboard({ url: window.location.toString(), title: document.title });