spoiler:var WebPush=function(){var o=!1,n=!1,e=!1,t=!1,i=null,r=!1,s=!1,c=null,u=!1,a="https://tweakers.net/",l=function(){s=jsConfig.get(["webPush","enabled"]),c=jsConfig.get(["webPush","applicationServerPublicKey"]),u=jsConfig.get("debug"),a=jsConfig.get("TnetBaseURL")},d=function(){l(),e=!0,navigator.serviceWorker.register("/service_worker.js",{scope:"/"}).then(function(e){return u&&console.log("Service Worker is registered",e),navigator.serviceWorker.ready}).then(function(e){u&&e.active&&e.active.postMessage("enableDebug")}).catch(function(e){u&&console.error("Service Worker Error",e),o=n=!1})},g=function(){return"denied"!==Notification.permission},h=function(){return e||(window.location.href.startsWith(a)?d():i=P(),e=!0),i?i.then(function(e){return e.getServiceWorker()}):navigator.serviceWorker.ready},f=function(){return h().then(function(e){return e.pushManager.getSubscription()})},p=function(){n?(u&&console.log("Subscribing user to push notifications"),h().then(function(e){return e.pushManager.subscribe({userVisibleOnly:!0,applicationServerKey:v(c)})}).then(function(e){u&&console.log("User is subscribed:",e,JSON.stringify(e)),b(e),t=!0}).catch(function(e){u&&console.log("Failed to subscribe the user: ",e),t=!1,i&&(r=!0),n=g(),w()})):u&&console.error("Not trying to subscribe user due to unavailable push notifications")},b=function(e,o){var n=(PushManager.supportedContentEncodings||["aesgcm"])[0];Ajax.sendRequest(location.protocol+"//"+location.host+"/ajax/webpush/"+(o?"removeSubscription":"storeSubscription")+"/",{method:"POST",type:"json",async:!0,nocache:!0,appendSid:!0,strictSidCheck:!0,handler:function(e){u&&console.log("Enabling subscription, with response: ",e),t=!o}},"subscription="+encodeURIComponent(JSON.stringify(e))+"&contentEncoding="+encodeURIComponent(n))},v=function(e){for(var o=(e+"=".repeat((4-e.length%4)%4)).replace(/\-/g,"+").replace(/_/g,"/"),n=window.atob(o),t=new Uint8Array(n.length),i=0;i<n.length;++i)t[i]=n.charCodeAt(i);return t},w=function(){var e=window.PopupGenerator&&PopupGenerator.get("userNotificationsPopup",!0);e&&e.options.addWebPushInfo(e)},P=function(){return new Promise(function(e,o){var n=document.createElement("iframe");n.style.cssText="position:absolute;top:0;left:0;width:0;height:0;display:none",n.src=a+"webpush/serviceworker/";var t=setTimeout(function(){n.onload=null,o("failed to load the iframe within 5 seconds")},5e3);n.onload=function(){clearTimeout(t);try{if(n.contentWindow.WebPush)return void e(n.contentWindow.WebPush)}catch(e){return void o("cannot access iframe window")}o("failed to load the iframe")},document.body.appendChild(n)})};return{init:function(){l(),c?("serviceWorker"in navigator?"Notification"in window&&"showNotification"in ServiceWorkerRegistration.prototype?"PushManager"in window?(o=!0,(n=g())?u&&console.log("Service Worker and Push is supported and enabled"):u&&console.log("Notifications are blocked.")):u&&console.log("No support for Push messaging."):u&&console.log("No support for notifications."):u&&console.log("No support for service workers."),n?s?f().then(function(e){(t=null!==e)?u&&console.log("User IS subscribed."):u&&console.log("User is NOT subscribed."),s&&!t&&p()}):t=!1:u&&console.log("Push is not available?!")):u&&console.log("Skipping notifications due to lacking server support")},toggleWebPush:function(){Ajax.sendRequest(location.protocol+"//"+location.host+"/ajax/webpush/toggleWebPush/",{method:"POST",type:"json",async:!0,nocache:!0,appendSid:!0,strictSidCheck:!0,handler:function(e){e&&"webPushEnabled"in e?(s=e.webPushEnabled)?(u&&console.log("Enabled WebPush"),p()):(u&&console.log("Disabled WebPush"),n&&f().then(function(e){if(e)return b(e,!0),e.unsubscribe()}).catch(function(e){u&&console.log("Error unsubscribing",e)}).then(function(){u&&console.log("User is unsubscribed."),t=!1})):u&&console.error("Failed to toggle generic WebPush setting")}},"webPushEnabled="+(s?0:1))},registerServiceWorker:d,getServiceWorker:h,isPushSupported:function(){return o},isPushAvailable:function(){return n},isPushEnabled:function(){return s},isPushProxyFailed:function(){return r}}}();