// ==UserScript== // @name 复制为Markdown格式 // @namespace https://github.com/nameldk/user-script // @version 0.3.2 // @description 复制网页内容为Markdown格式。点击右上角copy按钮开始选择内容,点击鼠标或按Enter进行复制。按Esc取消选择,上箭头选择父级,下箭头选择子级,左箭头选择前面的相邻元素,右箭头选择后面的相邻元素。按钮可以拖动。Ctrl+c+c激活。 // @author nameldk // @require https://unpkg.com/turndown/dist/turndown.js // @match https://*/* // @match http://*/* // @match file:///* // @grant none // @downloadURL https://update.greasyfork.icu/scripts/370299/%E5%A4%8D%E5%88%B6%E4%B8%BAMarkdown%E6%A0%BC%E5%BC%8F.user.js // @updateURL https://update.greasyfork.icu/scripts/370299/%E5%A4%8D%E5%88%B6%E4%B8%BAMarkdown%E6%A0%BC%E5%BC%8F.meta.js // ==/UserScript== (function () { 'use strict'; const CLASS_HINT = "_myhint_"; let $curElement = null; let $btn = document.createElement("div"); let turndownService = new TurndownService(); let urlProtocol = document.location.protocol; let urlOrigin = document.location.origin; let urlPath = (document.location.pathname.substring(0, document.location.pathname.lastIndexOf('/'))) + '/'; let isHold = 0, isDrag = 0; function addStyle(css) { let s = document.createElement("style"); s.type = "text/css"; s.textContent = css; document.body.prepend(s); } function showHint($this) { if ($this) { $this.classList.add(CLASS_HINT); } } function hideHint($this) { if ($this) { $this.classList.remove(CLASS_HINT); } } function handleMouseover(e) { hideHint($curElement); let $target = e.target; $curElement = $target; showHint($target); } function handleMouseout(e) { let $target = e.target; hideHint($target); } function handleKeyup(e) { function isValidNode(node) { return node && node.textContent && node.textContent.trim() !== ""; } function findNextNode(node) { if (node) { var findNode = node.nextElementSibling; while(findNode) { if (isValidNode(findNode)) { return findNode; } else { if (findNode.nextElementSibling) { findNode = findNode.nextElementSibling; } } } } return null; } if (e.keyCode === 13) { // enter process(e); return false; } else if (e.keyCode === 27) { // esc disable(); } else if (e.keyCode === 38) { // arrow up if ($curElement && isValidNode($curElement.parentElement)) { hideHint($curElement); $curElement = $curElement.parentElement; showHint($curElement); } } else if (e.keyCode === 37) { // arrow left if ($curElement && isValidNode($curElement.previousElementSibling)) { hideHint($curElement); $curElement = $curElement.previousElementSibling; showHint($curElement); } } else if (e.keyCode === 40) { // arrow down let nextNode = null; if ($curElement && (nextNode = findNextNode($curElement.firstElementChild))) { hideHint($curElement); $curElement = nextNode; showHint($curElement); } } else if (e.keyCode === 39) { // arrow right if ($curElement && isValidNode($curElement.nextElementSibling)) { hideHint($curElement); $curElement = $curElement.nextElementSibling; showHint($curElement); } } } function disableScroll(e) { if ([38, 40, 37, 39].indexOf(e.keyCode) > -1) { e.preventDefault(); } } function handleClick(e) { process(e); return false; } function process(e) { if ($curElement) { e.preventDefault(); copyIt($curElement); disable(); showTips(); } } function showTips() { let t = document.createElement("div"); t.style.position = "fixed"; t.style.width = "80px"; t.style.height = "24px"; t.style.lineHeight = "24px"; t.style.top = "10px"; t.style.right = "50%"; t.style.background = "#68af02"; t.style.fontSize = "14px"; t.style.color = "#fff"; t.style.textAlign = "center"; t.style.borderRadius = "5px"; t.style.marginLeft = "300px"; t.style.zIndex = 10000; t.innerHTML = "复制成功"; document.body.prepend(t); setTimeout(function () { document.body.removeChild(t); }, 1000); } function copyIt($curElement) { if ($curElement) { let html = $curElement.innerHTML; html = html .replace(//gi, processFigure) .replace(/]+>/gi, processImg) .replace(/()/gi, parseHref) ; let markdown = turndownService.turndown(html); markdown = markdown.replace(//g, ""); copyToClipboard(markdown); } } function processFigure(str) { str = str.replace(/