UNPKG

hellojs-xiaotian

Version:

A clientside Javascript library for standardizing requests to OAuth2 web services (and OAuth1 - with a shim)

2 lines 28.7 kB
/*! hellojs-xiaotian v1.18.4 | (c) 2012-2020 xiaotiandada | MIT https://adodson.com/hello.js/LICENSE */ Object.create||(Object.create=function(){function e(){}return function(t){if(1!=arguments.length)throw new Error("Object.create implementation only accepts one parameter.");return e.prototype=t,new e}}()),Object.keys||(Object.keys=function(e,t,n){for(t in n=[],e)n.hasOwnProperty.call(e,t)&&n.push(t);return n}),Array.prototype.indexOf||(Array.prototype.indexOf=function(e){for(var t=0;t<this.length;t++)if(this[t]===e)return t;return-1}),Array.prototype.forEach||(Array.prototype.forEach=function(e){if(void 0===this||null===this)throw new TypeError;var t=Object(this),n=t.length>>>0;if("function"!=typeof e)throw new TypeError;for(var r=arguments.length>=2?arguments[1]:void 0,o=0;o<n;o++)o in t&&e.call(r,t[o],o,t);return this}),Array.prototype.filter||(Array.prototype.filter=function(e,t){var n=[];return this.forEach(function(r,o,i){e.call(t||void 0,r,o,i)&&n.push(r)}),n}),Array.prototype.map||(Array.prototype.map=function(e,t){var n=[];return this.forEach(function(r,o,i){n.push(e.call(t||void 0,r,o,i))}),n}),Array.isArray||(Array.isArray=function(e){return"[object Array]"===Object.prototype.toString.call(e)}),"object"!=typeof window||"object"!=typeof window.location||window.location.assign||(window.location.assign=function(e){window.location=e}),Function.prototype.bind||(Function.prototype.bind=function(e){if("function"!=typeof this)throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");function t(){}var n=[].slice,r=n.call(arguments,1),o=this,i=function(){return o.apply(this instanceof t?this:e||window,r.concat(n.call(arguments)))};return t.prototype=this.prototype,i.prototype=new t,i});var hello=function(e){return hello.use(e)};hello.utils={extend:function(e){return Array.prototype.slice.call(arguments,1).forEach(function(t){if(Array.isArray(e)&&Array.isArray(t))Array.prototype.push.apply(e,t);else if(e&&(e instanceof Object||"object"==typeof e)&&t&&(t instanceof Object||"object"==typeof t)&&e!==t)for(var n in t)e[n]=hello.utils.extend(e[n],t[n]);else Array.isArray(t)&&(t=t.slice(0)),e=t}),e}},hello.utils.extend(hello,{settings:{redirect_uri:window.location.href.split("#")[0],response_type:"token",display:"popup",state:"",oauth_proxy:"https://auth-server.herokuapp.com/proxy",timeout:2e4,popup:{resizable:1,scrollbars:1,width:500,height:550},scope:["basic"],scope_map:{basic:""},default_service:null,force:null,page_uri:window.location.href},services:{},use:function(e){var t=Object.create(this);return t.settings=Object.create(this.settings),e&&(t.settings.default_service=e),t.utils.Event.call(t),t},init:function(e,t){var n=this.utils;if(!e)return this.services;for(var r in e)e.hasOwnProperty(r)&&"object"!=typeof e[r]&&(e[r]={id:e[r]});return n.extend(this.services,e),t&&(n.extend(this.settings,t),"redirect_uri"in t&&(this.settings.redirect_uri=n.url(t.redirect_uri).href)),this},login:function(){var e,t=this,n=t.utils,r=n.error,o=n.Promise(),i=n.args({network:"s",options:"o",callback:"f"},arguments),s=n.diffKey(i.options,t.settings),a=i.options=n.merge(t.settings,i.options||{});function c(e,t){hello.emit(e,t)}if(a.popup=n.merge(t.settings.popup,i.options.popup||{}),i.network=i.network||t.settings.default_service,o.proxy.then(i.callback,i.callback),o.proxy.then(c.bind(this,"auth.login auth"),c.bind(this,"auth.failed auth")),"string"!=typeof i.network||!(i.network in t.services))return o.reject(r("invalid_network","The provided network was not recognized"));var l=t.services[i.network],u=n.globalEvent(function(e){e?"string"==typeof e&&(e=JSON.parse(e)):e=r("cancelled","The authentication was not completed"),e.error?o.reject(e):(n.store(e.network,e),o.fulfill({network:e.network,authResponse:e}))}),p=n.url(a.redirect_uri).href,f=l.oauth.response_type||a.response_type;/\bcode\b/.test(f)&&!l.oauth.grant&&(f=f.replace(/\bcode\b/,"token")),i.qs=n.merge(s,{client_id:encodeURIComponent(l.id),response_type:encodeURIComponent(f),redirect_uri:encodeURIComponent(p),state:{client_id:l.id,network:i.network,display:a.display,callback:u,state:a.state,redirect_uri:p}});var h=n.store(i.network),d=/[,\s]+/,m=t.settings.scope?[t.settings.scope.toString()]:[],y=n.merge(t.settings.scope_map,l.scope||{});if((a.scope&&m.push(a.scope.toString()),h&&"scope"in h&&h.scope instanceof String&&m.push(h.scope),m=m.join(",").split(d),m=n.unique(m).filter(b),i.qs.state.scope=m.join(","),m=(m=m.map(function(e){return e in y?y[e]:e})).join(",").split(d),m=n.unique(m).filter(b),i.qs.scope=m.join(l.scope_delim||","),!1===a.force)&&(h&&"access_token"in h&&h.access_token&&"expires"in h&&h.expires>(new Date).getTime()/1e3&&0===n.diff((h.scope||"").split(d),(i.qs.state.scope||"").split(d)).length))return o.fulfill({unchanged:!0,network:i.network,authResponse:h}),o;if("page"===a.display&&a.page_uri&&(i.qs.state.page_uri=n.url(a.page_uri).href),"login"in l&&"function"==typeof l.login&&l.login(i),(!/\btoken\b/.test(f)||parseInt(l.oauth.version,10)<2||"none"===a.display&&l.oauth.grant&&h&&h.refresh_token)&&(i.qs.state.oauth=l.oauth,i.qs.state.oauth_proxy=a.oauth_proxy),i.qs.state=encodeURIComponent(JSON.stringify(i.qs.state)),1===parseInt(l.oauth.version,10)?e=n.qs(a.oauth_proxy,i.qs,w):"none"===a.display&&l.oauth.grant&&h&&h.refresh_token?(i.qs.refresh_token=h.refresh_token,e=n.qs(a.oauth_proxy,i.qs,w)):e=n.qs(l.oauth.auth,i.qs,w),c("auth.init",i),"none"===a.display)n.iframe(e,p);else if("popup"===a.display)var v=n.popup(e,p,a.popup),g=setInterval(function(){if((!v||v.closed)&&(clearInterval(g),!o.state)){var e=r("cancelled","Login has been cancelled");v||(e=r("blocked","Popup was blocked")),e.network=i.network,o.reject(e)}},100);else window.location=e;return o.proxy;function w(e){return e}function b(e){return!!e}},logout:function(){var e=this.utils,t=e.error,n=e.Promise(),r=e.args({name:"s",options:"o",callback:"f"},arguments);function o(e,t){hello.emit(e,t)}if(r.options=r.options||{},n.proxy.then(r.callback,r.callback),n.proxy.then(o.bind(this,"auth.logout auth"),o.bind(this,"error")),r.name=r.name||this.settings.default_service,r.authResponse=e.store(r.name),!r.name||r.name in this.services)if(r.name&&r.authResponse){var i=function(t){e.store(r.name,null),n.fulfill(hello.utils.merge({network:r.name},t||{}))},s={};if(r.options.force){var a=this.services[r.name].logout;if(a)if("function"==typeof a&&(a=a(i,r)),"string"==typeof a)e.iframe(a),s.force=null,s.message="Logout success on providers site was indeterminate";else if(void 0===a)return n.proxy}i(s)}else n.reject(t("invalid_session","There was no session to remove"));else n.reject(t("invalid_network","The network was unrecognized"));return n.proxy},getAuthResponse:function(e){return(e=e||this.settings.default_service)&&e in this.services&&this.utils.store(e)||null},events:{}}),hello.utils.extend(hello.utils,{error:function(e,t){return{error:{code:e,message:t}}},qs:function(e,t,n){if(t)for(var r in n=n||encodeURIComponent,t){var o=new RegExp("([\\?\\&])"+r+"=[^\\&]*");e.match(o)&&(e=e.replace(o,"$1"+r+"="+n(t[r])),delete t[r])}return this.isEmpty(t)?e:e+(e.indexOf("?")>-1?"&":"?")+this.param(t,n)},param:function(e,t){var n,r,o={};if("string"==typeof e){if(t=t||decodeURIComponent,r=e.replace(/^[\#\?]/,"").match(/([^=\/\&]+)=([^\&]+)/g))for(var i=0;i<r.length;i++)o[(n=r[i].match(/([^=]+)=(.*)/))[1]]=t(n[2]);return o}t=t||encodeURIComponent;var s=e;for(var a in o=[],s)s.hasOwnProperty(a)&&s.hasOwnProperty(a)&&o.push([a,"?"===s[a]?"?":t(s[a])].join("="));return o.join("&")},store:function(){for(var e,t=["localStorage","sessionStorage"],n=-1;t[++n];)try{(e=window[t[n]]).setItem("test"+n,n),e.removeItem("test"+n);break}catch(t){e=null}if(!e){var r=null;e={getItem:function(e){e+="=";for(var t=document.cookie.split(";"),n=0;n<t.length;n++){var o=t[n].replace(/(^\s+|\s+$)/,"");if(o&&0===o.indexOf(e))return o.substr(e.length)}return r},setItem:function(e,t){r=t,document.cookie=e+"="+t}},r=e.getItem("hello")}return function(t,n,r){var o,i=function(){var t={};try{t=JSON.parse(e.getItem("hello"))||{}}catch(e){}return t}();if(t&&void 0===n)return i[t]||null;if(t&&null===n)try{delete i[t]}catch(e){i[t]=null}else{if(!t)return i;i[t]=n}return o=i,e.setItem("hello",JSON.stringify(o)),i||null}}(),append:function(e,t,n){var r="string"==typeof e?document.createElement(e):e;if("object"==typeof t)if("tagName"in t)n=t;else for(var o in t)if(t.hasOwnProperty(o))if("object"==typeof t[o])for(var i in t[o])t[o].hasOwnProperty(i)&&(r[o][i]=t[o][i]);else"html"===o?r.innerHTML=t[o]:/^on/.test(o)?r[o]=t[o]:r.setAttribute(o,t[o]);return"body"===n?function e(){document.body?document.body.appendChild(r):setTimeout(e,16)}():"object"==typeof n?n.appendChild(r):"string"==typeof n&&document.getElementsByTagName(n)[0].appendChild(r),r},iframe:function(e){this.append("iframe",{src:e,style:{position:"absolute",left:"-1000px",bottom:0,height:"1px",width:"1px"}},"body")},merge:function(){var e=Array.prototype.slice.call(arguments);return e.unshift({}),this.extend.apply(null,e)},args:function(e,t){var n={},r=0,o=null,i=null;for(i in e)if(e.hasOwnProperty(i))break;if(1===t.length&&"object"==typeof t[0]&&"o!"!=e[i])for(i in t[0])if(e.hasOwnProperty(i)&&i in e)return t[0];for(i in e)if(e.hasOwnProperty(i))if(o=typeof t[r],"function"==typeof e[i]&&e[i].test(t[r])||"string"==typeof e[i]&&(e[i].indexOf("s")>-1&&"string"===o||e[i].indexOf("o")>-1&&"object"===o||e[i].indexOf("i")>-1&&"number"===o||e[i].indexOf("a")>-1&&"object"===o||e[i].indexOf("f")>-1&&"function"===o))n[i]=t[r++];else if("string"==typeof e[i]&&e[i].indexOf("!")>-1)return!1;return n},url:function(e){if(e){if(window.URL&&URL instanceof Function&&0!==URL.length)return new URL(e,window.location);var t=document.createElement("a");return t.href=e,t.cloneNode(!1)}return window.location},diff:function(e,t){return t.filter(function(t){return-1===e.indexOf(t)})},diffKey:function(e,t){if(e||!t){var n={};for(var r in e)r in t||(n[r]=e[r]);return n}return e},unique:function(e){return Array.isArray(e)?e.filter(function(t,n){return e.indexOf(t)===n}):[]},isEmpty:function(e){if(!e)return!0;if(Array.isArray(e))return!e.length;if("object"==typeof e)for(var t in e)if(e.hasOwnProperty(t))return!1;return!0},Promise:function(){var e=function(t){if(!(this instanceof e))return new e(t);this.id="Thenable/1.0.6",this.state=0,this.fulfillValue=void 0,this.rejectReason=void 0,this.onFulfilled=[],this.onRejected=[],this.proxy={then:this.then.bind(this)},"function"==typeof t&&t.call(this,this.fulfill.bind(this),this.reject.bind(this))};e.prototype={fulfill:function(e){return t(this,1,"fulfillValue",e)},reject:function(e){return t(this,2,"rejectReason",e)},then:function(t,r){var i=new e;return this.onFulfilled.push(o(t,i,"fulfill")),this.onRejected.push(o(r,i,"reject")),n(this),i.proxy}};var t=function(e,t,r,o){return 0===e.state&&(e.state=t,e[r]=o,n(e)),e},n=function(e){1===e.state?r(e,"onFulfilled",e.fulfillValue):2===e.state&&r(e,"onRejected",e.rejectReason)},r=function(e,t,n){if(0!==e[t].length){var r=e[t];e[t]=[];var o=function(){for(var e=0;e<r.length;e++)r[e](n)};"object"==typeof process&&"function"==typeof process.nextTick?process.nextTick(o):"function"==typeof setImmediate?setImmediate(o):setTimeout(o,0)}},o=function(e,t,n){return function(r){if("function"!=typeof e)t[n].call(t,r);else{var o;try{o=e(r)}catch(e){return void t.reject(e)}i(t,o)}}},i=function(e,t){if(e!==t&&e.proxy!==t){var n;if("object"==typeof t&&null!==t||"function"==typeof t)try{n=t.then}catch(t){return void e.reject(t)}if("function"!=typeof n)e.fulfill(t);else{var r=!1;try{n.call(t,function(n){r||(r=!0,n===t?e.reject(new TypeError("circular thenable chain")):i(e,n))},function(t){r||(r=!0,e.reject(t))})}catch(t){r||e.reject(t)}}}else e.reject(new TypeError("cannot resolve promise with itself"))};return e}(),Event:function(){var e=/[\s\,]+/;return this.parent={events:this.events,findEvents:this.findEvents,parent:this.parent,utils:this.utils},this.events={},this.on=function(t,n){if(n&&"function"==typeof n)for(var r=t.split(e),o=0;o<r.length;o++)this.events[r[o]]=[n].concat(this.events[r[o]]||[]);return this},this.off=function(e,t){return this.findEvents(e,function(e,n){t&&this.events[e][n]!==t||(this.events[e][n]=null)}),this},this.emit=function(e){var t=Array.prototype.slice.call(arguments,1);t.push(e);for(var n=function(n,r){t[t.length-1]="*"===n?e:n,this.events[n][r].apply(this,t)},r=this;r&&r.findEvents;)r.findEvents(e+",*",n),r=r.parent;return this},this.emitAfter=function(){var e=this,t=arguments;return setTimeout(function(){e.emit.apply(e,t)},0),this},this.findEvents=function(t,n){var r=t.split(e);for(var o in this.events)if(this.events.hasOwnProperty(o)&&r.indexOf(o)>-1)for(var i=0;i<this.events[o].length;i++)this.events[o][i]&&n.call(this,o,i)},this},globalEvent:function(e,t){return t=t||"_hellojs_"+parseInt(1e12*Math.random(),10).toString(36),window[t]=function(){try{e.apply(this,arguments)&&delete window[t]}catch(e){console.error(e)}},t},popup:function(e,t,n){var r=document.documentElement;if(n.height&&void 0===n.top){var o=void 0!==window.screenTop?window.screenTop:screen.top,i=screen.height||window.innerHeight||r.clientHeight;n.top=parseInt((i-n.height)/2,10)+o}if(n.width&&void 0===n.left){var s=void 0!==window.screenLeft?window.screenLeft:screen.left,a=screen.width||window.innerWidth||r.clientWidth;n.left=parseInt((a-n.width)/2,10)+s}var c=[];Object.keys(n).forEach(function(e){var t=n[e];c.push(e+(null!==t?"="+t:""))}),-1!==navigator.userAgent.indexOf("Safari")&&-1===navigator.userAgent.indexOf("Chrome")&&(e=t+"#oauth_redirect="+encodeURIComponent(encodeURIComponent(e)));var l=window.open(e,"_blank",c.join(","));return l&&l.focus&&l.focus(),l},responseHandler:function(e,t){var n,r,o=this,i=e.location;if((n=o.param(i.search))&&n.state&&(n.code||n.oauth_token))try{var s=JSON.parse(n.state);n.redirect_uri=s.redirect_uri||i.href.replace(/[\?\#].*$/,"");var a=o.qs(s.oauth_proxy,n);return void i.assign(a)}catch(e){return void console.error("Could not decode state parameter",e)}if((n=o.merge(o.param(i.search||""),o.param(i.hash||"")))&&"state"in n){try{var c=JSON.parse(n.state);o.extend(n,c)}catch(e){var l=decodeURIComponent(n.state);try{var u=JSON.parse(l);o.extend(n,u)}catch(e){console.error("Could not decode state parameter")}}if("access_token"in n&&n.access_token&&n.network)n.expires_in&&0!==parseInt(n.expires_in,10)||(n.expires_in=0),n.expires_in=parseInt(n.expires_in,10),n.expires=(new Date).getTime()/1e3+(n.expires_in||31536e3),f(n,e,t);else if("error"in n&&n.error&&n.network)n.error={code:n.error,message:n.error_message||n.error_description},f(n,e,t);else if(n.callback&&n.callback in t){var p=!!("result"in n&&n.result)&&JSON.parse(n.result);h(t,n.callback)(p),d()}n.page_uri&&(r=n.page_uri,/^https?:/.test(r))&&i.assign(n.page_uri)}else if("oauth_redirect"in n)return void i.assign(decodeURIComponent(n.oauth_redirect));function f(e,t,n){var r=e.callback,i=e.network;if(o.store(i,e),!("display"in e&&"page"===e.display)){if(n&&r&&r in n){try{delete e.callback}catch(e){}o.store(i,e);var s=JSON.stringify(e);try{h(n,r)(s)}catch(e){}}d()}}function h(e,t){return 0!==t.indexOf("_hellojs_")?function(){throw"Could not execute callback "+t}:e[t]}function d(){if(e.frameElement)t.document.body.removeChild(e.frameElement);else{try{e.close()}catch(e){}e.addEventListener&&e.addEventListener("load",function(){e.close()})}}}}),hello.utils.Event.call(hello),function(e){var t={},n={};e.on("auth.login, auth.logout",function(n){n&&"object"==typeof n&&n.network&&(t[n.network]=e.utils.store(n.network)||{})}),function r(){var o=(new Date).getTime()/1e3,i=function(t){e.emit("auth."+t,{network:s,authResponse:a})};for(var s in e.services)if(e.services.hasOwnProperty(s)){if(!e.services[s].id)continue;var a=e.utils.store(s)||{},c=e.services[s],l=t[s]||{};if(a&&"callback"in a){var u=a.callback;try{delete a.callback}catch(e){}e.utils.store(s,a);try{window[u](a)}catch(e){}}if(a&&"expires"in a&&a.expires<o){var p=c.refresh||a.refresh_token;!p||s in n&&!(n[s]<o)?p||s in n||(i("expired"),n[s]=!0):(e.emit("notice",s+" has expired trying to resignin"),e.login(s,{display:"none",force:!1}),n[s]=o+600);continue}if(l.access_token===a.access_token&&l.expires===a.expires)continue;!a.access_token&&l.access_token?i("logout"):a.access_token&&!l.access_token?i("login"):a.expires!==l.expires&&i("update"),t[s]=a,s in n&&delete n[s]}setTimeout(r,1e3)}()}(hello),hello.api=function(){var e=this,t=e.utils,n=t.error,r=t.Promise(),o=t.args({path:"s!",query:"o",method:"s",data:"o",timeout:"i",callback:"f"},arguments);o.method=(o.method||"get").toLowerCase(),o.headers=o.headers||{},o.query=o.query||{},"get"!==o.method&&"delete"!==o.method||(t.extend(o.query,o.data),o.data={});var i=o.data=o.data||{};if(r.then(o.callback,o.callback),!o.path)return r.reject(n("invalid_path","Missing the path parameter from the request"));o.path=o.path.replace(/^\/+/,"");var s=(o.path.split(/[\/\:]/,2)||[])[0].toLowerCase();if(s in e.services){o.network=s;var a=new RegExp("^"+s+":?/?");o.path=o.path.replace(a,"")}o.network=e.settings.default_service=o.network||e.settings.default_service;var c=e.services[o.network];if(!c)return r.reject(n("invalid_network","Could not match the service requested: "+o.network));if(o.method in c&&o.path in c[o.method]&&!1===c[o.method][o.path])return r.reject(n("invalid_path","The provided path is not available on the selected network"));o.oauth_proxy||(o.oauth_proxy=e.settings.oauth_proxy),"proxy"in o||(o.proxy=o.oauth_proxy&&c.oauth&&1===parseInt(c.oauth.version,10)),"timeout"in o||(o.timeout=e.settings.timeout),"formatResponse"in o||(o.formatResponse=!0),o.authResponse=e.getAuthResponse(o.network),o.authResponse&&o.authResponse.access_token&&(o.query.access_token=o.authResponse.access_token);var l,u=o.path;o.options=t.clone(o.query),o.data=t.clone(i);var p=c[{delete:"del"}[o.method]||o.method]||{};if("get"===o.method){var f=u.split(/[\?#]/)[1];f&&(t.extend(o.query,t.param(f)),u=u.replace(/\?.*?(#|$)/,"$1"))}return(l=u.match(/#(.+)/,""))?(u=u.split("#")[0],o.path=l[1]):u in p?(o.path=u,u=p[u]):"default"in p&&(u=p.default),o.redirect_uri=e.settings.redirect_uri,o.xhr=c.xhr,o.jsonp=c.jsonp,o.form=c.form,"function"==typeof u?u(o,h):h(u),r.proxy;function h(e){(e=e.replace(/\@\{([a-z\_\-]+)(\|.*?)?\}/gi,function(e,t,i){var s=i?i.replace(/^\|/,""):"";return t in o.query?(s=o.query[t],delete o.query[t]):o.data&&t in o.data?(s=o.data[t],delete o.data[t]):i||r.reject(n("missing_attribute","The attribute "+t+" is missing from the request")),s})).match(/^https?:\/\//)||(e=c.base+e),o.url=e,t.request(o,function(e,n){if(o.formatResponse){if(!0===e?e={success:!0}:e||(e={}),"delete"===o.method&&(e=!e||t.isEmpty(e)?{success:!0}:e),c.wrap&&(o.path in c.wrap||"default"in c.wrap)){var i=o.path in c.wrap?o.path:"default",s=((new Date).getTime(),c.wrap[i](e,n,o));s&&(e=s)}e&&"paging"in e&&e.paging.next&&("?"===e.paging.next[0]?e.paging.next=o.path+e.paging.next:e.paging.next+="#"+o.path),!e||"error"in e?r.reject(e):r.fulfill(e)}else("object"==typeof n?n.statusCode>=400:"object"==typeof e&&"error"in e)?r.reject(e):r.fulfill(e)})}},hello.utils.extend(hello.utils,{request:function(e,t){var n=this,r=n.error;if(n.isEmpty(e.data)||"FileList"in window||!n.hasBinary(e.data)||(e.xhr=!1,e.jsonp=!1),this.request_cors(function(){return void 0===e.xhr||e.xhr&&("function"!=typeof e.xhr||e.xhr(e,e.query))}))s(e,function(r){var o=n.xhr(e.method,r,e.headers,e.data,t);o.onprogress=e.onprogress||null,o.upload&&e.onuploadprogress&&(o.upload.onprogress=e.onuploadprogress)});else{var o,i=e.query;if(e.query=n.clone(e.query),e.callbackID=n.globalEvent(),!1!==e.jsonp){if(e.query.callback=e.callbackID,"function"==typeof e.jsonp&&e.jsonp(e,e.query),"get"===e.method)return void s(e,function(r){n.jsonp(r,t,e.callbackID,e.timeout)});e.query=i}if(!1!==e.form&&(e.query.redirect_uri=e.redirect_uri,e.query.state=JSON.stringify({callback:e.callbackID}),"function"==typeof e.form&&(o=e.form(e,e.query)),"post"===e.method&&!1!==o))return void s(e,function(r){n.post(r,e.data,o,t,e.callbackID,e.timeout)});t(r("invalid_request","There was no mechanism for handling this request"))}function s(e,t){var r;e.authResponse&&e.authResponse.oauth&&1===parseInt(e.authResponse.oauth.version,10)&&(r=e.query.access_token,delete e.query.access_token,e.proxy=!0),!e.data||"get"!==e.method&&"delete"!==e.method||(n.extend(e.query,e.data),e.data=null);var o=n.qs(e.url,e.query);e.proxy&&(o=n.qs(e.oauth_proxy,{path:o,access_token:r||"",then:e.proxy_response_type||("get"===e.method.toLowerCase()?"redirect":"proxy"),method:e.method.toLowerCase(),suppress_response_codes:e.suppress_response_codes||!0})),t(o)}},request_cors:function(e){return"withCredentials"in new XMLHttpRequest&&e()},domInstance:function(e,t){var n="HTML"+(e||"").replace(/^[a-z]/,function(e){return e.toUpperCase()})+"Element";return!!t&&(window[n]?t instanceof window[n]:window.Element?t instanceof window.Element&&(!e||t.tagName&&t.tagName.toLowerCase()===e):!(t instanceof Object||t instanceof Array||t instanceof String||t instanceof Number)&&t.tagName&&t.tagName.toLowerCase()===e)},clone:function(e){if(null===e||"object"!=typeof e||e instanceof Date||"nodeName"in e||this.isBinary(e)||"function"==typeof FormData&&e instanceof FormData)return e;if(Array.isArray(e))return e.map(this.clone.bind(this));var t={};for(var n in e)t[n]=this.clone(e[n]);return t},xhr:function(e,t,n,r,o){var i,s=new XMLHttpRequest,a=this.error,c=!1;if("blob"===e&&(c=e,e="GET"),e=e.toUpperCase(),s.onload=function(t){var n=s.response;try{n=JSON.parse(s.responseText)}catch(e){401===s.status&&(n=a("access_denied",s.statusText))}var r=function(e){var t,n={},r=/([a-z\-]+):\s?(.*);?/gi;for(;t=r.exec(e);)n[t[1]]=t[2];return n}(s.getAllResponseHeaders());r.statusCode=s.status,o(n||("GET"===e?a("empty_response","Could not get resource"):{}),r)},s.onerror=function(e){var t=s.responseText;try{t=JSON.parse(s.responseText)}catch(e){}o(t||a("access_denied","Could not get resource"))},"GET"===e||"DELETE"===e)r=null;else if(r&&"string"!=typeof r&&!(r instanceof FormData)&&!(r instanceof File)&&!(r instanceof Blob)){var l=new FormData;for(i in r)r.hasOwnProperty(i)&&(r[i]instanceof HTMLInputElement?"files"in r[i]&&r[i].files.length>0&&l.append(i,r[i].files[0]):r[i]instanceof Blob?l.append(i,r[i],r.name):l.append(i,r[i]));r=l}if(s.open(e,t,!0),c&&("responseType"in s?s.responseType=c:s.overrideMimeType("text/plain; charset=x-user-defined")),n)for(i in n)s.setRequestHeader(i,n[i]);return s.send(r),s},jsonp:function(e,t,n,r){var o,i=this.error,s=0,a=document.getElementsByTagName("head")[0],c=i("server_error","server_error"),l=function(){s++||window.setTimeout(function(){t(c),a.removeChild(u)},0)};n=this.globalEvent(function(e){return c=e,!0},n),e=e.replace(new RegExp("=\\?(&|$)"),"="+n+"$1");var u=this.append("script",{id:n,name:n,src:e,async:!0,onload:l,onerror:l,onreadystatechange:function(){/loaded|complete/i.test(this.readyState)&&l()}});window.navigator.userAgent.toLowerCase().indexOf("opera")>-1&&(o=this.append("script",{text:"document.getElementById('"+n+"').onerror();"}),u.async=!1),r&&window.setTimeout(function(){c=i("timeout","timeout"),l()},r),a.appendChild(u),o&&a.appendChild(o)},post:function(e,t,n,r,o,i){var s,a,c=this,l=c.error,u=document,p=null,f=[],h=0,d=null,m=0,y=function(e){m++||r(e)};c.globalEvent(y,o);try{a=u.createElement('<iframe name="'+o+'">')}catch(e){a=u.createElement("iframe")}if(a.name=o,a.id=o,a.style.display="none",n&&n.callbackonload&&(a.onload=function(){y({response:"posted",message:"Content was posted"})}),i&&setTimeout(function(){y(l("timeout","The post operation timed out"))},i),u.body.appendChild(a),c.domInstance("form",t)){for(p=t.form,h=0;h<p.elements.length;h++)p.elements[h]!==t&&p.elements[h].setAttribute("disabled",!0);t=p}if(c.domInstance("form",t))for(p=t,h=0;h<p.elements.length;h++)p.elements[h].disabled||"file"!==p.elements[h].type||(p.encoding=p.enctype="multipart/form-data",p.elements[h].setAttribute("name","file"));else{for(d in t)t.hasOwnProperty(d)&&c.domInstance("input",t[d])&&"file"===t[d].type&&((p=t[d].form).encoding=p.enctype="multipart/form-data");var v;for(d in p||(p=u.createElement("form"),u.body.appendChild(p),s=p),t)if(t.hasOwnProperty(d)){var g=c.domInstance("input",t[d])||c.domInstance("textArea",t[d])||c.domInstance("select",t[d]);if(g&&t[d].form===p)g&&t[d].name!==d&&(t[d].setAttribute("name",d),t[d].name=d);else{var w=p.elements[d];if(v)for(w instanceof NodeList||(w=[w]),h=0;h<w.length;h++)w[h].parentNode.removeChild(w[h]);(v=u.createElement("input")).setAttribute("type","hidden"),v.setAttribute("name",d),g?v.value=t[d].value:c.domInstance(null,t[d])?v.value=t[d].innerHTML||t[d].innerText:v.value=t[d],p.appendChild(v)}}for(h=0;h<p.elements.length;h++)(v=p.elements[h]).name in t||!0===v.getAttribute("disabled")||(v.setAttribute("disabled",!0),f.push(v))}p.setAttribute("method","POST"),p.setAttribute("target",o),p.target=o,p.setAttribute("action",e),setTimeout(function(){p.submit(),setTimeout(function(){try{s&&s.parentNode.removeChild(s)}catch(e){try{console.error("HelloJS: could not remove iframe")}catch(e){}}for(var e=0;e<f.length;e++)f[e]&&(f[e].setAttribute("disabled",!1),f[e].disabled=!1)},0)},100)},hasBinary:function(e){for(var t in e)if(e.hasOwnProperty(t)&&this.isBinary(e[t]))return!0;return!1},isBinary:function(e){return e instanceof Object&&(this.domInstance("input",e)&&"file"===e.type||"FileList"in window&&e instanceof window.FileList||"File"in window&&e instanceof window.File||"Blob"in window&&e instanceof window.Blob)},toBlob:function(e){var t=/^data\:([^;,]+(\;charset=[^;,]+)?)(\;base64)?,/i,n=e.match(t);if(!n)return e;for(var r=atob(e.replace(t,"")),o=[],i=0;i<r.length;i++)o.push(r.charCodeAt(i));return new Blob([new Uint8Array(o)],{type:n[1]})}}),function(e){var t=e.api,n=e.utils;n.extend(n,{dataToJSON:function(e){var t=this,n=window,r=e.data;if(t.domInstance("form",r)?r=t.nodeListToJSON(r.elements):"NodeList"in n&&r instanceof NodeList?r=t.nodeListToJSON(r):t.domInstance("input",r)&&(r=t.nodeListToJSON([r])),("File"in n&&r instanceof n.File||"Blob"in n&&r instanceof n.Blob||"FileList"in n&&r instanceof n.FileList)&&(r={file:r}),!("FormData"in n&&r instanceof n.FormData))for(var o in r)if(r.hasOwnProperty(o))if("FileList"in n&&r[o]instanceof n.FileList)1===r[o].length&&(r[o]=r[o][0]);else{if(t.domInstance("input",r[o])&&"file"===r[o].type)continue;t.domInstance("input",r[o])||t.domInstance("select",r[o])||t.domInstance("textArea",r[o])?r[o]=r[o].value:t.domInstance(null,r[o])&&(r[o]=r[o].innerHTML||r[o].innerText)}return e.data=r,r},nodeListToJSON:function(e){for(var t={},n=0;n<e.length;n++){var r=e[n];!r.disabled&&r.name&&("file"===r.type?t[r.name]=r:t[r.name]=r.value||r.innerHTML)}return t}}),e.api=function(){var e=n.args({path:"s!",method:"s",data:"o",timeout:"i",callback:"f"},arguments);return e.data&&n.dataToJSON(e),t.call(this,e)}}(hello),hello.utils.responseHandler(window,window.opener||window.parent),"object"==typeof chrome&&"object"==typeof chrome.identity&&chrome.identity.launchWebAuthFlow&&function(){hello.utils.popup=function(e){return t(e,!0)},hello.utils.iframe=function(e){t(e,!1)},hello.utils.request_cors=function(e){return e(),!0};var e={};function t(e,n){var r={closed:!1};return chrome.identity.launchWebAuthFlow({url:e,interactive:n},function(e){if(void 0!==e){var n=hello.utils.url(e),o={location:{assign:function(e){t(e,!1)},search:n.search,hash:n.hash,href:n.href},close:function(){}};hello.utils.responseHandler(o,window)}else r.closed=!0}),r}chrome.storage.local.get("hello",function(t){e=t.hello||{}}),hello.utils.store=function(t,n){return 0===arguments.length?e:1===arguments.length?e[t]||null:n?(e[t]=n,chrome.storage.local.set({hello:e}),n):null===n?(delete e[t],chrome.storage.local.set({hello:e}),null):void 0}}(),function(){if(/^file:\/{3}[^\/]/.test(window.location.href)&&window.cordova){hello.utils.iframe=function(e,t){hello.utils.popup(e,t,{hidden:"yes"})};var e=hello.utils.popup;hello.utils.popup=function(t,n,r){var o=e.call(this,t,n,r);try{if(o&&o.addEventListener){var i=hello.utils.url(n),s=i.origin||i.protocol+"//"+i.hostname;o.addEventListener("loadstart",function(e){var t=e.url;if(0===t.indexOf(s)){var n=hello.utils.url(t),r={location:{assign:function(e){o.executeScript({code:'window.location.href = "'+e+';"'})},search:n.search,hash:n.hash,href:n.href},close:function(){if(o.close){o.close();try{o.closed=!0}catch(e){}}}};hello.utils.responseHandler(r,window)}})}}catch(e){}return o}}}(),"function"==typeof define&&define.amd&&define(function(){return hello}),"object"==typeof module&&module.exports&&(module.exports=hello);