因为我一直用的 macOS,所以 OSA Script 来控制浏览器来完成 HTML 抓取,然后通过 org-protocol 输入到 Emacs
tell application (path to frontmost application as text)
if name is equal to "Safari" then
tell application "Safari"
set title to quoted form of (do JavaScript "encodeURIComponent(document.title)" in current tab of first window)
set link to quoted form of (do JavaScript "encodeURIComponent(window.location.href)" in current tab of first window)
set body to quoted form of (do JavaScript "encodeURIComponent((function () {var html = \"\"; if (typeof window.getSelection != \"undefined\") {var sel = window.getSelection(); if (sel.rangeCount) {var container = document.createElement(\"div\"); for (var i = 0, len = sel.rangeCount; i < len; ++i) {container.appendChild(sel.getRangeAt(i).cloneContents());} html = container.innerHTML;}} else if (typeof document.selection != \"undefined\") {if (document.selection.type == \"Text\") {html = document.selection.createRange().htmlText;}} var relToAbs = function (href) {var a = document.createElement(\"a\"); a.href = href; var abs = a.protocol + \"//\" + a.host + a.pathname + a.search + a.hash; a.remove(); return abs;}; var elementTypes = [['a', 'href'], ['img', 'src']]; var div = document.createElement('div'); div.innerHTML = html; elementTypes.map(function(elementType) {var elements = div.getElementsByTagName(elementType[0]); for (var i = 0; i < elements.length; i++) {elements[i].setAttribute(elementType[1], relToAbs(elements[i].getAttribute(elementType[1])));}}); return div.innerHTML;})());" in current tab of first window)
tell application "Emacs" to activate
do shell script "export prefix=/usr/local; if [ -e /opt/homebrew ]; then prefix=/opt/homebrew; fi; $prefix/bin/emacsclient -e '(org-protocol-capture-html--with-pandoc-patch \"'" & "w" & "'\" \"'" & title & "'\" \"'" & link & "'\" \"'" & body & "'\")'"
do shell script "export prefix=/usr/local; if [ -e /opt/homebrew ]; then prefix=/opt/homebrew; fi; $prefix/bin/terminal-notifier -title 'Page captured' -sender org.gnu.Emacs -sound Purr"
end tell
else if name is equal to "Microsoft Edge" then
tell application "Microsoft Edge"
set xtitle to quoted form of (execute active tab of first window javascript "encodeURIComponent(document.title)")
set xlink to quoted form of (execute active tab of first window javascript "encodeURIComponent(window.location.href)")
set xbody to quoted form of (execute active tab of first window javascript "encodeURIComponent((function () {var html = \"\"; if (typeof window.getSelection != \"undefined\") {var sel = window.getSelection(); if (sel.rangeCount) {var container = document.createElement(\"div\"); for (var i = 0, len = sel.rangeCount; i < len; ++i) {container.appendChild(sel.getRangeAt(i).cloneContents());} html = container.innerHTML;}} else if (typeof document.selection != \"undefined\") {if (document.selection.type == \"Text\") {html = document.selection.createRange().htmlText;}} var relToAbs = function (href) {var a = document.createElement(\"a\"); a.href = href; var abs = a.protocol + \"//\" + a.host + a.pathname + a.search + a.hash; a.remove(); return abs;}; var elementTypes = [['a', 'href'], ['img', 'src']]; var div = document.createElement('div'); div.innerHTML = html; elementTypes.map(function(elementType) {var elements = div.getElementsByTagName(elementType[0]); for (var i = 0; i < elements.length; i++) {elements[i].setAttribute(elementType[1], relToAbs(elements[i].getAttribute(elementType[1])));}}); return div.innerHTML;})());")
tell application "Emacs" to activate
do shell script "export prefix=/usr/local; if [ -e /opt/homebrew ]; then prefix=/opt/homebrew; fi; $prefix/bin/emacsclient -e '(org-protocol-capture-html--with-pandoc-patch \"'" & "w" & "'\" \"'" & xtitle & "'\" \"'" & xlink & "'\" \"'" & xbody & "'\")'"
do shell script "export prefix=/usr/local; if [ -e /opt/homebrew ]; then prefix=/opt/homebrew; fi; $prefix/bin/terminal-notifier -title 'Page captured' -sender org.gnu.Emacs -sound Purr"
end tell
end if
end tell