Last active
October 21, 2016 20:52
-
-
Save jenyayel/7bdf35737291cc1cb4a8 to your computer and use it in GitHub Desktop.
Tests a list of domains whether they blocked by proxy or by ISP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This scripts tests a list of domains whether they blocked by proxy or by ISP | |
// the tests relies on image object loading and its native events. | |
// The domains are queued in FIFO and tested one by one until the non-blocked domain found. | |
// | |
// usage: | |
// tester.validateDomains(['//domain1.com', '//domain2.com'], '/favicon.ico') | |
// .done(function (domain) { | |
// // one of the domains is OK | |
// }) | |
// .fail(function () { | |
// // all domains are blocked | |
// }); | |
var tester = function ($) { | |
var HandlersQueue = function (domains, testPath) { | |
var _dfdQueue = new $.Deferred(); | |
// clone domains | |
var _domainsQueue = domains.slice(0) | |
// enqueue next domain check | |
function createDomainTestTask() { | |
// if there are no domains, stop queueing tasks | |
if (_domainsQueue.length == 0) | |
return; | |
// grab a domain name | |
var _nextDomain = _domainsQueue.shift(); | |
log('Queueing domain check task', { domain: _nextDomain }); | |
// deffers execution of domain check | |
// note: domainChecker returns separate promise | |
domainChecker(_nextDomain) | |
.done(function (okDomain) { | |
// found domain that not blocked - resolve main promise | |
_dfdQueue.resolve(okDomain); | |
}) | |
.fail(function () { | |
// domain test failed | |
if (_domainsQueue.length > 0) | |
// there are more domains that can be checked - enqueue next domain for check | |
createDomainTestTask(); | |
else { | |
log('No domains found that passed validation'); | |
_dfdQueue.reject(); | |
} | |
}); | |
} | |
function domainChecker(domain) { | |
var _dfd = new $.Deferred(); | |
var _image = new Image(); | |
log('Starting testing domain', { domain: domain }); | |
_image.onerror = function () { | |
log('Domain test failed', { domain: domain }); | |
_dfd.reject(domain); | |
}; | |
_image.onload = function () { | |
log('Domain test succeeded', { domain: domain }); | |
_dfd.resolve(domain); | |
}; | |
// TODO: decide whether the use random query string as cache baster | |
_image.src = domain + decodeURI(testPath); | |
return _dfd.promise(); | |
} | |
// starts execution and returns promise of the entire queue | |
this.start = function () { | |
createDomainTestTask(); | |
return _dfdQueue.promise(); | |
}; | |
}; | |
function log(message, params) { | |
if (console) | |
console.info(message, params); | |
} | |
return { | |
log: log, | |
validateDomains: function (domains, testPath) { | |
var _queue = new HandlersQueue(domains, testPath); | |
return _queue.start(); | |
} | |
}; | |
}(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment