diff --git a/components/GoogleAdsense.js b/components/GoogleAdsense.js index f102847b..12aacc95 100644 --- a/components/GoogleAdsense.js +++ b/components/GoogleAdsense.js @@ -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 且内容为 '' 的 div 元素 - const notionTextElements = document.querySelectorAll('div.notion-text') - + const notionTextElements = document.querySelectorAll( + '#article-wrapper #notion-article div.notion-text' + ) // 遍历找到的元素 notionTextElements?.forEach(element => { // 检查元素的内容是否为 '' - if (element.innerHTML.trim() === '<ins/>') { + if (element.textContent.trim() === '') { // 创建新的 元素 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 <>