Teach you to write Baidu Search ads filter Chrome Plugin

Source: Internet
Author: User
1 Preface

At present, Baidu search list home page, ads 5 normal content is 10, and the ads are the first 1 to 5 positions, and the normal content of the display style. For those of us who are capable developers, we can actually implement a chrome plugin simply by executing some of our own JavaScript on the Baidu search page and deleting the ad entries.

2 Introduction to Plugins

The chrome plug-in logic body is composed of two parts, one is in the target document (such as Www.baidu.com page) running JS and CSS, called content_scripts , part of the content that is loaded and displayed on the Chrome shell (the Benchmarking and setting interface) is called browser_action, which needs to be described in a Manifest.json file.

2.1 Plug-in structure
manifest.jsonscripts  --jquery-1.7.1.min.js  --include.jsimages  --icon16.png  --icon32.png

Among them, scripts under the JS is content_scripts need, images under the picture is called browser_action need.

Ad features for 300-degree search 3.1 document structure characteristics
<div id="content_left">    <div>        <div>广告1</div>        <div>广告2</div>        <div>广告3</div>        <div>广告4</div>    </div>    <div>搜索记录1</div>    <div>搜索记录2</div>    <div>        <div>广告5</div>    </div></div>
    • Ads can be displayed in two places, or only in the first
    • Multiple ads may be wrapped in a div that is parallel to the search record, but sometimes the ad is directly parallel to the search record
3.2 Style Features
    • The class name of the ad entry is dynamic, and each refresh gets a different class name
    • The ad entry uses inline styles to ensure that the display properties of the ad are not affected by the style sheet
3.3 Self-protection features

Within two seconds of the completion of the document loading, the DOM of the ad entry is protected, attempts to modify the inline style of the ad, or directly delete the ad's DOM, will trigger a new ad to be generated and insert the search record in front of it.

4 Advertising filtering Ideas

Because Baidu search in anti-anti-advertising has done a lot of measures, based on the CSS level of hidden advertising ideas is not feasible, we can be based on jquery, monitoring the document element insert event, inside search advertising conditions, and then delete. Although Baidu has self-protection features, but after all, inserting new ads is also triggered to test document element insert event.

4.1 Include.js Code
; (function () {    $(document).bind("DOMNodeInserted", function (e) {        $("#content_left div[data-click] span:contains('广告')")            .parents("#content_left div[data-click]")            .remove();    });})();

$ ("#content_left Div[data-click] span:contains (' ad ')") can select an Ad ID and select it up to #content_left Div[data-click], which is the ad entry, Then remove it. After the deletion, Baidu will automatically insert new ads, new ads also conform to this selector, so will be two times to run our ad code logic.

4.2 Plugins and source code

The source code has been placed on GitHub, with a packaged baiduadblock.crx in it, dragged it to the Chrome kernel's browser, and checked "Allow access to sensitive pages" to complete the installation.

GitHub links

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.