使用Bookmarklet书签快速生成markdown参考链接

痛点

写文章标注参考链接时手动生成markdown链接很麻烦
例如[Google](https://www.google.com/)
很长一段时间我都使用chrome扩展进行获取
Copy Title and Url as Markdown Style
对于macOS的Safari无能为力

解决

思路:使用包含javascript代码的Bookmarklet书签获取title+url拼接为markdown格式
只需添加书签并将js代码填入地址即可,由于使用js理论上支持所有平台浏览器调用

弹窗版

弹出窗口手动复制markdown链接

javascript:window.prompt("Copy to clipboard: Ctrl+C, Enter",'['+document.title+']('+window.location.href+')');

复制版

直接将markdown链接写入剪贴板

javascript:(function() {

function copyToClipboard(text) {
    if (window.clipboardData && window.clipboardData.setData) {
        /*IE specific code path to prevent textarea being shown while dialog is visible.*/
        return clipboardData.setData("Text", text); 

    } else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
        var textarea = document.createElement("textarea");
        textarea.textContent = text;
        textarea.style.position = "fixed";  /* Prevent scrolling to bottom of page in MS Edge.*/
        document.body.appendChild(textarea);
        textarea.select();
        try {
            return document.execCommand("copy");  /* Security exception may be thrown by some browsers.*/
        } catch (ex) {
            console.warn("Copy to clipboard failed.", ex);
            return false;
        } finally {
            document.body.removeChild(textarea);
        }
    }
}

var markdown = '[' + document.title + '](' + window.location.href + ')';
copyToClipboard(markdown);
})();

参考

How do I copy a tab's title in Chrome? - Super User
Bookmarklet to copy current page title and url in Markdown format to clipboard, like [title](url) - Usual for posting links to resources in README.md files

原文地址:https://www.cnblogs.com/azureology/p/15708917.html