ChatGPT 代码导出按钮

为 ChatGPT 响应中的代码块添加导出按钮,提示用户根据代码块的类名检测到的编程语言将代码保存为文件。

< 脚本ChatGPT 代码导出按钮的反馈

评价:好评 - 脚本运行良好

Sometimes the button was inserted after the Copy code button, so I fixed it by changing the code:

// Insert "Export" button after the language label
languageLabel.parentNode.insertBefore(exportButton, languageLabel.nextSibling);

To

// Find the element containing "Copy code"
let copyCodeDiv = null;
const elements = header.querySelectorAll('.flex.items-center.relative.text-token-text-secondary.bg-token-main-surface-secondary.px-4.py-2.text-xs.font-sans.justify-between.rounded-t-md div');
elements.forEach(element => {
if (element.innerHTML.includes('Copy code')) {
copyCodeDiv = element;
}
});

// Insert "Export" button before the found element or append to header if not found
if (copyCodeDiv) {
copyCodeDiv.parentNode.insertBefore(exportButton, copyCodeDiv);
} else {
header.appendChild(exportButton);
}

§
发表于:2024-07-11
编辑于:2024-07-11

Thanks a lot, it really fixed it!

发表回复

登录以发表回复。