-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathpwacompat.min.js
7 lines (7 loc) · 3.45 KB
/
pwacompat.min.js
1
2
3
4
5
6
7
(function(){function l(){var a=document.head.querySelector('link[rel="manifest"]'),b=a?a.href:"";Promise.resolve().then(function(){if(!b)throw'can\'t find <link rel="manifest" href=".." />\'';var a={};"use-credentials"===b.crossOrigin&&(a.credentials="include");return window.fetch(b,a)}).then(function(a){return a.json()}).then(function(a){return n(a,b)}).catch(function(a){return console.warn("pwacompat.js error",a)})}function p(a,b){a=document.createElement(a);for(var c in b)a.setAttribute(c,b[c]);
document.head.appendChild(a);return a}function h(a,b){b&&(!0===b&&(b="yes"),p("meta",{name:a,content:b}))}function n(a,b){function c(e,c,b){var d=e.width,g=e.height,f=window.devicePixelRatio;e=q({width:d*f,height:g*f});e.scale(f,f);e.fillStyle=a.background_color||"#f8f9fa";e.fillRect(0,0,d,g);e.translate(d/2,(g-32)/2);e.font="24px HelveticaNeue-CondensedBold";e.fillStyle=l?"white":"black";d=e.measureText(r).width;b&&(g=b.width/f,f=b.height/f,128<f&&(g/=f/128,f=128),48<=g&&48<=f&&(e.drawImage(b,g/
-2,f/-2,g,f),e.translate(0,f/2+32)));e.fillText(r,d/-2,0);b=document.createElement("link");b.setAttribute("rel","apple-touch-startup-image");b.setAttribute("media","(orientation: "+c+")");b.setAttribute("href",e.canvas.toDataURL());return b}function d(a){var b=c(window.screen,"portrait",a);a=c({width:window.screen.height,height:window.screen.width},"landscape",a);t.forEach(function(a){return a.remove()});document.head.appendChild(b);document.head.appendChild(a);t.add(b);t.add(a)}var k=a.icons||[];
k.sort(function(a,b){return parseInt(b.sizes,10)-parseInt(a.sizes,10)});var u=k.map(function(a){a={rel:"icon",href:new URL(a.src,b),sizes:a.sizes};p("link",a);if(v)return a.rel="apple-touch-icon",p("link",a)});k=-1!==A.indexOf(a.display);h("mobile-web-app-capable",k);B&&(h("msapplication-starturl",a.start_url||"/"),h("msapplication-TileColor",a.theme_color));if(v){var l=w(a.background_color||"#f8f9fa"),n=w(a.theme_color||"black"),r=a.name||a.short_name||document.title,x=C(a.related_applications);
x&&h("apple-itunes-app","app-id="+x);h("apple-mobile-web-app-status-bar-style",n?"default":"black");h("apple-mobile-web-app-capable",k);h("apple-mobile-web-app-title",r);var t=new Set;d(null);if(u.length){var y=u[0],m=new Image;m.onload=function(){d(m);if(a.background_color){var b=z(m,a.background_color);null!==b&&(y.href=b,u.slice(1).forEach(function(b){var c=new Image;c.onload=function(){var d=z(c,a.background_color,!0);b.href=d};c.src=b.href}))}};m.src=y.href}}}function C(a){var b;(a||[]).filter(function(a){return"itunes"===
a.platform}).forEach(function(a){a.id?b=a.id:(a=a.url.match(/id(\d+)/))&&(b=a[1])});return b}function w(a){var b=q();b.fillStyle=a;b.fillRect(0,0,1,1);a=b.getImageData(0,0,1,1).data.map(function(a){a/=255;return.03928>a?a/12.92:Math.pow((a+.055)/1.055,2.4)});return 3<Math.abs(1.05/(.2126*a[0]+.7152*a[1]+.0722*a[2]+.05))}function z(a,b,c){c=void 0===c?!1:c;var d=q(a);d.drawImage(a,0,0);if(!c&&255==d.getImageData(0,0,1,1).data[3])return null;d.globalCompositeOperation="destination-over";d.fillStyle=
b;d.fillRect(0,0,a.width,a.height);return d.canvas.toDataURL()}function q(a){a=void 0===a?{width:1,height:1}:a;var b=a.height,c=document.createElement("canvas");c.width=a.width;c.height=b;return c.getContext("2d")}if("fetch"in window){var A=["standalone","fullscreen","minimal-ui"],v=navigator.vendor&&-1!==navigator.vendor.indexOf("Apple"),B=navigator.userAgent&&-1!==navigator.userAgent.indexOf("Edge");"complete"===document.readyState?l():window.addEventListener("load",l)}})();