/*! * v 0.2.11 * * https://github.com/fooleap/disqus-php-api * * Copyright 2017-2018 fooleap * Released under the MIT license */ (function (global) { 'use strict'; var d = document, l = localStorage, scripts = d.scripts, lasturl = scripts[scripts.length - 1].src, filepath = lasturl.substring(0, lasturl.lastIndexOf('/')), isEdge = navigator.userAgent.indexOf("Edge") > -1, isIE = !!window.ActiveXObject || "ActiveXObject" in window; function getLocation(href) { var link = d.createElement('a'); link.href = href; return link; } function getAjax(url, success, error) { var xhr = new XMLHttpRequest(); xhr.open ('GET', encodeURI(url)); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { success(xhr.responseText); } } xhr.onerror = error; xhr.withCredentials = true; xhr.send(); return xhr; } function postAjax(url, data, success, error) { var params = typeof data == 'string' ? data : Object.keys(data).map( function(k){ return encodeURIComponent(k) + '=' + encodeURIComponent(data[k]) } ).join('&'); var xhr = new XMLHttpRequest(); xhr.open('POST', url); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { success(xhr.responseText); } }; xhr.onerror = error; xhr.withCredentials = true; xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.send(params); return xhr; } // matches & closest polyfill https://github.com/jonathantneal/closest (function (ElementProto) { if (typeof ElementProto.matches !== 'function') { ElementProto.matches = ElementProto.msMatchesSelector || ElementProto.mozMatchesSelector || ElementProto.webkitMatchesSelector || function matches(selector) { var element = this; var elements = (element.document || element.ownerDocument).querySelectorAll(selector); var index = 0; while (elements[index] && elements[index] !== element) { ++index; } return Boolean(elements[index]); }; } if (typeof ElementProto.closest !== 'function') { ElementProto.closest = function closest(selector) { var element = this; while (element && element.nodeType === 1) { if (element.matches(selector)) { return element; } element = element.parentNode; } return null; }; } })(window.Element.prototype); // 访客信息 var User = function () { this.dom = arguments[0]; this.opts = arguments[1]; this.init(); this.autologin(); } User.prototype = { // 初始化访客信息 init: function(){ var _ = this; // 读取访客信息 _.name = l.getItem('name'); _.email = l.getItem('email'); _.url = l.getItem('url'); _.avatar = l.getItem('avatar'); _.type = l.getItem('type'); _.logged_in = l.getItem('logged_in'); var boxarr = _.dom.getElementsByClassName('comment-box'); if( _.logged_in == 'true' ) { [].forEach.call(boxarr,function(item){ if(_.type == '1'){ item.querySelector('.comment-form-wrapper').classList.add('logged-in'); } item.querySelector('.comment-form-name').value = _.name; item.querySelector('.comment-form-email').value = _.email; item.querySelector('.comment-form-url').value = _.url; item.querySelector('.comment-avatar-image').src = _.avatar; }); } else { [].forEach.call(boxarr,function(item){ item.querySelector('.comment-form-wrapper').classList.remove('logged-in'); item.querySelector('.comment-form-name').value = ''; item.querySelector('.comment-form-email').value = ''; item.querySelector('.comment-form-url').value = ''; item.querySelector('.comment-avatar-image').src = _.dom.querySelector('.comment-avatar-image').dataset.avatar; }); l.setItem('logged_in', 'false'); } }, // 自动登录 autologin: function(){ var _ = this; getAjax( _.opts.api +'/user.php', function(resp){ var data = JSON.parse(resp); if( data.code == 0 ){ _.submit(data.response); } else { if( _.type == '1' ){ l.setItem('logged_in', 'false'); _.init(); } } }, function(){ }) }, // 登录 login: function(){ var _ = this; var popup = window.open(_.opts.api+'/login.php', 'Disqus Oauth', 'width=470,height=508'); var timer; function isLogged(){ if (!popup || !popup.closed) return; clearInterval(timer); _.user.autologin(); } timer = setInterval(isLogged, 100); }, // 退出登录 logout: function(){ var _ = this; postAjax( _.opts.api + '/logout.php', {}, function(resp){ l.setItem('logged_in', 'false'); l.removeItem('type'); l.removeItem('email'); l.removeItem('avatar'); l.removeItem('name'); l.removeItem('url'); _.user.init(); }) }, // 提交访客信息 submit: function(user){ l.setItem('email', user.email); l.setItem('type', user.type); l.setItem('name', user.name); l.setItem('url', user.url); l.setItem('avatar', user.avatar); l.setItem('logged_in', 'true'); this.init(); } } var iDisqus = function () { var _ = this; // 配置 _.opts = typeof(arguments[1]) == 'object' ? arguments[1] : arguments[0]; _.dom = d.getElementById(typeof(arguments[0]) == 'string' ? arguments[0] : 'comment'); _.opts.api = _.opts.api.slice(-1) == '/' ? _.opts.api.slice(0,-1) : _.opts.api; _.opts.site = _.opts.site || location.origin; if(!!_.opts.url){ var optsUrl = _.opts.url.replace(_.opts.site, ''); _.opts.url = optsUrl.slice(0, 1) != '/' ? '/' + optsUrl : optsUrl; } else if(isEdge || isIE) { _.opts.url = encodeURI(location.pathname) + encodeURI(location.search); } else { _.opts.url = location.pathname + location.search; } _.opts.identifier = _.opts.identifier || _.opts.url; _.opts.link = _.opts.site + _.opts.url; _.opts.title = _.opts.title || d.title; _.opts.slug = !!_.opts.slug ? _.opts.slug.replace(/[^A-Za-z0-9_-]+/g,'') : ''; _.opts.desc = _.opts.desc || (!!d.querySelector('[name="description"]') ? d.querySelector('[name="description"]').content : ''); _.opts.mode = _.opts.mode || 1; _.opts.timeout = _.opts.timeout || 3000; _.opts.toggle = !!_.opts.toggle ? d.getElementById(_.opts.toggle) : null; _.opts.autoCreate = !!_.opts.autoCreate || !!_.opts.auto; // emoji 表情 _.opts.emojiPath = _.opts.emojiPath || _.opts.emoji_path || 'https://cdn.jsdelivr.net/emojione/assets/png/'; _.emojiList = _.opts.emojiList || _.opts.emoji_list || [{ code:'smile', title:'笑脸', unicode:'1f604' },{ code:'mask', title:'生病', unicode:'1f637' },{ code:'joy', title:'破涕为笑', unicode:'1f602' },{ code:'stuck_out_tongue_closed_eyes', title:'吐舌', unicode:'1f61d' },{ code:'flushed', title:'脸红', unicode:'1f633' },{ code:'scream', title:'恐惧', unicode:'1f631' },{ code:'pensive', title:'失望', unicode:'1f614' },{ code:'unamused', title:'无语', unicode:'1f612' },{ code:'grin', title:'露齿笑', unicode:'1f601' },{ code:'heart_eyes', title:'色', unicode:'1f60d' },{ code:'sweat', title:'汗', unicode:'1f613' },{ code:'smirk', title:'得意', unicode:'1f60f' },{ code:'relieved', title:'满意', unicode:'1f60c' },{ code:'rolling_eyes', title:'翻白眼', unicode:'1f644' },{ code:'ok_hand', title:'OK', unicode:'1f44c' },{ code:'v', title:'胜利', unicode:'270c' }]; if(!!_.opts.emoji_preview || !!_.opts.emojiPreview ){ getAjax(_.opts.api +'/eac.min.php', function(resp){ _.eac = JSON.parse(resp); }, function(){ }) } // 默认状态 _.stat = { current: 'idisqus', // 当前显示评论框 loaded: false, // 评论框已加载 loading: false, // 评论加载中 editing: false, // 评论编辑中 offsetTop: 0, // 高度位置 thread: null, // 本页 thread id next: null, // 下条评论 message: null, // 新评论 mediaHtml: null, // 新上传图片 root: [], // 根评论 count: 0, // 评论数 users: [], // Disqus 会员 imageSize: [], // 已上传图片大小 disqusLoaded: false // Disqus 已加载 }; // Disqus 评论框设置 window.disqus_config = function () { this.page.identifier = _.opts.identifier; this.page.title = _.opts.title; this.page.url = _.opts.link; this.callbacks.onReady.push(function() { _.stat.current = 'disqus'; _.stat.disqusLoaded = true; _.dom.querySelector('#idisqus').style.display = 'none'; _.dom.querySelector('#disqus_thread').style.display = 'block'; if( _.opts.mode == 3 && !!_.opts.toggle) { _.opts.toggle.disabled = ''; _.opts.toggle.checked = true; _.opts.toggle.addEventListener('change', _.handle.toggle, false); } }); } // 自动初始化 if( !!_.opts.init ){ _.init(); //console.log(_); } } // TimeAgo https://coderwall.com/p/uub3pw/javascript-timeago-func-e-g-8-hours-ago iDisqus.prototype.timeAgo = function() { var _ = this; var templates = { prefix: "", suffix: "前", seconds: "几秒", minute: "1分钟", minutes: "%d分钟", hour: "1小时", hours: "%d小时", day: "1天", days: "%d天", month: "1个月", months: "%d个月", year: "1年", years: "%d年" }; var template = function (t, n) { return templates[t] && templates[t].replace(/%d/i, Math.abs(Math.round(n))); }; var timer = function (time) { if (!time) return; time = time.replace(/\.\d+/, ""); // remove milliseconds time = time.replace(/-/, "/").replace(/-/, "/"); time = time.replace(/T/, " ").replace(/Z/, " UTC"); time = time.replace(/([\+\-]\d\d)\:?(\d\d)/, " $1$2"); // -04:00 -> -0400 time = new Date(time * 1000 || time); var now = new Date(); var seconds = ((now.getTime() - time) * .001) >> 0; var minutes = seconds / 60; var hours = minutes / 60; var days = hours / 24; var years = days / 365; return templates.prefix + ( seconds < 45 && template('seconds', seconds) || seconds < 90 && template('minute', 1) || minutes < 45 && template('minutes', minutes) || minutes < 90 && template('hour', 1) || hours < 24 && template('hours', hours) || hours < 42 && template('day', 1) || days < 30 && template('days', days) || days < 45 && template('month', 1) || days < 365 && template('months', days / 30) || years < 1.5 && template('year', 1) || template('years', years)) + templates.suffix; }; var elements = _.dom.querySelectorAll('.comment-item-time'); for (var i in elements) { var $this = elements[i]; if (typeof $this === 'object') { $this.title = new Date($this.getAttribute('datetime')); $this.innerHTML = timer($this.getAttribute('datetime')); } } // update time every minute setTimeout(_.timeAgo.bind(_), 60000); } // 初始化评论框 iDisqus.prototype.init = function(){ var _ = this; if(!_.dom){ //console.log('该页面没有评论框!'); return } // 表情 var emojiList = ''; _.emojiList.forEach(function(item){ emojiList += '
  • '; }) _.dom.innerHTML = '
    \n'+ '
    \n'+ '
    评论Disqus 讨论区
    \n'+ '
    \n'+ '
    \n'+ '
    \n'+ '
    \n'+ ' \n'+ '
    \n'+ '
    \n'+ '
      \n'+ '
      \n'+ '
      \n'+ '
      \n'+ '
      \n'+ '
      \n'+ '
      \n'+ ' \n'+ ' \n'+ ' \n'+ ' \n'+ '
      \n'+ '
      \n'+ ' \n'+ ' \n'+ '
      \n'+ '
      \n'+ '
      \n'+ '
      '+ ( _.opts.mode != 1 ? '
      ' : '' ) + '
      \n'+ '
      \n'+ '
      \n'+ ' \n'+ ' 加载更多\n'+ '
      \n'+ '
      '; _.user = new User(_.dom,_.opts); _.box = _.dom.querySelector('.comment-box').outerHTML.replace(/