From 98b41407e073cae9c868e30ac564518dd61010b1 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sat, 6 Oct 2018 23:32:43 +0800 Subject: [PATCH] refactor(checker): use image way --- src/disqus.js | 66 ++++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/src/disqus.js b/src/disqus.js index 534e51a..ca1206a 100644 --- a/src/disqus.js +++ b/src/disqus.js @@ -8,13 +8,15 @@ /* * 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.shortname - The disqus shortname * disqusjs.config.identifier - The identifier of the page * disqusjs.config.url - The url of the page * disqusjs.config,api - Where to get data * 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.page.id = The thread id, used at next API call @@ -24,6 +26,7 @@ */ disqusjs.page = {}; +disqusjs.mode = 'proxy'; var xhr = new XMLHttpRequest(); /* @@ -47,36 +50,39 @@ function loadDisqus() { Then check [shortname].disqus.com/favicon.ico is avaliable in 3000 or not. */ function checkDisqus() { - xhr.open('GET', 'https://disqus.com/next/config.json', true); - xhr.timeout = 2000; - xhr.send(); - xhr.onload = function () { - if (this.status == 200 || this.status == 304) { - var img = new Image; - var checker = setTimeout(function () { - img.onerror = img.onload = null; - 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); + var domain = ['disqus.com', disqusjs.config.shortname + '.disqus.com'], + test = 0, + success = 0; + var setmode = function (d) { + if (success = test) { + disqusjs.mode = 'direct'; + } else { + disqusjs.mode = 'proxy'; } }; - xhr.ontimeout = function (e) { - disqusjs.config.mode = 'proxy'; - console.log(disqusjs) + var check = function (domain) { + var img = new Image; + 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'; - console.log(disqusjs) + + for (var i of domain) { + check(i) }; } @@ -137,7 +143,3 @@ function getComment() { console.log(e) }; } - -checkDisqus(); - -/* getThreadInfo(); */