我已经写了一些Javascript来将广告注入文章中。根据显示的字符数或段数,将广告注入文章的某个点。
我遇到的问题是,当页面上有无序列表时,jaavscript广告会将自己注入到列表项中。然后,这会稍微抛弃格式,因为它们与列表项的其余部分一起缩进。
是否有一种Javascript方法可以确保这些广告不会被注入到列表中并且只会被注入段落标记?
欢呼声
下面是代码示例。以下设置vtext广告
setupVText() {
const longArticle = this.articleLength > shortestArticle;
const noVideos = !this.elements.videos.length;
const noAdInitiated = !this.vTextHasInit;
// only init if article is long enough and
// the article does not have any videos
// the v text add has not already been initiated
if ( longArticle && noVideos && noAdInitiated ) {
this.vTextOptions = {
title: 'ADVERTISEMENT',
vptags: this.setTags(),
vpcategory: this.setCategory(),
divid: 'vtext-container',
identifier: 'uk-dev',
injectAfter: this.injectAfterParagraph(this.paragraphLengths)
};
window.addEventListener('resize', event => {
// debounce resize event
if (typeof this.resizeTimeout !== 'undefined') clearTimeout(this.resizeTimeout);
this.resizeTimeout = setTimeout(this.resize.bind(this), 300);
});
this.vTextHasInit = true;
this.injectContainer();
this.setContainerDimensions();
this.serveAd();
}
}
以下代码根据字符数或段落长度
将其注入页面injectAfterParagraph(paragraphLengths) {
let characterCount = 0;
for (const paragraph in paragraphLengths) {
// loop through paragraphs and add length to characterCount
characterCount += paragraphLengths[paragraph];
// if count goes above character limit return paragraph number
if (characterCount >= injectAfterChar) {
// return paragraph or 1 if paragraph is 0
window.paragraph = paragraph;
return parseInt(paragraph) || 1;
}
}
}