Telegram 上群友 David 提出的原问题是这样的
从网页中往org中复制, 怎样保留源文档的超链接?
正好咱也有这个需求,就去摸了摸鱼,下面是成果:
-
首先配置好 org-protocol,这个不是这里的重点,而且也挺罗嗦的,所以这里就不说了
-
在配置好 org-protocol 的基础上,假定对应的 org-capture 模板的标识符为
w
,那么添加下面这段 js 代码为浏览器书签javascript:location.href='org-protocol://capture?template=w&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)+'&body='+encodeURIComponent(function(){var html = "";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;}var dataDom = document.createElement('div');dataDom.innerHTML = html;dataDom.querySelectorAll('a').forEach(function(item, idx) {console.log('find a link');var url = new URL(item.href, window.location.href).href;var content = item.innerText;item.innerText = '[['+url+']['+content+']]';});['p', 'h1', 'h2', 'h3', 'h4'].forEach(function(tag, idx){dataDom.querySelectorAll(tag).forEach(function(item, index) {var content = item.innerHTML.trim();if (content.length > 0) {item.innerHTML = content + ' ';}});});return%20dataDom.innerText.trim();}())
里面其实是写了一个 js 函数来做转换,对人友好版如下:
function(){
var html = "";
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;
}
var dataDom = document.createElement('div');
dataDom.innerHTML = html;
dataDom.querySelectorAll('a').forEach(function(item, idx) {
console.log('find a link');
var url = new URL(item.href, window.location.href).href;
var content = item.innerText;
item.innerText = '[['+url+']['+content+']]';
});
['p', 'h1', 'h2', 'h3', 'h4'].forEach(function(tag, idx){
dataDom.querySelectorAll(tag).forEach(function(item, index) {
var content = item.innerHTML.trim();
if (content.length > 0) {
item.innerHTML = content + ' ';
}
});
});
return dataDom.innerText.trim();
}
最终效果如下
有更好的办法也欢迎交流啦~