前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >拷贝浏览器链接到 org-mode

拷贝浏览器链接到 org-mode

作者头像
飞驰的西瓜
发布2022-07-26 16:41:33
4870
发布2022-07-26 16:41:33
举报
文章被收录于专栏:EmacsTalk

在 org-mode 里写博客文章时,经常会旁征博引一些链接,之前都是用 C-c C-l(org-insert-link) 的快捷键来输入链接,但这是个费事费力的事情,需要去浏览器里面拷贝两次,一次是链接,一次是标题,有没有更好地方式呢?答案是肯定的。不同浏览器做法大同小异,这里以 macOS 上的 Safari 与 Firefox 为例说明。

Safari

在 macOS 中,可以使用下面的 applescript 脚本来获取 org 格式的链接:

代码语言:javascript
复制
#!/usr/bin/osascript

# @raycast.title Copy URL as org-capture
# @raycast.packageName org-capture-url-safari
# @raycast.mode silent
# @raycast.schemaVersion 1


tell application "Safari"
    set tab_link to URL of front document
    set tab_title to NAME of front document
    set org_link to ("[[" & tab_link & "]" & "[" & tab_title & "]]")
    set the clipboard to org_link
    display notification org_link with title "链接复制成功"
end tell

我这里封装成了 Raycast 中可以调用的格式。

Raycast 调用示范

Firefox

按理来说,Firefox 也可以参考 Safari 的做法,但是 Firefox 有个长达 20 年的 bug:

•125419 - [META]Add AppleScript support and capabilities to Mozilla on Mac, e.g. AppleScript/OSA Script menu[1]

因此只能另辟蹊径,虽然网上有些 hack[2] 的解法,但是我觉得太复杂了,不如直接用 userscript 来解决(完整代码可参考 web-util.js[3]):

代码语言:javascript
复制
function copy_as_org_capture() {
  const url = window.location.href;
  const title = window.document.title;
  GM_setClipboard(`[[${url}][${title}]]`);
  alert(title);
}
GM_registerMenuCommand("org-capture", copy_as_org_capture);

VM.shortcut.register('c-i', copy_as_org_capture);

这样既能在快捷菜单中,通过点击来实现链接的复制,也能通过快捷键 C-i 来复制。

油猴脚本快捷菜单

参考

•Violentmonkey API[4]

•Getting URL and Tab Title from Firefox with AppleScript · Matt's programming blog[5]

•使用 Emacs + ox-hugo 来写博客 · Jiayuan Thoughts[6]

引用链接

[1] 125419 - [META]Add AppleScript support and capabilities to Mozilla on Mac, e.g. AppleScript/OSA Script menu: https://bugzilla.mozilla.org/show_bug.cgi?id=125419 [2] hack: https://matthewbilyeu.com/blog/2018-08-24/getting-url-and-tab-title-from-firefox-with-applescript [3] web-util.js: https://github.com/jiacai2050/blog-snippets/blob/master/userscript/web-util.js [4] Violentmonkey API: https://violentmonkey.github.io/api/gm/#gm_registermenucommand [5] Getting URL and Tab Title from Firefox with AppleScript · Matt's programming blog: https://matthewbilyeu.com/blog/2018-08-24/getting-url-and-tab-title-from-firefox-with-applescript [6] 使用 Emacs + ox-hugo 来写博客 · Jiayuan Thoughts: http://blog.jiayuanzhang.com/post/blog-with-ox-hugo/

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-12-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 EmacsTalk 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Safari
  • Firefox
  • 参考
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档