From 5942d28cca830f5bc48d8f9d30ee073040fa8cb7 Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Wed, 2 Aug 2023 16:37:40 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A6=81=E6=AD=A2=E5=A4=8D=E5=88=B6=E6=96=87?= =?UTF-8?q?=E7=AB=A0=E5=8A=9F=E8=83=BD=EF=BC=8C=E9=99=84=E5=B8=A6=E5=BC=80?= =?UTF-8?q?=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blog.config.js | 3 +-- components/DisableCopy.js | 21 +++++++++++++++++++++ components/ExternalPlugins.js | 2 ++ lib/global.js | 7 ------- styles/globals.css | 6 ++++++ themes/example/style.js | 6 ------ themes/fukasawa/style.js | 6 ------ themes/gitbook/style.js | 6 ------ themes/heo/style.js | 6 ------ themes/hexo/style.js | 6 ------ themes/landing/style.js | 6 ------ themes/matery/style.js | 6 ------ themes/medium/style.js | 6 ------ themes/next/style.js | 6 ------ themes/nobelium/style.js | 6 ------ themes/plog/style.js | 6 ------ 16 files changed, 30 insertions(+), 75 deletions(-) create mode 100644 components/DisableCopy.js diff --git a/blog.config.js b/blog.config.js index 489c87ff..e781e780 100644 --- a/blog.config.js +++ b/blog.config.js @@ -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 @@ -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, // 自定义右键菜单,覆盖系统菜单 // 自定义外部脚本,外部样式 diff --git a/components/DisableCopy.js b/components/DisableCopy.js new file mode 100644 index 00000000..80860714 --- /dev/null +++ b/components/DisableCopy.js @@ -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 +} diff --git a/components/ExternalPlugins.js b/components/ExternalPlugins.js index c4b62548..b239f954 100644 --- a/components/ExternalPlugins.js +++ b/components/ExternalPlugins.js @@ -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 }) /** * 各种第三方组件 @@ -55,6 +56,7 @@ const ExternalPlugin = (props) => { {JSON.parse(BLOG.COMMENT_TWIKOO_COUNT_ENABLE) && } {JSON.parse(BLOG.RIBBON) && } {JSON.parse(BLOG.CUSTOM_RIGHT_CLICK_CONTEXT_MENU) && } + {!JSON.parse(BLOG.CAN_COPY) && } } diff --git a/lib/global.js b/lib/global.js index 9e6819f8..04d0176e 100644 --- a/lib/global.js +++ b/lib/global.js @@ -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() diff --git a/styles/globals.css b/styles/globals.css index dbaa8ef2..5151e216 100644 --- a/styles/globals.css +++ b/styles/globals.css @@ -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; +} \ No newline at end of file diff --git a/themes/example/style.js b/themes/example/style.js index b5c3e553..0708b7b5 100644 --- a/themes/example/style.js +++ b/themes/example/style.js @@ -10,12 +10,6 @@ const Style = () => { .dark body{ background-color: black; } - // 文本不可选取 - .forbid-copy { - user-select: none; - -webkit-user-select: none; - -ms-user-select: none; - } `} } diff --git a/themes/fukasawa/style.js b/themes/fukasawa/style.js index bc024d7a..e730d919 100644 --- a/themes/fukasawa/style.js +++ b/themes/fukasawa/style.js @@ -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 { diff --git a/themes/gitbook/style.js b/themes/gitbook/style.js index 47e60906..5e8eaa5a 100644 --- a/themes/gitbook/style.js +++ b/themes/gitbook/style.js @@ -11,12 +11,6 @@ const Style = () => { .dark body{ background-color: black; } - // 文本不可选取 - .forbid-copy { - user-select: none; - -webkit-user-select: none; - -ms-user-select: none; - } `} } diff --git a/themes/heo/style.js b/themes/heo/style.js index f7e96a21..b0de7c2d 100644 --- a/themes/heo/style.js +++ b/themes/heo/style.js @@ -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{ diff --git a/themes/hexo/style.js b/themes/hexo/style.js index 8e789ca1..33d2878a 100644 --- a/themes/hexo/style.js +++ b/themes/hexo/style.js @@ -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 { diff --git a/themes/landing/style.js b/themes/landing/style.js index 6f4aeb36..7a59e053 100644 --- a/themes/landing/style.js +++ b/themes/landing/style.js @@ -10,12 +10,6 @@ const Style = () => { .test { text-color: red; } - // 文本不可选取 - .forbid-copy { - user-select: none; - -webkit-user-select: none; - -ms-user-select: none; - } `} } diff --git a/themes/matery/style.js b/themes/matery/style.js index 6263f78c..711670c9 100644 --- a/themes/matery/style.js +++ b/themes/matery/style.js @@ -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 { diff --git a/themes/medium/style.js b/themes/medium/style.js index 47e60906..5e8eaa5a 100644 --- a/themes/medium/style.js +++ b/themes/medium/style.js @@ -11,12 +11,6 @@ const Style = () => { .dark body{ background-color: black; } - // 文本不可选取 - .forbid-copy { - user-select: none; - -webkit-user-select: none; - -ms-user-select: none; - } `} } diff --git a/themes/next/style.js b/themes/next/style.js index 86ca0fc1..d01b13b3 100644 --- a/themes/next/style.js +++ b/themes/next/style.js @@ -14,12 +14,6 @@ const Style = () => { .dark body{ background-color: black; } - // 文本不可选取 - .forbid-copy { - user-select: none; - -webkit-user-select: none; - -ms-user-select: none; - } `} } diff --git a/themes/nobelium/style.js b/themes/nobelium/style.js index 47e60906..5e8eaa5a 100644 --- a/themes/nobelium/style.js +++ b/themes/nobelium/style.js @@ -11,12 +11,6 @@ const Style = () => { .dark body{ background-color: black; } - // 文本不可选取 - .forbid-copy { - user-select: none; - -webkit-user-select: none; - -ms-user-select: none; - } `} } diff --git a/themes/plog/style.js b/themes/plog/style.js index d1db3053..23643fa9 100644 --- a/themes/plog/style.js +++ b/themes/plog/style.js @@ -10,12 +10,6 @@ const Style = () => { .dark body{ background-color: black; } - // 文本不可选取 - .forbid-copy { - user-select: none; - -webkit-user-select: none; - -ms-user-select: none; - } `} }