Last active
November 28, 2024 05:31
-
-
Save taowen/95ae056924f33bafa809cb4147e52566 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name 拷贝任意选中的 HTML | |
// @description 方便粘贴到 chatgpt 进行问答 | |
// @namespace github.com/taowen | |
// @match *://*/* | |
// @version 1.0.0 | |
// @author taowen | |
// @license MIT | |
// @grant GM.registerMenuCommand | |
// @grant GM_setClipboard | |
// @grant GM.getValue | |
// @grant GM.setValue | |
// @grant GM.xmlHttpRequest | |
// @require https://unpkg.com/turndown/dist/turndown.js | |
// ==/UserScript== | |
function extractSelectedHtml() { | |
let html = ""; | |
const sel = window.getSelection(); | |
if (sel.rangeCount) { | |
var container = document.createElement("div"); | |
for (var i = 0, len = sel.rangeCount; i < len; ++i) { | |
var frag = sel.getRangeAt(i).cloneContents(); | |
container.appendChild(frag); | |
} | |
html = container.innerHTML; | |
} | |
return html | |
} | |
GM.registerMenuCommand("复制当前选中 Html 为 MarkDown", () => { | |
const html = extractSelectedHtml(); | |
const turndownService = new TurndownService() | |
const markdown = turndownService.turndown(html) | |
GM_setClipboard (markdown); | |
alert('copied ' + markdown.length + ' characters'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment