-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpushr-amp-helper.html
33 lines (31 loc) · 7.61 KB
/
webpushr-amp-helper.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!doctype html>
<html>
<!-- AMP Web Push Helper IFrame -->
<head>
<meta charset="utf-8">
<script>(function(){var f;function g(a){for(var b=["object"==typeof window&&window,"object"==typeof self&&self,"object"==typeof global&&global,a],c=0;c<b.length;++c){var d=b[c];if(d&&d.Math==Math)return d}return globalThis}var k=g(this);function l(a,b){b=void 0===b?"":b;try{return decodeURIComponent(a)}catch(c){return b}};var m=/(?:^[#?]?|&)([^=&]+)(?:=([^&]*))?/g;self.__AMP_LOG=self.__AMP_LOG||{user:null,dev:null,userForEmbed:null};var n=self.__AMP_LOG;/*
https://mths.be/cssescape v1.5.1 by @mathias | MIT license */
function p(){var a=100;this.M=a;this.C=this.I=0;this.w=Object.create(null)}p.prototype.has=function(a){return!!this.w[a]};p.prototype.get=function(a){var b=this.w[a];if(b)return b.access=++this.C,b.payload};
p.prototype.put=function(a,b){this.has(a)||this.I++;this.w[a]={payload:b,access:this.C};if(!(this.I<=this.M)){if(n.dev)a=n.dev;else throw Error("failed to call initLogConstructor");a.warn("lru-cache","Trimming LRU cache");a=this.w;var c=this.C+1,d;for(d in a){var e=a[d].access;if(e<c){c=e;var h=d}}void 0!==h&&(delete a[h],this.I--)}};(function(a){return a||{}})({c:!0,v:!0,a:!0,ad:!0,action:!0});var r,t;
function u(a){var b;r||(r=self.document.createElement("a"),t=self.__AMP_URL_CACHE||(self.__AMP_URL_CACHE=new p));var c=b?null:t,d=r;if(c&&c.has(a))a=c.get(a);else{d.href=a;d.protocol||(d.href=d.href);var e={href:d.href,protocol:d.protocol,host:d.host,hostname:d.hostname,port:"0"==d.port?"":d.port,pathname:d.pathname,search:d.search,hash:d.hash,origin:null};"/"!==e.pathname[0]&&(e.pathname="/"+e.pathname);if("http:"==e.protocol&&80==e.port||"https:"==e.protocol&&443==e.port)e.port="",e.host=e.hostname;
e.origin=d.origin&&"null"!=d.origin?d.origin:"data:"!=e.protocol&&e.host?e.protocol+"//"+e.host:e.href;c&&c.put(a,e);a=e}return a};function v(a){a||(a={debug:!1,windowContext:window});this.o={};this.l={};this.J=a.debug;this.A=this.N=this.O=!1;this.B=this.G=this.H=this.j=this.F=null;this.h=a.windowContext||window}f=v.prototype;
f.listen=function(a){var b=this;return(new Promise(function(c,d){b.A?d(Error("Already connected.")):b.O?d(Error("Already listening for connections.")):Array.isArray(a)?(b.H=b.W.bind(b,a,c,d),b.h.addEventListener("message",b.H)):d(Error("allowedOrigins should be a string array of allowed origins to accept messages from. Got:",a))})).then(function(){b.send(v.Topics.CONNECT_HANDSHAKE,null);b.A=!0})};
f.W=function(a,b,c,d){var e=d.data,h=d,z=h.ports;a:{h=u(h.origin).origin;for(var q=0;q<a.length;q++)if(u(a[q]).origin===h){a=!0;break a}a=!1}a&&e&&e.topic===v.Topics.CONNECT_HANDSHAKE&&(this.h.removeEventListener("message",this.H),this.j=z[0],this.B=this.K.bind(this),this.j.addEventListener("message",this.B,!1),this.j.start(),b())};
f.connect=function(a,b){var c=this;return new Promise(function(d,e){a||e(Error("Provide a valid Window context to connect to."));b||e(Error("Provide an expected origin for the remote Window or provide the wildcard *."));c.A?e(Error("Already connected.")):c.N?e(Error("Already connecting.")):(c.F=new MessageChannel,c.j=c.F.port1,c.G=c.V.bind(c,c.j,b,d),c.j.addEventListener("message",c.G),c.j.start(),a.postMessage({topic:v.Topics.CONNECT_HANDSHAKE},"*"===b?"*":u(b).origin,[c.F.port2]))})};
f.V=function(a,b,c){this.A=!0;a.removeEventListener("message",this.G);this.B=this.K.bind(this);a.addEventListener("message",this.B,!1);c()};f.K=function(a){a=a.data;if(this.o[a.id]&&a.isReply){var b=this.o[a.id];delete this.o[a.id];var c=b.promiseResolver;b.message=a.data;c([a.data,this.L.bind(this,a.id,b.topic)])}else{var d=this.l[a.topic];if(d)for(var e=0;e<d.length;e++)(0,d[e])(a.data,this.L.bind(this,a.id,a.topic))}};f.on=function(a,b){this.l[a]?this.l[a].push(b):this.l[a]=[b]};
f.off=function(a,b){if(b){var c=this.l[a].indexOf(b);-1!==c&&this.l[a].splice(c,1)}else this.l[a]&&delete this.l[a]};f.L=function(a,b,c){var d=this,e={id:a,topic:b,data:c,isReply:!0};this.j.postMessage(e);return new Promise(function(a){d.o[e.id]={message:c,topic:b,promiseResolver:a}})};f.send=function(a,b){var c=this,d={id:crypto.getRandomValues(new Uint8Array(10)).join(""),topic:a,data:b};this.j.postMessage(d);return new Promise(function(e){c.o[d.id]={message:b,topic:a,promiseResolver:e}})};
k.Object.defineProperties(v,{Topics:{configurable:!0,enumerable:!0,get:function(){return{CONNECT_HANDSHAKE:"topic-connect-handshake",NOTIFICATION_PERMISSION_STATE:"topic-notification-permission-state",SERVICE_WORKER_STATE:"topic-service-worker-state",SERVICE_WORKER_REGISTRATION:"topic-service-worker-registration",SERVICE_WORKER_QUERY:"topic-service-worker-query",STORAGE_GET:"topic-storage-get"}}}});function w(){var a={debug:!1};this.J=a&&a.debug;this.h=a.windowContext||window;this.m=new v({debug:this.J,windowContext:this.h});this.D={}}function x(a,b){var c=!0,d=null;a({success:c,error:c?void 0:d,result:c?b:void 0})}f=w.prototype;f.P=function(a,b){if(a&&a.isQueryTopicSupported){var c=!1,d;for(d in v.Topics)a.isQueryTopicSupported===v.Topics[d]&&(c=!0);x(b,c)}else x(b,Notification.permission)};
f.U=function(a,b){var c=null;try{if(a&&a.key&&this.h.localStorage)c=this.h.localStorage.getItem(a.key);else{if(!n.user)throw Error("failed to call initLogConstructor");n.user.warn("amp-web-push","LocalStorage retrieval failed.")}}catch(d){}x(b,c)};
f.T=function(a,b){var c={isControllingFrame:!!this.h.navigator.serviceWorker.controller,url:this.h.navigator.serviceWorker.controller?this.h.navigator.serviceWorker.controller.scriptURL:null,state:this.h.navigator.serviceWorker.controller?this.h.navigator.serviceWorker.controller.state:null};x(b,c)};
f.S=function(a,b){if(!a||!a.workerUrl||!a.registrationOptions)throw Error("Expected arguments workerUrl and registrationOptions in message, got:",a);this.h.navigator.serviceWorker.register(a.workerUrl,a.registrationOptions).then(function(){x(b,null)}).catch(function(a){x(b,a?a.message||a.toString():null)})};f.messageServiceWorker=function(a){this.h.navigator.serviceWorker.controller.postMessage({command:a.topic,payload:a.payload})};
f.R=function(a,b){var c=this;if(!a||!a.topic)throw Error("Expected argument topic in message, got:",a);(new Promise(function(b){c.D[a.topic]=b;c.waitUntilWorkerControlsPage().then(function(){c.messageServiceWorker(a)})})).then(function(d){delete c.D[a.topic];return x(b,d)})};
function y(a){a=a.h.location.search;var b=Object.create(null);if(a)for(var c;c=m.exec(a);){var d=l(c[1],c[1]);c=c[2]?l(c[2].replace(/\+/g," "),c[2]):"";b[d]=c}var e=b;if(!e.parentOrigin)throw Error("Expecting parentOrigin URL query parameter.");return e.parentOrigin}f.X=function(a){a=a.data;var b=a.command;a=a.payload;var c=this.D[b];"function"===typeof c&&c(a)};
function A(a){return a.h.navigator.serviceWorker&&a.h.navigator.serviceWorker.controller&&"activated"===a.h.navigator.serviceWorker.controller.state}f.waitUntilWorkerControlsPage=function(){var a=this;return new Promise(function(b){A(a)?b():a.h.navigator.serviceWorker.addEventListener("controllerchange",function(){A(a)?b():a.h.navigator.serviceWorker.controller.addEventListener("statechange",function(){A(a)&&b()})})})};
f.run=function(a){var b=this;this.m.on(v.Topics.NOTIFICATION_PERMISSION_STATE,this.P.bind(this));this.m.on(v.Topics.SERVICE_WORKER_STATE,this.T.bind(this));this.m.on(v.Topics.SERVICE_WORKER_REGISTRATION,this.S.bind(this));this.m.on(v.Topics.SERVICE_WORKER_QUERY,this.R.bind(this));this.m.on(v.Topics.STORAGE_GET,this.U.bind(this));this.waitUntilWorkerControlsPage().then(function(){b.h.navigator.serviceWorker.addEventListener("message",b.X.bind(b))});this.m.listen([a||y(this)])};
window._ampWebPushHelperFrame=new w;window._ampWebPushHelperFrame.run();})();
//# sourceMappingURL=amp-web-push-helper-frame.js.map
</script>
</head>
<body>
</body>
</html>