Skip to content

Instantly share code, notes, and snippets.

@taowen
Last active April 13, 2024 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taowen/95ae056924f33bafa809cb4147e52566 to your computer and use it in GitHub Desktop.
Save taowen/95ae056924f33bafa809cb4147e52566 to your computer and use it in GitHub Desktop.
// ==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