Skip to content

Commit

Permalink
OPEN_WRITE导流插件
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed Sep 5, 2024
1 parent d0a4acd commit c2dc5ba
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ yarn-error.log*
/public/robots.txt
/public/sitemap.xml
/public/rss/*

/sitemap.xml

# yarn
package-lock.json
Expand Down
60 changes: 60 additions & 0 deletions components/OpenWrite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { siteConfig } from '@/lib/config'
import { isBrowser, loadExternalResource } from '@/lib/utils'
import { useRouter } from 'next/router'
import { useEffect, useRef } from 'react'
/**
* OpenWrite公众号导流插件
* 使用介绍:https://openwrite.cn/guide/readmore/readmore.html#%E4%BA%8C%E3%80%81%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8
* 登录后台配置你的博客:https://readmore.openwrite.cn/
* @returns
*/
const OpenWrite = () => {
const router = useRouter()
const qrcode = siteConfig('OPEN_WRITE_QRCODE', '请配置公众号二维码')
const blogId = siteConfig('OPEN_WRITE_BLOG_ID')
const name = siteConfig('OPEN_WRITE_NAME', '请配置公众号名')
const id = 'article-wrapper'
const keyword = siteConfig('OPEN_WRITE_KEYWORD', '请配置公众号关键词')
const btnText = siteConfig(
'OPEN_WRITE_BTN_TEXT',
'原创不易,完成人机检测,阅读全文'
)

const hasLoaded = useRef(false)

const loadOpenWrite = async () => {
try {
await loadExternalResource(
'https://readmore.openwrite.cn/js/readmore-2.0.js',
'js'
)
const BTWPlugin = window?.BTWPlugin

if (BTWPlugin) {
const btw = new BTWPlugin()
window.btw = btw
btw.init({
qrcode,
id,
name,
btnText,
keyword,
blogId
})
}
} catch (error) {
console.error('OpenWrite 加载异常', error)
}
}

useEffect(() => {
if (isBrowser && blogId && !hasLoaded.current) {
loadOpenWrite()
hasLoaded.current = true
}
}, [router])

return <></>
}

export default OpenWrite
2 changes: 1 addition & 1 deletion lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export function getLastPartOfUrl(url) {
* @param type js 或 css
* @returns {Promise<unknown>}
*/
export function loadExternalResource(url, type) {
export function loadExternalResource(url, type = 'js') {
// 检查是否已存在
const elements =
type === 'js'
Expand Down
7 changes: 6 additions & 1 deletion pages/[prefix]/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import BLOG from '@/blog.config'
import useNotification from '@/components/Notification'
import OpenWrite from '@/components/OpenWrite'
import { siteConfig } from '@/lib/config'
import { getGlobalData, getPost, getPostBlocks } from '@/lib/db/getSiteData'
import { useGlobal } from '@/lib/global'
Expand Down Expand Up @@ -95,8 +96,12 @@ const Slug = props => {
})
return (
<>
{/* 文章布局 */}
<Layout {...props} />
{/* 解锁密码提示框 */}
{post?.password && post?.password !== '' && !lock && <Notification />}
{/* 导流工具 */}
<OpenWrite />
</>
)
}
Expand Down Expand Up @@ -129,7 +134,7 @@ export async function getStaticProps({ params: { prefix }, locale }) {
fullSlug += '.html'
}
}

// 在列表内查找文章
props.post = props?.allPages?.find(p => {
return (
Expand Down
4 changes: 4 additions & 0 deletions styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,7 @@ a.avatar-wrapper {
margin-right: 20px;
}
}

img {
display: unset;
}

0 comments on commit c2dc5ba

Please sign in to comment.