refactor(checker): use image way

0.2.5(deprecated)
SukkaW 7 years ago
parent e23e319cf0
commit 98b41407e0

@ -8,13 +8,15 @@
/* /*
* The variable used in DisqusJS * The variable used in DisqusJS
* *
* DisqusJS Mode
* disqusjs.mode = proxy | direct - Set which mode to use, should store and get in localStorage
*
* DisqusJS Config * DisqusJS Config
* disqusjs.config.shortname - The disqus shortname * disqusjs.config.shortname - The disqus shortname
* disqusjs.config.identifier - The identifier of the page * disqusjs.config.identifier - The identifier of the page
* disqusjs.config.url - The url of the page * disqusjs.config.url - The url of the page
* disqusjs.config,api - Where to get data * disqusjs.config,api - Where to get data
* disqusjs.config.apikey - The apikey used to request Disqus API * disqusjs.config.apikey - The apikey used to request Disqus API
* disqusjs.config.mode - proxy | direct - Set which mode to use, should store and get in localStorage
* *
* DisqusJS Info * DisqusJS Info
* disqusjs.page.id = The thread id, used at next API call * disqusjs.page.id = The thread id, used at next API call
@ -24,6 +26,7 @@
*/ */
disqusjs.page = {}; disqusjs.page = {};
disqusjs.mode = 'proxy';
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
/* /*
@ -47,36 +50,39 @@ function loadDisqus() {
Then check [shortname].disqus.com/favicon.ico is avaliable in 3000 or not. Then check [shortname].disqus.com/favicon.ico is avaliable in 3000 or not.
*/ */
function checkDisqus() { function checkDisqus() {
xhr.open('GET', 'https://disqus.com/next/config.json', true); var domain = ['disqus.com', disqusjs.config.shortname + '.disqus.com'],
xhr.timeout = 2000; test = 0,
xhr.send(); success = 0;
xhr.onload = function () { var setmode = function (d) {
if (this.status == 200 || this.status == 304) { if (success = test) {
var img = new Image; disqusjs.mode = 'direct';
var checker = setTimeout(function () { } else {
img.onerror = img.onload = null; disqusjs.mode = 'proxy';
disqusjs.config.mode = 'proxy';
}, 2500);
img.onerror = function () {
clearTimeout(checker);
disqusjs.config.mode = 'proxy';
console.log(disqusjs)
};
img.onload = function () {
clearTimeout(checker);
disqusjs.config.mode = 'direct';
console.log(disqusjs)
};
img.src = "https://" + disqusjs.config.shortname + ".disqus.com/favicon.ico?" + +(new Date);
} }
}; };
xhr.ontimeout = function (e) { var check = function (domain) {
disqusjs.config.mode = 'proxy'; var img = new Image;
console.log(disqusjs) var checker = setTimeout(function () {
img.onerror = img.onload = null,
test++,
setmode();
}, 2500);
img.onerror = function () {
clearTimeout(checker),
test++,
setmode();
};
img.onload = function () {
clearTimeout(checker),
success++,
test++,
setmode();
};
img.src = 'https://' + domain + '/favicon.ico?' + +(new Date);
}; };
xhr.onerror = function (e) {
disqusjs.config.mode = 'proxy'; for (var i of domain) {
console.log(disqusjs) check(i)
}; };
} }
@ -137,7 +143,3 @@ function getComment() {
console.log(e) console.log(e)
}; };
} }
checkDisqus();
/* getThreadInfo(); */

Loading…
Cancel
Save