forked from mqyqingfeng/AutoType
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autotype.min.js
1 lines (1 loc) · 2.28 KB
/
autotype.min.js
1
!function(){function t(t,e,n){EventEmitter.call(this),this.element="string"==typeof t?document.querySelector(t):t,this.arr=e,this.textArr=[],this.options=i.extend({},this.constructor.defaultOptions,n),this.index=0,this.handle()}var e="object"==typeof self&&self.self==self&&self||"object"==typeof global&&global.global==global&&global||this||{},i={extend:function(t){for(var e=1,i=arguments.length;e<i;e++)for(var n in arguments[e])arguments[e].hasOwnProperty(n)&&(t[n]=arguments[e][n]);return t}};t.VERSION="1.0.0",t.defaultOptions={speed:200,type:""};var n=t.prototype=new EventEmitter;n.constructor=t,n.handle=function(){var t=this.arr[this.index];if(!t)return this.render(!0),void this.emit("end");switch(t.type){case"text":this.handleText(t);break;case"wait":this.handleWait(t);break;case"delete":this.handleDelete(t);break;case"br":this.handleBr(t);break;case"img":this.handleImg(t);break;default:this.handlePlainText(t)}},n.handleText=function(t){var e=t.text.split("");this.type(e,0,t.time?t.time:this.options.speed)},n.type=function(t,e,i){e<t.length?(this.textArr.push(t[e]),this.render(),setTimeout(this.type.bind(this),i,t,++e,i)):(this.index++,this.handle())},n.render=function(t){this.element.innerHTML=this.textArr.join("")+(t?"":this.options.type),this.element.scrollTop=this.element.scrollHeight},n.handleWait=function(t){this.index++,setTimeout(this.handle.bind(this),t.time?t.time:this.options.speed)},n.handleDelete=function(t){this.delete(t.num,t.time?t.time:this.options.speed)},n.delete=function(t,e){t>0?(this.textArr.pop(),this.render(),setTimeout(this.delete.bind(this),e,--t,e)):this.next()},n.handleBr=function(t){this.textArr.push("<br /><br />"),this.index++,setTimeout(this.handle.bind(this),t.time?t.time:this.options.speed)},n.handleImg=function(t){var e=["<img "];for(var i in t)e.push(i+'="'+t[i]+'" ');e.push(" />"),this.textArr.push(e.join("")),this.render();var n=this;setTimeout(function(){n.next()},t.time?t.time:this.options.speed)},n.handlePlainText=function(t){this.textArr.push(t),this.render();var e=this;setTimeout(function(){e.next()},t.time?t.time:this.options.speed)},n.next=function(){this.index++,this.handle()},"undefined"==typeof exports||exports.nodeType?e.AutoType=t:("undefined"!=typeof module&&!module.nodeType&&module.exports&&(exports=module.exports=t),exports.AutoType=t)}();