简体   繁体   中英

Search a text file with list of ad servers in javascript

I am making a program that blocks ads. I have found a list of ad servers from this page. My question: is it possible to search for these ad servers from that page in a website using javascript? and please note that I would like code for an extension

By default, a web page cannot access the content of another webpage in JavaScript. This is known as cross-origin HTTP request . The website can allow different websites to access its content using the HTTP header Access-Control-Allow-Origin , but your page, in particular, is not doing that.

If you go ahead and try you will get the error XMLHttpRequest cannot load https://pgl.yoyo.org/as/serverlist.php?hostformat=nohtml&showintro=1&startdate%5Bday%5D=&startdate%5Bmonth%5D=&startdate%5Byear%5D=. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. XMLHttpRequest cannot load https://pgl.yoyo.org/as/serverlist.php?hostformat=nohtml&showintro=1&startdate%5Bday%5D=&startdate%5Bmonth%5D=&startdate%5Byear%5D=. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

To work around this limitation you could develop a browser extension instead or use a proxy like crossorigin.me .

This is a working example:

 function loadAdServers(callback) { const url = 'https://crossorigin.me/https://pgl.yoyo.org/as/serverlist.php?hostformat=nohtml&showintro=1&startdate%5Bday%5D=&startdate%5Bmonth%5D=&startdate%5Byear%5D='; const request = new XMLHttpRequest(); request.open('GET', url); request.addEventListener('load', function () { callback(request.response.split('\\n').filter(x => x)); }); request.send(); } function removeElement(element) { if (element.parentElement) { element.parentElement.removeChild(element); } } function removeAdElements(adServers) { for (const img of document.querySelectorAll('img')) { for (const adServer of adServers) { if (img.src.indexOf(adServer) >= 0) { removeElement(img); } } } for (const a of document.querySelectorAll('a')) { for (const adServer of adServers) { if (a.href.indexOf(adServer) >= 0) { removeElement(a); } } } } loadAdServers(removeAdElements); 
  <img src="http://101order.com/company.logo"> <a href="http://101order.com/">Link</a> <a href="http://stackoverflow.com">stackoverflow</a> 

It takes some time to load the list and filter the elements.

you can use something like this:

var sites = ['site1', 'site2', '...'],
    sites_len = sites.length;
var els = document.getElementsByTagName('*');

for (var i = 0, len = els.length; i < len; i++) {
    var attr = "";
    switch (els[i].tagName.toLowerCase()) {
        case 'iframe':
        case 'script':
        case 'img':
            attr = 'src';
            break;
        case 'link':
        case 'a':
            attr = 'href';
            break
        default:
            continue;
    }
    var attr_val = els[i].getAttribute(attr);
    for (var j = 0; j < sites_len; j++)
        if (sites[j].indexOf(attr_val) > -1)
            els[i].parentNode.reamoveChild(els[i]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM