禁止复制文章功能,附带开关

This commit is contained in:
tangly1024.com
2023-08-02 16:37:40 +08:00
parent 86fd5adf59
commit 5942d28cca
16 changed files with 30 additions and 75 deletions

21
components/DisableCopy.js Normal file
View File

@@ -0,0 +1,21 @@
import BLOG from '@/blog.config'
import { useEffect } from 'react'
/**
* 禁止用户拷贝文章的插件
*/
export default function DisableCopy() {
useEffect(() => {
if (!JSON.parse(BLOG.CAN_COPY)) {
// 全栈添加禁止复制的样式
document.getElementsByTagName('html')[0].classList.add('forbid-copy')
// 监听复制事件
document.addEventListener('copy', function (event) {
event.preventDefault() // 阻止默认复制行为
alert('抱歉,本网页内容不可复制!')
})
}
}, [])
return null
}