Advertisement
shubhamgoyal

Untitled

Jun 27th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var dexecure = {
  2.     "server": {
  3.         "www.lazada.sg": "d1jgs8c63vf72f.cloudfront.net",
  4.         "sg-live-g-02.slatic.net": "d1ksvckhah2lla.cloudfront.net",
  5.         "sg-live-g-03.slatic.netsg-live-g-01.slatic.net": "d20qka2w418dmy.cloudfront.net",
  6.         "sg-live.slatic.net": 0,
  7.         "lazada.d3.sc.omtrdc.net": 0,
  8.         "s3.amazonaws.com": "dcx7enujj9l54.cloudfront.net"
  9.     },
  10.     "firstPartyDomain": [
  11.         "www.lazada.sg",
  12.         "sg-live-g-02.slatic.net",
  13.         "sg-live-g-03.slatic.netsg-live-g-01.slatic.net",
  14.         "sg-live.slatic.net",
  15.         "lazada.d3.sc.omtrdc.net",
  16.         "s3.amazonaws.com"
  17.     ],
  18.     "pagesEnabled": [
  19.         ""
  20.     ],
  21.     "debugMode": false,
  22.     "imageMatchRegex": "\\.jpe?g|\\.png",
  23.     "optimisationsEnabled": true
  24. };"use strict";function isFirstPartyDomain(e){for(var r=new URL(e),o=dexecure.firstPartyDomain,t=o.length-1;t>=0;t--)if(o[t].toLowerCase()==r.host.toLowerCase())return!0;return!1}function changeToDexecureURL(e){var r=new URL(e);return dexecure.debugMode&&console.log("inputURL is ",e),dexecure.debugMode&&console.log("inputURL.hostname is ",e.hostname),r.hostname=dexecure.server[r.hostname],dexecure.debugMode&&console.log("changedURL.hostname is ",r.hostname),dexecure.debugMode&&console.log("changedURL is ",r.href),r.href}function isCloudflareURL(e){return 0==new URL(e).pathname.indexOf("/cdn-cgi/")}function isPageEnabled(e){return!0}dexecure.optimisationsEnabled&&(self.addEventListener("install",function(e){dexecure.debugMode&&console.log("install triggered"),e.waitUntil(self.skipWaiting())}),self.addEventListener("activate",function(e){e.waitUntil(self.clients.claim())}),self.addEventListener("fetch",function(e){dexecure.debugMode&&console.log("fetch triggered");var r={};e.request.headers.has("Accept")&&(r.Accept=e.request.headers.get("Accept"));var o=new Headers(r),t=new RegExp(dexecure.imageMatchRegex,"i");if(dexecure.debugMode&&console.log("input url is ",e.request.url),isPageEnabled(e.request.referrer)&&t.test(e.request.url.toLowerCase())&&isFirstPartyDomain(e.request.url)&&!isCloudflareURL(e.request.url)){var n=changeToDexecureURL(e.request.url);n=decodeURIComponent(n),dexecure.debugMode&&console.log("output url is ",n),e.respondWith(fetch(n,{mode:"cors",headers:o}).then(function(r){if(r.ok)return r;throw dexecure.debugMode&&console.log("Responding with original image as optimiser was not reachable ",e.request.url),new Error("Unable to fetch optimised image")}).catch(function(r){return dexecure.debugMode&&(console.log("Sending original image as an error occured when trying to optimise ",e.request.url),console.log("The error was ",r)),fetch(e.request)}))}}));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement