feat/refactor: disquscheck & other function

0.2.5(deprecated)
SukkaW 7 years ago
parent 0b1ebbc02c
commit d73d055a94

@ -230,7 +230,7 @@
* The variable used in DisqusJS * The variable used in DisqusJS
* *
* DisqusJS Mode * DisqusJS Mode
* disqusjs.mode = proxy | direct - Set which mode to use, should store and get in localStorage * disqusjs.mode = dsqjs | disqus - 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
@ -248,7 +248,12 @@
*/ */
disqusjs.page = []; disqusjs.page = [];
disqusjs.mode = 'proxy';
window.disqus_config = function () {
this.page.url = disqusjs.config.url;
this.page.identifier = disqusjs.config.identifier;
};
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
setLS = (key, value) => { setLS = (key, value) => {
@ -295,31 +300,15 @@ Date.prototype.Format = function (fmt) {
return fmt; return fmt;
} }
/*
* Name: getMode()
* Description: get mode from localstorage
*/
function getMode() {
let s = getLS('disqusjs_mode');
if (!s) {
// Run checkDisqus() when no localStorage item
// disqusjs.mode will be set in checkDisqus()
checkDisqus();
} else {
disqusjs.mode = s;
}
}
/* /*
* Name: loadDisqus() * Name: loadDisqus()
* Descriptin: load disqus as it should be. * Descriptin: load disqus as it should be.
*/ */
function loadDisqus() { loadDisqus = () => {
var d = document; var d = document;
var s = d.createElement('script'); var s = d.createElement('script');
s.src = '//' + disqusjs.config.shortname + '.disqus.com/embed.js'; s.src = 'https://' + disqusjs.config.shortname + '.disqus.com/embed.js';
s.setAttribute('data-timestamp', + new Date()); s.setAttribute('data-timestamp', + new Date());
(d.head || d.body).appendChild(s); (d.head || d.body).appendChild(s);
} }
@ -328,43 +317,59 @@ function loadDisqus() {
* Name: checkDisqus() * Name: checkDisqus()
* Description: Check disqus is avaliable for visitor or not * Description: Check disqus is avaliable for visitor or not
* How it works: check favicons under 2 domains can be loaded or not. * How it works: check favicons under 2 domains can be loaded or not.
*/ */
function checkDisqus() {
checkDisqus = () => {
let domain = ['disqus.com', disqusjs.config.shortname + '.disqus.com'], let domain = ['disqus.com', disqusjs.config.shortname + '.disqus.com'],
test = 0, test = 0,
success = 0; success = 0;
setmode = () => { setmode = () => {
if (success = test) { if (success = test) {
disqusjs.mode = 'direct', setLS('disqusjs_mode', 'disqus');
setLS('disqusjs_mode', 'direct');
} else { } else {
disqusjs.mode = 'proxy', setLS('disqusjs_mode', 'dsqjs');
setLS('disqusjs_mode', 'proxy');
} }
}; };
check = (domain) => {
var img = new Image; var img = new Image;
var checker = setTimeout(() => { let check1 = setTimeout(() => {
img.onerror = img.onload = null, img.onerror = img.onload = null;
test++ , test = test + 1;
console.log(img.src + ' timeout')
setmode(); setmode();
}, 3000); }, 2000);
img.onerror = () => { img.onerror = () => {
clearTimeout(checker), clearTimeout(check1);
test++ , setLS('disqusjs_mode', 'dsqjs');
setmode(); main();
}; };
img.onload = () => { img.onload = () => {
clearTimeout(checker), clearTimeout(check1);
success++ , let check2 = setTimeout(() => {
test++ , img.onerror = img.onload = null;
test = test + 1;
console.log(img.src + ' timeout')
setmode(); setmode();
}, 2000);
img.onerror = () => {
clearTimeout(check2);
setLS('disqusjs_mode', 'dsqjs');
main();
}; };
img.src = 'https://' + domain + '/favicon.ico?' + +(new Date);
img.onload = () => {
clearTimeout(check2);
setLS('disqusjs_mode', 'disqus');
main();
}; };
for (let i of domain) {
check(i); img.src = 'https://' + disqusjs.config.shortname + '.disqus.com/favicon.ico?' + +(new Date);
}; };
img.src = 'https://disqus.com/favicon.ico?' + +(new Date);
} }
/* /*
@ -374,28 +379,21 @@ function checkDisqus() {
* API URI: /3.0/threads/list.json?forum=[disqus_shortname]&thread=ident:[identifier]&api_key=[apikey] * API URI: /3.0/threads/list.json?forum=[disqus_shortname]&thread=ident:[identifier]&api_key=[apikey]
*/ */
function getThreadInfo() { getThreadInfo = () => {
let url = disqusjs.config.api + '3.0/threads/list.json?forum=' + disqusjs.config.shortname + '&thread=ident:' + disqusjs.config.identifier + '&api_key=' + disqusjs.config.apikey;
/*
* Name: getComment()
* Description: get the comment content
* API URI: /3.0/posts/list.json?forum=[shortname]&thread=[thread id]&api_key=[apikey]
*/
getComment = () => {
let url = disqusjs.config.api + '3.0/posts/list.json?forum=' + disqusjs.config.shortname + '&thread=' + disqusjs.page.id + '&api_key=' + disqusjs.config.apikey;
xhr.open('GET', url, true); xhr.open('GET', url, true);
xhr.timeout = 4000; xhr.timeout = 4000;
xhr.send(); xhr.send();
xhr.onload = function () { xhr.onload = function () {
if (this.status == 200 || this.status == 304) { if (this.status == 200 || this.status == 304) {
var res = JSON.parse(this.responseText); var response = JSON.parse(this.responseText).response[0];
if (res.code === 0) { disqusjs.page = {
getCommentList(res.response); id: response.id,
} else { title: response.title,
// Have error when get comments. isClosed: response.isClosed,
} length: response.posts
};
getComment();
} }
}; };
xhr.ontimeout = (e) => { xhr.ontimeout = (e) => {
@ -404,22 +402,28 @@ function getThreadInfo() {
xhr.onerror = (e) => { xhr.onerror = (e) => {
console.log(e) console.log(e)
}; };
} }
let url = disqusjs.config.api + '3.0/threads/list.json?forum=' + disqusjs.config.shortname + '&thread=ident:' + disqusjs.config.identifier + '&api_key=' + disqusjs.config.apikey; /*
* Name: getComment()
* Description: get the comment content
* API URI: /3.0/posts/list.json?forum=[shortname]&thread=[thread id]&api_key=[apikey]
*/
getComment = () => {
let url = disqusjs.config.api + '3.0/posts/list.json?forum=' + disqusjs.config.shortname + '&thread=' + disqusjs.page.id + '&api_key=' + disqusjs.config.apikey;
xhr.open('GET', url, true); xhr.open('GET', url, true);
xhr.timeout = 4000; xhr.timeout = 4000;
xhr.send(); xhr.send();
xhr.onload = function () { xhr.onload = function () {
if (this.status == 200 || this.status == 304) { if (this.status == 200 || this.status == 304) {
var response = JSON.parse(this.responseText).response[0]; var res = JSON.parse(this.responseText);
disqusjs.page = { if (res.code === 0) {
id: response.id, getCommentList(res.response);
title: response.title, } else {
isClosed: response.isClosed, // Have error when get comments.
length: response.posts }
};
getComment();
} }
}; };
xhr.ontimeout = (e) => { xhr.ontimeout = (e) => {
@ -435,7 +439,7 @@ function getThreadInfo() {
* Description: Render JSON to comment list components * Description: Render JSON to comment list components
*/ */
function getCommentList(data) { getCommentList = (data) => {
var topLevelComments = []; var topLevelComments = [];
var childComments = []; var childComments = [];
@ -477,7 +481,7 @@ function getCommentList(data) {
renderComment(commentLists) renderComment(commentLists)
} }
function renderComment(data) { renderComment = (data) => {
var disqusjsBaseTpl = ` var disqusjsBaseTpl = `
<div id="dsqjs"> <div id="dsqjs">
<section class="dsqjs-action"></section> <section class="dsqjs-action"></section>
@ -513,7 +517,6 @@ function renderComment(data) {
return; return;
} }
console.log(nesting < 4)
if (nesting < 4) { if (nesting < 4) {
var html = '<ul class="dsqjs-list dsqjs-children">'; var html = '<ul class="dsqjs-list dsqjs-children">';
} else { } else {
@ -542,8 +545,7 @@ function renderComment(data) {
html += baidu.template(commentBodyTpl, comment) html += baidu.template(commentBodyTpl, comment)
html += `${childrenComments(s)} html += `${childrenComments(s)}</li>`
</li>`
}) })
html += '</ul>'; html += '</ul>';
@ -577,12 +579,24 @@ function renderComment(data) {
html += baidu.template(commentBodyTpl, comment) html += baidu.template(commentBodyTpl, comment)
html += `${childrenComments(s)} html += `${childrenComments(s)}</li>`;
</li>`;
document.getElementById('dsqjs-list').insertAdjacentHTML('beforeend', html); document.getElementById('dsqjs-list').insertAdjacentHTML('beforeend', html);
}) })
} }
main = () => {
disqusjs.mode = getLS('disqusjs_mode');
if (disqusjs.mode === 'disqus') {
loadDisqus();
} else if (disqusjs.mode === 'dsqjs') {
getThreadInfo();
} else {
// Run checkDisqus() when no localStorage item
// disqusjs.mode will be set in checkDisqus()
checkDisqus();
}
}
getThreadInfo(); main();
Loading…
Cancel
Save