简体   繁体   English

在开发模式下,在本地主机上阻止Google广告

[英]Block Google ads on localhost while in development mode

I frequently have to update and test my small websites on my local machine. 我经常不得不在本地计算机上更新和测试小型网站。 The websites use google ads. 该网站使用谷歌广告。

I used to manually disable and enable the ads on each and every page and this is very painful. 我曾经手动禁用和启用每个页面上的广告,这非常痛苦。

I want to disable all the google ads while I am testing the website on my local machine. 我想在本地计算机上测试网站时禁用所有的google广告。 How do I achieve this? 我该如何实现?

After a few struggles, I found a basic solution to block all ads on localhost. 经过几番努力,我找到了一种基本的解决方案来阻止localhost上的所有广告。 The only limitation is that this has to be placed in master file and all the files inheriting the master file will have ads blocked. 唯一的限制是必须将其放置在master文件中,并且继承该主文件的所有文件都将被屏蔽广告。

Paste the below script in your master file in head section before including any other scripts. 在包括任何其他脚本之前,将以下脚本粘贴到您的master文件的head部分中。

    <script type="text/javascript">
        //block google ads on localhost
        if (window.location.hostname === "localhost") {
            window.adsbygoogle = {
                enabled: true,
                loaded: true,
                push: function () {
                    return;
                },
                google_ad_client: "",
                enable_page_level_ads: true
            }
        }
    </script>

您可以使用adblocker在本地主机上启用它,然后在发布站点时禁用它。

There are two ways, you can develop your own Chrome extension that runs only on your chrome to hide Google Ads (which by the way have unique set of HTML so it is easy to pin point them and remove them). 有两种方法,您可以开发自己的Chrome扩展程序,该扩展程序仅在您的Chrome上运行以隐藏Google Ads(顺便说一下,它们具有独特的HTML集,因此很容易将它们定位和删除)。

Or you can make a link to a javascript file in the main template, that is always empty on the server (production), but locally it defines the css for Google Ads to be hidden. 或者,您可以链接到主模板中的javascript文件,该文件在服务器(生产)上始终为空,但在本地它定义了隐藏Google Ads的css。 Again, Google Ads selectors are not so hard to pin point. 同样,Google Ads选择器也不难确定。

https://developer.chrome.com/extensions/getstarted https://developer.chrome.com/extensions/getstarted

Here is a plnkr code for that purpose 这是为此目的的plnkr代码

manifest.json manifest.json

http://plnkr.co/edit/wPxuO0vndIBMTz1ajMyJ?p=preview
{
  "name": "Walnut",
  "description": "hide ads",
  "version": "2.0",

 "content_scripts": [
    {
      "matches": ["https://*.facebook.com/*","http://localhost:8080/*"],
        "css": [ "remove.css" ],
        "js": ["remove.js"],
      "run_at": "document_start"
    }
  ],
  "manifest_version": 2
}

remove.js remove.js

// you can also make the removal periodical in case the Ad provider keeps recreating itself
// here you can have more freedon to find elements that main contain certain part of a class name or id
document.addEventListener('DOMContentLoaded', function () {

    var all = document.getElementsByTagName("*");

    for (var i=0, max=all.length; i < max; i++) {
      // check id and hide if it matches google-ad-*
    }
    setTimeout(function () {
        var ads = document.getElementsByClassName("example-suspected-class");


        if (ads.length == 1) {

            ads[0].remove();
        }
    }, 100);

    setInterval(function () {
        var ads = document.getElementsByClassName("example-suspected-class");

        if (ads.length == 1) {

            ads[0].remove();
        }
    },10000);


});

remove.css remove.css

/* setup any css you susspect would mean something to web page, examples */
#slot_TL1 {display: none!important;}
.vp-off .mb-list-ad {display: none!important;}
.vp-off .mb-list-ad * {display: none!important;}

http://plnkr.co/edit/wPxuO0vndIBMTz1ajMyJ?p=preview http://plnkr.co/edit/wPxuO0vndIBMTz1ajMyJ?p=preview

怎么样在配置文件中有一个标志,在本地进行开发/测试时可以在其中设置一个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何阻止Google广告注入JavaScript - How to block google ads from injecting JavaScript 在本地主机上进行开发时,firebase 分析是否有效? - Does firebase analytics work while development on localhost? 打印页面时隐藏 Google Ads - Hide Google Ads while printing the page 如何在不阻塞主线程的情况下运行代码(Google/Doubleclick Ads) - How to run code (Google/Doubleclick Ads) without block main thread Google Maps API和KML文件LocalHost开发选项 - Google Maps API and KML File LocalHost Development Options WKWebView在加载任何URL时阻止某些脚本(块跟踪或任何网站中的广告) - WKWebView block certain scripts while loading any URL (Block tracking or Ads in any website) 在Mozilla for Ads中阻止Javascript - Block Javascripts in Mozilla for Ads 在开发模式下未从 nextjs 中的 URL 获取图像 - not fetching an image from URL in nextjs while on development mode 是否可以在开发模式下渲染存储在 Laravel 存储中的图像? - Is it possible to render an image stored in laravel storage while in development mode? Node.js护照Google策略的开发模式? - Development mode for node.js passport google strategy?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM