2022-09-30  2022-11-21    627 字  2 分钟

感谢小伙伴 Whatacold 的创意 A Bookmarklet for Copying a Link as an Org-mode Link - whatacold’s space - whatacold.io ,可以方便地复制当前页面的地址并格式化 .md 链接格式,当然,也可以格式化成 .org 链接格式。

📌 快速使用

picture 54

以下为压缩并添加前缀之后的代码,复制粘贴至上图剪头处即可。

javascript:function test(){const e=document.querySelector("#btn"),t=document.querySelector("#ipt"),o=document.querySelector("#cont");t.select(),document.execCommand("copy")?(document.execCommand("copy"),console.log("copy success"),e.innerText="🎉 success",e.style.background="#67c23a",e.style.color="#fff"):(console.log("copy failed"),e.innerText="❌ faild",e.style.background="#f56c6c",e.style.color="#fff"),setTimeout(()=>{document.body.removeChild(o)},1500)}!function(){let e="["+document.title+" - "+document.location.hostname+"]("+document.location.href+")",t=document.createElement("div");t.id="btn",t.innerText="🥳 copy",t.style="background: #e6a23c; box-shadow: 1px 1px 3px #333; width: 120px; height: 40px; text-align: center; line-height: 40px; border-radius: 4px; color: #333; cursor: pointer; font-weight: 700; font-family: segoe script, courier new;",t.addEventListener("click",test);let o=document.createElement("input");o.value=e,o.id="ipt",o.style="opacity: 0;",o.select();let c=document.createElement("div");c.id="cont",c.style="position: fixed; left: 16px; top: 16px; width: 100px;z-index: 10000;",c.appendChild(t),c.appendChild(o),document.body.appendChild(c)}();

完成后,点击标签后,会在当前网页左上角弹出复制按钮;

picture 59 picture 60 picture 61

点击复制后,按钮会更新状态及样式,并在 1.5s 自动消失。

当然,也可能失败,但希望你永远都不会看到它。

📌 源码解析

源码如下:

(function () {
	let val = "[" + document.title + " - " + document.location.hostname + "](" + document.location.href + ")"

	let btn = document.createElement("div")
	btn.id = 'btn'
	btn.innerText = "🥳 copy"
	btn.style = "background: #e6a23c; box-shadow: 1px 1px 3px #333; width: 120px; height: 40px; text-align: center; line-height: 40px; border-radius: 4px; color: #333; cursor: pointer; font-weight: 700; font-family: segoe script, courier new;"
	btn.addEventListener("click", test)

	let ipt = document.createElement("input")
	ipt.value = val
	ipt.id = "ipt"
	ipt.style = "opacity: 0;"
	ipt.select()

	let cont = document.createElement("div")
	cont.id = 'cont'
	cont.style = "position: fixed; left: 16px; top: 16px; width: 100px;z-index: 10000;"

	cont.appendChild(btn)
	cont.appendChild(ipt)
	document.body.appendChild(cont)
})();

function test() {
	const btn = document.querySelector('#btn')
	const ipt = document.querySelector('#ipt')
	const cont = document.querySelector('#cont')
	ipt.select();
	if (!!document.execCommand('copy')) {
		document.execCommand('copy')
		console.log('copy success')
		btn.innerText = '🎉 success'
		btn.style.background = '#67c23a'
		btn.style.color = '#fff'

	} else {
		console.log('copy failed')
		btn.innerText = '❌ faild'
		btn.style.background = '#f56c6c'
		btn.style.color = '#fff'
	}

	setTimeout(() => {
		document.body.removeChild(cont)
	}, 1500)
}

📌 结语

Just for fun 🎉