// ==UserScript== // @name 网页复制成Markdown // @description 获取部分页面源代码并复制为 Markdown 格式 // @author shenfangda // @license MIT // @version 1.0.0 // @match https://*.shenfangda.cn/* // @grant none // @namespace https://greasyfork.org/users/1414941 // @downloadURL none // ==/UserScript== (function() { 'use strict'; const BUTTON_STYLE = ` .button-lgcm { outline: none !important; cursor: pointer; line-height: 1.25; position: relative; display: flex; margin-left: -.0625rem; padding: .5rem .75rem; color: #fff !important; border: .0625rem solid #dee2e6; font-size: 15px; font-weight: unset; min-width: 36px; height: 36px; margin: 0 3px; border-radius: 100px !important; align-items: center; justify-content: center; transition: all .3s; background-color: #5e72e4; } .button-lgcm:hover { box-shadow: 0 7px 14px rgba(50,50,93,.1), 0 3px 6px rgba(0,0,0,.08); transform: translateY(-1px); } `; // 创建按钮的通用函数 function createButton(getContentFn) { const button = document.createElement('button'); button.textContent = '复制Md'; button.classList.add('button-lgcm'); button.style.position = 'absolute'; button.style.top = '100px'; button.style.right = '100px'; // 滚动时更新按钮位置 window.addEventListener('scroll', () => { button.style.top = `${100 + window.scrollY}px`; }); // 点击复制内容 button.addEventListener('click', async () => { try { const content = await getContentFn(); await navigator.clipboard.writeText(content); alert('复制成功'); } catch (error) { console.error('复制失败:', error); alert('复制失败,请检查控制台'); } }); document.body.appendChild(button); } // 页面类型对应的内容获取函数 const pageHandlers = { user: () => _feInstance.currentData.user.introduction, blog: async () => { const res = await fetch(`/api/blog/detail/${BlogGlobals.blogID}`); const data = await res.json(); return data.data.Content; }, contest: () => _feInstance.currentData.contest.description, training: () => _feInstance.currentData.training.description }; // URL 判断逻辑 function shouldRunHandler(url, type) { const parsedUrl = new URL(url); const path = parsedUrl.pathname.split('/'); switch (type) { case 'blog': if (url.includes('blog') && !url.includes('Admin') && !url.includes('admin')) { if (url.includes('org')) { return path.length >= 2 && path[1] !== ''; } return path.length >= 4; } return false; case 'user': return url.includes('user') && !url.includes('notification'); case 'contest': return url.includes('contest') && !url.includes('list') && !url.includes('edit') && !url.includes('contestId'); case 'training': return url.includes('training') && !url.includes('edit') && !url.includes('list'); default: return false; } } // 初始化样式 const style = document.createElement('style'); style.textContent = BUTTON_STYLE; document.head.appendChild(style); // 主逻辑 const url = window.location.href; Object.entries(pageHandlers).forEach(([type, getContentFn]) => { if (shouldRunHandler(url, type)) { window.addEventListener('load', () => createButton(getContentFn)); } }); })();