Skip to content

Commit

Permalink
禁止复制文章功能,附带开关
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed Aug 2, 2023
1 parent 86fd5ad commit 5942d28
Show file tree
Hide file tree
Showing 16 changed files with 30 additions and 75 deletions.
3 changes: 1 addition & 2 deletions blog.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const BLOG = {
// 3.14.1版本后,欢迎语在此配置,英文逗号隔开 , 即可支持多个欢迎语打字效果。
GREETING_WORDS: process.env.NEXT_PUBLIC_GREETING_WORDS || 'Hi,我是一个程序员, Hi,我是一个打工人,Hi,我是一个干饭人,欢迎来到我的博客🎉',

CAN_COPY: process.env.NEXT_PUBLIC_CAN_COPY || true, // 是否允许复制页面内容 默认允许
CUSTOM_MENU: process.env.NEXT_PUBLIC_CUSTOM_MENU || false, // 支持Menu 类型,从3.12.0版本起,各主题将逐步支持灵活的二级菜单配置,替代了原来的Page类型,此配置是试验功能、默认关闭。

AUTHOR: process.env.NEXT_PUBLIC_AUTHOR || 'NotionNext', // 您的昵称 例如 tangly1024
Expand Down Expand Up @@ -84,7 +83,7 @@ const BLOG = {
FONT_AWESOME: process.env.NEXT_PUBLIC_FONT_AWESOME_PATH || 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css', // font-awesome 字体图标地址; 可选 /css/all.min.css , https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/font-awesome/6.0.0/css/all.min.css

// END ************网站字体*****************

CAN_COPY: process.env.NEXT_PUBLIC_CAN_COPY || true, // 是否允许复制页面内容 默认允许,如果设置为false、则全栈禁止复制内容。
CUSTOM_RIGHT_CLICK_CONTEXT_MENU: process.env.NEXT_PUBLIC_CUSTOM_RIGHT_CLICK_CONTEXT_MENU || true, // 自定义右键菜单,覆盖系统菜单

// 自定义外部脚本,外部样式
Expand Down
21 changes: 21 additions & 0 deletions components/DisableCopy.js
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions components/ExternalPlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const GoogleAdsense = dynamic(() => import('@/components/GoogleAdsense'), { ssr:
const Messenger = dynamic(() => import('@/components/FacebookMessenger'), { ssr: false })
const VConsole = dynamic(() => import('@/components/VConsole'), { ssr: false })
const CustomContextMenu = dynamic(() => import('@/components/CustomContextMenu'), { ssr: false })
const DisableCopy = dynamic(() => import('@/components/DisableCopy'), { ssr: false })

/**
* 各种第三方组件
Expand All @@ -55,6 +56,7 @@ const ExternalPlugin = (props) => {
{JSON.parse(BLOG.COMMENT_TWIKOO_COUNT_ENABLE) && <TwikooCommentCounter {...props}/>}
{JSON.parse(BLOG.RIBBON) && <Ribbon />}
{JSON.parse(BLOG.CUSTOM_RIGHT_CLICK_CONTEXT_MENU) && <CustomContextMenu {...props} />}
{!JSON.parse(BLOG.CAN_COPY) && <DisableCopy/>}
<VConsole/>
</>
}
Expand Down
7 changes: 0 additions & 7 deletions lib/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ export function GlobalContextProvider(props) {
initTheme()
}, [])

// 是否允许复制页面内容
useEffect(() => {
if (!BLOG.CAN_COPY) {
document.getElementsByTagName('html')[0].classList.add('forbid-copy')
}
}, [])

useEffect(() => {
const handleStart = (url) => {
NProgress.start()
Expand Down
6 changes: 6 additions & 0 deletions styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,9 @@ a.avatar-wrapper {
.reply-author-name {
font-weight: 500;
}

.forbid-copy {
user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
6 changes: 0 additions & 6 deletions themes/example/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ const Style = () => {
.dark body{
background-color: black;
}
// 文本不可选取
.forbid-copy {
user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
`}</style>
}
Expand Down
6 changes: 0 additions & 6 deletions themes/fukasawa/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ const Style = () => {
.dark body{
background-color: black;
}
// 文本不可选取
.forbid-copy {
user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
/* fukasawa的首页响应式分栏 */
#theme-fukasawa .grid-item {
Expand Down
6 changes: 0 additions & 6 deletions themes/gitbook/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ const Style = () => {
.dark body{
background-color: black;
}
// 文本不可选取
.forbid-copy {
user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
`}</style>
}
Expand Down
6 changes: 0 additions & 6 deletions themes/heo/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ const Style = () => {
background-color: #f7f9fe;
overflow-x: hidden;
}
// 文本不可选取
.forbid-copy {
user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
// 公告栏中的字体固定白色
#theme-heo #announcement-content .notion{
Expand Down
6 changes: 0 additions & 6 deletions themes/hexo/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ const Style = () => {
.dark body{
background-color: black;
}
// 文本不可选取
.forbid-copy {
user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
/* 菜单下划线动画 */
#theme-hexo .menu-link {
Expand Down
6 changes: 0 additions & 6 deletions themes/landing/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ const Style = () => {
.test {
text-color: red;
}
// 文本不可选取
.forbid-copy {
user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
`}</style>
}
Expand Down
6 changes: 0 additions & 6 deletions themes/matery/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ const Style = () => {
.dark body{
background-color: black;
}
// 文本不可选取
.forbid-copy {
user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
/* 设置了从上到下的渐变黑色 */
#theme-matery .header-cover::before {
Expand Down
6 changes: 0 additions & 6 deletions themes/medium/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ const Style = () => {
.dark body{
background-color: black;
}
// 文本不可选取
.forbid-copy {
user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
`}</style>
}
Expand Down
6 changes: 0 additions & 6 deletions themes/next/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ const Style = () => {
.dark body{
background-color: black;
}
// 文本不可选取
.forbid-copy {
user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
`}</style>
}
Expand Down
6 changes: 0 additions & 6 deletions themes/nobelium/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ const Style = () => {
.dark body{
background-color: black;
}
// 文本不可选取
.forbid-copy {
user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
`}</style>
}
Expand Down
6 changes: 0 additions & 6 deletions themes/plog/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ const Style = () => {
.dark body{
background-color: black;
}
// 文本不可选取
.forbid-copy {
user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
`}</style>
}

Expand Down

0 comments on commit 5942d28

Please sign in to comment.