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

推荐订阅源

Jina AI
Jina AI
N
Netflix TechBlog - Medium
P
Proofpoint News Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
aimingoo的专栏
aimingoo的专栏
Stack Overflow Blog
Stack Overflow Blog
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
I
InfoQ
F
Fortinet All Blogs
J
Java Code Geeks
Last Week in AI
Last Week in AI
美团技术团队
大猫的无限游戏
大猫的无限游戏
有赞技术团队
有赞技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件

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