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

推荐订阅源

S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
爱范儿
爱范儿
博客园 - 聂微东
V
Visual Studio Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
M
MIT News - Artificial intelligence
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
L
LangChain Blog
I
InfoQ
T
Tailwind CSS Blog
博客园 - 【当耐特】
V
V2EX
博客园_首页
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
GbyAI
GbyAI
Vercel News
Vercel News
雷峰网
雷峰网
量子位
A
About on SuperTechFans
Martin Fowler
Martin Fowler
H
Help Net Security

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 });