mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-13 23:16:47 +00:00
Merge pull request #3092 from qixing-jk/feat-google-adsense
修复并优化 google adsense
This commit is contained in:
@@ -42,10 +42,7 @@ function getNodesWithAdsByGoogleClass(node) {
|
||||
|
||||
// 检查节点及其子节点是否包含 adsbygoogle 类
|
||||
function checkNodeForAds(node) {
|
||||
if (
|
||||
node.nodeType === Node.ELEMENT_NODE &&
|
||||
node.classList.contains('adsbygoogle')
|
||||
) {
|
||||
if (node.tagName === 'INS' && node.classList.contains('adsbygoogle')) {
|
||||
adsNodes.push(node)
|
||||
} else {
|
||||
// 递归检查子节点
|
||||
@@ -71,7 +68,7 @@ export const initGoogleAdsense = async ADSENSE_GOOGLE_ID => {
|
||||
).then(url => {
|
||||
setTimeout(() => {
|
||||
// 页面加载完成后加载一次广告
|
||||
const ads = document.getElementsByClassName('adsbygoogle')
|
||||
const ads = document.querySelectorAll('ins.adsbygoogle')
|
||||
if (window.adsbygoogle && ads.length > 0) {
|
||||
requestAd(Array.from(ads))
|
||||
}
|
||||
@@ -99,7 +96,11 @@ export const initGoogleAdsense = async ADSENSE_GOOGLE_ID => {
|
||||
}
|
||||
|
||||
// 启动 MutationObserver
|
||||
observer.observe(document.body, observerConfig)
|
||||
observer.observe(
|
||||
document.querySelector('#article-wrapper #notion-article') ||
|
||||
document.body,
|
||||
observerConfig
|
||||
)
|
||||
}, 100)
|
||||
})
|
||||
}
|
||||
@@ -182,12 +183,13 @@ const AdEmbed = () => {
|
||||
useEffect(() => {
|
||||
setTimeout(() => {
|
||||
// 找到所有 class 为 notion-text 且内容为 '<ins/>' 的 div 元素
|
||||
const notionTextElements = document.querySelectorAll('div.notion-text')
|
||||
|
||||
const notionTextElements = document.querySelectorAll(
|
||||
'#article-wrapper #notion-article div.notion-text'
|
||||
)
|
||||
// 遍历找到的元素
|
||||
notionTextElements?.forEach(element => {
|
||||
// 检查元素的内容是否为 '<ins/>'
|
||||
if (element.innerHTML.trim() === '<ins/>') {
|
||||
if (element.textContent.trim() === '<ins/>') {
|
||||
// 创建新的 <ins> 元素
|
||||
const newInsElement = document.createElement('ins')
|
||||
newInsElement.className = 'adsbygoogle w-full py-1'
|
||||
@@ -205,8 +207,6 @@ const AdEmbed = () => {
|
||||
element?.parentNode?.replaceChild(newInsElement, element)
|
||||
}
|
||||
})
|
||||
|
||||
requestAd()
|
||||
}, 1000)
|
||||
}, [])
|
||||
return <></>
|
||||
|
||||
Reference in New Issue
Block a user