返回资讯中心
外部精选
软件工程
#社区热议

Things to know when you get cancer

document.body.setAttribute('data-copy-code', copyCode ? 'enabled' : 'disabled')

Hacker Newsjjmaxwell42026/10/14 · 14 天前7 分钟阅读

以下正文同步自 Hacker News,版权归原站所有,已转换为易读排版。

`

const copiedIcon = `

`

document.body.setAttribute('data-copy-code', copyCode ? 'enabled' : 'disabled')

if (!copyCode) { return }

const copyButtons = document.querySelectorAll('.copy-button')

copyButtons.forEach((button) => { const preElement = button.closest('.copy-code-block') if (!preElement) return

const codeElement = preElement.querySelector('code') if (!codeElement) return

if (!button.querySelector('svg')) { button.innerHTML = copyIcon }

button.style.opacity = '0' button.style.pointerEvents = 'none'

preElement.addEventListener('mouseenter', () => { button.style.opacity = '1' button.style.pointerEvents = 'auto' })

preElement.addEventListener('mouseleave', () => { if (!button.hasAttribute('data-copying')) { button.style.opacity = '0' button.style.pointerEvents = 'none' } })

button.addEventListener('click', async () => { const codeText = codeElement.textContent || ''

try { // Primary method: use modern clipboard API await navigator.clipboard.writeText(codeText) button.setAttribute('data-copying', 'true') button.innerHTML = copiedIcon

setTimeout(() => { if (!preElement.matches(':hover')) { button.style.opacity = '0' button.style.pointerEvents = 'none' } button.removeAttribute('data-copying')

setTimeout(() => { button.innerHTML = copyIcon }, 500) }, 1500) } catch (err) { console.error('Failed to copy code:', err)

// Fallback method: create temporary textarea for older browsers try { const textArea = document.createElement('textarea') textArea.value = codeText textArea.style.position = 'fixed' textArea.style.opacity = '0' document.body.appendChild(textArea) textArea.focus() textArea.select()

navigator.clipboard .writeText(codeText) .then(() => { document.body.removeChild(textArea) }) .catch(() => { console.error('Both clipboard methods failed') document.body.removeChild(textArea) }) } catch (fallbackErr) { console.error('All clipboard methods failed:', fallbackErr) }

button.setAttribute('data-copying', 'true') button.innerHTML = copiedIcon

setTimeout(() => { if (!preElement.matches(':hover')) { button.style.opacity = '0' button.style.pointerEvents = 'none' } button.removeAttribute('data-copying')

setTimeout(() => { button.innerHTML = copyIcon }, 500) }, 1500) } }) }) }

document.addEventListener('astro:page-load', initCopyCode) document.addEventListener('DOMContentLoaded', initCopyCode) })(); function loadNeoDBCards() { const allContainers = document.querySelectorAll('.neodb-card-container')

allContainers.forEach((container) => { if (!container.hasAttribute('data-url')) { container.remove() } })

const containers = document.querySelectorAll('.neodb-card-container[data-url]')

if (containers.length === 0) { return }

const renderError = (elem) => { elem.innerHTML = `🔴 Error

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →