forked from jcubic/favloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfavloader.min.js
12 lines (12 loc) · 4.16 KB
/
favloader.min.js
1
2
3
4
5
6
7
8
9
10
11
12
/**@license
*
* favloader v. 0.4.4
*
* Vanilla JavaScript library for loading animation in favicon
*
* Copyright (c) 2018-2019 Jakub T. Jankiewicz <https://jcubic.pl/me>
* Released under the MIT license
*
* Build: Sat, 22 May 2021 11:37:31 +0000
*/
(function(e){var t=typeof window!=="undefined"?window:global;if(typeof define==="function"&&define.amd){define([],e)}else if(typeof module==="object"&&module.exports){module.exports=e()}else{t.favloader=e()}})(function(e){function t(e){var t="("+e.toString()+")()";var n=window.URL||window.webkitURL;var i;try{i=new Blob([t],{type:"application/javascript"})}catch(e){window.BlobBuilder=window.BlobBuilder||window.WebKitBlobBuilder||window.MozBlobBuilder;i=new BlobBuilder;i.append(t);i=i.getBlob()}return new Worker(n.createObjectURL(i))}var n=function(){var a=t(function(){var e=["ms","moz","webkit","o"];for(var t=0;t<e.length&&!self.requestAnimationFrame;++t){self.requestAnimationFrame=self[e[t]+"RequestAnimationFrame"];self.cancelAnimationFrame=self[e[t]+"CancelAnimationFrame"]||self[e[t]+"CancelRequestAnimationFrame"]}var r={};self.addEventListener("message",function(e){var t=e.data;var n=t.id;if(t.type!=="RPC"||n===null){return}if(t.method=="setInterval"){var i=t.params[0];if(typeof i==="number"){r[i]=self.setInterval(function(){self.postMessage({type:"interval",id:i})},t.params[1]);self.postMessage({type:"RPC",id:n,result:i})}else{self.postMessage({type:"RPC",id:n,result:null,error:"Invalid Argument, expected number"})}}else if(t.method=="clearInterval"){self.clearInterval(r[t.params[0]]);delete r[t.params[0]]}})});var i={};var r=function(){var i=0;return function e(t,n){var o=++i;return new Promise(function(i,r){a.addEventListener("message",function e(t){var n=t.data;if(n&&n.type==="RPC"&&n.id===o){if(n.error){r(n.error)}else{i(n.result)}a.removeEventListener("message",e)}});a.postMessage({type:"RPC",method:t,id:o,params:n})})}}();a.addEventListener("message",function(e){var t=e.data;if(t&&t.type==="interval"&&i[t.id]){i[t.id]()}});return{set:function(e,t){var n=Date.now();i[n]=e;r("setInterval",[n,t]);return n},clear:function(e){delete i[e];return r("clearInterval",[e])}}}();var i,r;if(typeof document.hidden!=="undefined"){i="hidden";r="visibilitychange"}else if(typeof document.msHidden!=="undefined"){i="msHidden";r="msvisibilitychange"}else if(typeof document.webkitHidden!=="undefined"){i="webkitHidden";r="webkitvisibilitychange"}function o(e){if(console&&console.warn){console.warn(e)}else{setTimeout(function(){throw new Error(e)},0)}}var a,s,l,u,d,f=0,c,m,p,v,w,h=-.25,b,y;function g(e){if(document.readyState!=="complete"){setTimeout(g.bind(this,e),100);return}p=Object.assign({size:16,radius:6,thickness:2,color:"#0F60A8",duration:5e3},e);if(!l){l=document.querySelector('link[rel*="icon"]');if(!l){l=document.createElement("link");l.setAttribute("rel","icon");document.head.appendChild(l);o("No default icon found, restore state will not work")}else{u=l.getAttribute("href");w=l.getAttribute("type")}}k();if(p.gif){if(typeof parseGIF==="undefined"){throw new Error("parseGIF not defined, please include parseGIF.js file")}parseGIF(p.gif).then(function(e){v=e})}else{if(!s){s=document.createElement("canvas")}s.width=s.height=p.size;a=s.getContext("2d");a.lineCap="round";a.lineWidth=p.thickness;a.strokeStyle=p.color;c=p.duration}m=true}function k(){if(b){n.clear(b);b=null}}function A(){if(u){l.setAttribute("href",u+"?"+Date.now());l.setAttribute("type",w)}else if(l){l.parentNode.removeChild(l)}k()}function I(){if(!m){setTimeout(I,100);return}if(b){return}f=0;if(p.gif&&parseGIF){if(!v){setTimeout(I,100);return}b=n.set(C,20)}else{b=n.set(E,20)}}function B(e){return(e+h)*2*Math.PI}function C(){f++;if(f>=v.uris.length){f=0}M(v.uris[f])}function F(e){return B(e+h)+B(Math.max(0,e*2-1))}function R(e){return B(e+h)+B(Math.min(1,e*2))}function M(e){var t,n=document.querySelector('link[rel*="icon"]');(t=n.cloneNode(true)).setAttribute("href",e);n.parentNode.replaceChild(t,n);l=t}function E(){a.clearRect(0,0,p.size,p.size);if(typeof p.frame==="function"){p.frame(a)}else{var e=f%c/c;a.beginPath();var t=Math.round(p.size/2);a.arc(t,t,p.radius,F(e),R(e));a.stroke()}M(a.canvas.toDataURL());f+=c/100}return{init:g,start:I,stop:A,version:"0.4.4"}});