
替换网页中的google广告内容,首先需要准确识别这些广告元素。google广告通常通过两种主要方式集成:google ad manager (gpt) 和 google adsense。识别它们的方法有所不同。
对于使用Google Publisher Tag (GPT) 库投放的广告,googletag 对象在页面全局可用,它提供了访问广告位信息的方法。
// 遍历所有已定义的广告位,并替换其内容
for (let i = 0; i < googletag.pubads().getSlots().length; i++) {
const slotDomId = googletag.pubads().getSlots()[i].getSlotElementId();
document.getElementById(slotDomId).innerHTML = '<h1>您的自定义HTML内容</h1>';
}说明:
注意事项: 此方法仅适用于使用了Google Publisher Tag (GPT) 的网站。如果网站未使用GPT,googletag 对象将不存在,导致脚本错误。
对于通过Google AdSense 代码嵌入的广告,通常可以通过其特有的CSS类名来识别。AdSense广告单元通常包含在 ins 标签中,并带有 adsbygoogle 类。
// 查找所有带有 'adsbygoogle' 类的 <ins> 元素
const adsenseBlocks = document.querySelectorAll('ins.adsbygoogle');
for (let i = 0; i < adsenseBlocks.length; i++) {
// AdSense广告通常会将其父元素作为广告容器,或者直接修改ins元素
// 这里假设需要替换的是其直接父元素的内容,或直接ins元素的内容
const adContainer = adsenseBlocks[i].parentNode; // 或者直接 adsenseBlocks[i]
if (adContainer && adContainer.id) { // 确保父元素有ID以便选择
document.querySelector('#' + adContainer.id).innerHTML = '<h1>您的自定义HTML内容</h1>';
} else {
// 如果父元素没有ID,直接替换 <ins> 元素本身的内容
adsenseBlocks[i].innerHTML = '<h1>您的自定义HTML内容</h1>';
}
}说明:
由于 googletag 对象是页面级别的全局变量,直接在Chrome扩展程序的 content-script 中访问它会导致 undefined 错误。这是因为 content-script 运行在隔离的环境中,无法直接访问页面脚本创建的变量。为了解决这个问题,我们需要将替换逻辑作为普通的 <script> 标签注入到页面的DOM中。
首先,配置您的 manifest.json 文件,声明必要的权限和可访问的资源。
{
"name": "自定义广告替换扩展",
"action": {},
"manifest_version": 3,
"version": "0.1",
"description": "替换网页中的Google广告为自定义内容。",
"permissions": [
"activeTab",
"webNavigation",
"scripting"
],
"host_permissions": [
"<all_urls>"
],
"background": {
"service_worker": "service-worker.js"
},
"web_accessible_resources": [{
"resources": ["pageScript.js"],
"matches": ["<all_urls>"]
}]
}权限解释:
背景脚本负责监听页面加载完成事件,并执行注入逻辑。
chrome.webNavigation.onCompleted.addListener((details) => {
// 确保只在主框架加载完成时执行
if (details.frameId === 0) {
chrome.scripting.executeScript({
target: { tabId: details.tabId },
func: injectPageScript // 使用 func 属性直接传入函数
});
}
});
// 这个函数将在目标页面的上下文中执行
function injectPageScript() {
const s = document.createElement('script');
s.src = chrome.runtime.getURL('pageScript.js');
s.onload = function() {
// 脚本加载并执行后,可以移除这个临时的script标签
this.remove();
};
// 将脚本添加到页面的 <head> 或 <html> 元素中
(document.head || document.documentElement).appendChild(s);
}说明:
这个脚本包含实际的广告替换逻辑。为了处理动态加载的广告(例如懒加载或刷新后出现的广告),我们还需要监听GPT的 slotOnload 事件。
function replaceAdContent() {
// 1. 替换Google Ad Manager (GPT) 广告
if (typeof googletag !== 'undefined' && googletag.pubads) {
for (let i = 0; i < googletag.pubads().getSlots().length; i++) {
const slotDomId = googletag.pubads().getSlots()[i].getSlotElementId();
const adElement = document.getElementById(slotDomId);
if (adElement) {
adElement.innerHTML = '<h1>您的自定义HTML内容 (GPT)</h1>';
}
}
}
// 2. 替换Google AdSense 广告
const adsenseBlocks = document.querySelectorAll('ins.adsbygoogle');
adsenseBlocks.forEach(adBlock => {
// 可以选择替换 <ins> 元素本身,或者其父元素
// 这里以替换 <ins> 元素本身为例
adBlock.innerHTML = '<h1>您的自定义HTML内容 (AdSense)</h1>';
});
}
// 首次页面加载时执行替换
replaceAdContent();
// 监听GPT广告加载事件,以处理动态或懒加载的广告
// 确保 googletag 存在才添加监听器
if (typeof googletag !== 'undefined' && googletag.pubads) {
googletag.pubads().addEventListener("slotOnload", (event) => {
// 当一个广告位加载完成时,重新执行替换逻辑
replaceAdContent();
});
// 还可以监听其他事件,例如 'slotRenderEnded'
// googletag.pubads().addEventListener("slotRenderEnded", (event) => {
// replaceAdContent();
// });
}说明:
通过上述步骤,您可以构建一个Chrome扩展程序,有效地检测并替换网页中的Google广告为自定义内容。
关键点回顾:
注意事项:
以上就是如何通过Chrome扩展程序替换Google广告内容的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号