【油猴脚本】复制MD格式的网页标题和链接,[标题](链接)

在网页上按 Shift+q 直接复制MD格式的网页标题和链接,[标题](链接),方便写文章。

// ==UserScript==
// @name         获取[标题](链接)
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  try to take over the world!
// @author       tfnmdmx
// @match        *://*/*
// @run-at       document_start
// @grant        unsafeWindow
// @require      https://cdn.jsdelivr.net/npm/sweetalert2@11
// @icon         https://www.google.com/s2/favicons?sz=64&domain=csdn.net
// ==/UserScript==

(function() {
    'use strict';
    async function copyPageUrl(md) {
        try {
            await navigator.clipboard.writeText(md);
            console.log('已复制:'+md);
            Swal.fire({
                toast: true,
                position: 'bottom-end',
                type: 'success',
                title: "已复制!",
                showConfirmButton: false,
                timer: 1500
            });
        } catch (err) {
            try {
                console.log('不安全的链接http');
                // 创建text area
                const textArea = document.createElement('textarea')
                textArea.value = md
                // 使text area不在viewport,同时设置不可见
                document.body.appendChild(textArea)
                textArea.focus()
                textArea.select()
                return new Promise((res, rej) => {
                    // 执行复制命令并移除文本框
                    document.execCommand('copy') ? res() : rej()
                    textArea.remove()
                })
                console.log('已复制:'+md);
                Swal.fire({
                    toast: true,
                    position: 'bottom-end',
                    type: 'success',
                    title: "已复制!",
                    showConfirmButton: false,
                    timer: 1500
                });
            } catch (e) {
                console.error('Failed to copy: ', e);
                Swal.fire({
                    toast: true,
                    position: 'bottom-end',
                    type: 'error',
                    title: "出错了!",
                    showConfirmButton: false,
                    timer: 1500
                });
            }
        }
    }
    window.addEventListener('keydown' , (e) => {
        //console.log(e.keyCode);
        if(e.shiftKey && e.keyCode== 81){
            var url=window.location.href
            var title=document.title
            console.log('title:'+title);
            console.log('url:'+url);

            if(title.indexOf("CSDN博客") != -1){
                var pos = title.indexOf('_');
                if(title.indexOf('_', pos) != -1){
                    pos = title.indexOf('_', pos);
                }
                title = title.substr(0,pos);
            }else if(title.indexOf("博客园") != -1 || title.indexOf("简书") != -1 || title.indexOf("知乎") != -1 || title.indexOf("搜索") != -1){
                pos = title.indexOf('-');
                if(title.indexOf('-', pos) != -1){
                    pos = title.indexOf('-', pos);
                }
                title = title.substr(0,pos);
            }else if(title.indexOf("菜鸟教程") != -1){
                pos = title.indexOf('|');
                if(title.indexOf('|', pos) != -1){
                    pos = title.indexOf('|', pos);
                }
                title = title.substr(0,pos);
            }
            var md = "["+title+"]("+url+")";
            copyPageUrl(md);
        }
    })
})();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tfnmdmx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值