fasterweb-1.0.1/js/fasterLibrary.js
js/fasterLibrary.js
!function(e){var t={};function n(o){if(t[o])return t[o].exports;var r=t[o]={i:o,l:!1,exports:{}};return e[o].call(r.exports,r,r.exports,n),r.l=!0,r.exports}n.m=e,n.c=t,n.d=function(e,t,o){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:o})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(n.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var r in e)n.d(o,r,function(t){return e[t]}.bind(null,r));return o},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){"use strict";n.r(t);var o;var r="undefined"==typeof document?void 0:document,i=!!r&&"content"in r.createElement("template"),a=!!r&&r.createRange&&"createContextualFragment"in r.createRange();function l(e){return e=e.trim(),i?function(e){var t=r.createElement("template");return t.innerHTML=e,t.content.childNodes[0]}(e):a?function(e){return o||(o=r.createRange()).selectNode(r.body),o.createContextualFragment(e).childNodes[0]}(e):function(e){var t=r.createElement("body");return t.innerHTML=e,t.childNodes[0]}(e)}function c(e,t){var n,o,r=e.nodeName,i=t.nodeName;return r===i||(n=r.charCodeAt(0),o=i.charCodeAt(0),n<=90&&o>=97?r===i.toUpperCase():o<=90&&n>=97&&i===r.toUpperCase())}function u(e,t,n){e[n]!==t[n]&&(e[n]=t[n],e[n]?e.setAttribute(n,""):e.removeAttribute(n))}var d={OPTION:function(e,t){var n=e.parentNode;if(n){var o=n.nodeName.toUpperCase();"OPTGROUP"===o&&(o=(n=n.parentNode)&&n.nodeName.toUpperCase()),"SELECT"!==o||n.hasAttribute("multiple")||(e.hasAttribute("selected")&&!t.selected&&(e.setAttribute("selected","selected"),e.removeAttribute("selected")),n.selectedIndex=-1)}u(e,t,"selected")},INPUT:function(e,t){u(e,t,"checked"),u(e,t,"disabled"),e.value!==t.value&&(e.value=t.value),t.hasAttribute("value")||e.removeAttribute("value")},TEXTAREA:function(e,t){var n=t.value;e.value!==n&&(e.value=n);var o=e.firstChild;if(o){var r=o.nodeValue;if(r==n||!n&&r==e.placeholder)return;o.nodeValue=n}},SELECT:function(e,t){if(!t.hasAttribute("multiple")){for(var n,o,r=-1,i=0,a=e.firstChild;a;)if("OPTGROUP"===(o=a.nodeName&&a.nodeName.toUpperCase()))a=(n=a).firstChild;else{if("OPTION"===o){if(a.hasAttribute("selected")){r=i;break}i++}!(a=a.nextSibling)&&n&&(a=n.nextSibling,n=null)}e.selectedIndex=r}}};function s(){}function f(e){if(e)return e.getAttribute&&e.getAttribute("id")||e.id}var p=function(e){return function(t,n,o){if(o||(o={}),"string"==typeof n)if("#document"===t.nodeName||"HTML"===t.nodeName||"BODY"===t.nodeName){var i=n;(n=r.createElement("html")).innerHTML=i}else n=l(n);var a=o.getNodeKey||f,u=o.onBeforeNodeAdded||s,p=o.onNodeAdded||s,m=o.onBeforeElUpdated||s,h=o.onElUpdated||s,g=o.onBeforeNodeDiscarded||s,v=o.onNodeDiscarded||s,b=o.onBeforeElChildrenUpdated||s,N=!0===o.childrenOnly,y=Object.create(null),w=[];function S(e){w.push(e)}function x(e,t,n){!1!==g(e)&&(t&&t.removeChild(e),v(e),function e(t,n){if(1===t.nodeType)for(var o=t.firstChild;o;){var r=void 0;n&&(r=a(o))?S(r):(v(o),o.firstChild&&e(o,n)),o=o.nextSibling}}(e,n))}function E(e){p(e);for(var t=e.firstChild;t;){var n=t.nextSibling,o=a(t);if(o){var r=y[o];r&&c(t,r)?(t.parentNode.replaceChild(r,t),T(r,t)):E(t)}else E(t);t=n}}function T(t,n,o){var i=a(n);if(i&&delete y[i],!o){if(!1===m(t,n))return;if(e(t,n),h(t),!1===b(t,n))return}"TEXTAREA"!==t.nodeName?function(e,t){var n,o,i,l,s,f=t.firstChild,p=e.firstChild;e:for(;f;){for(l=f.nextSibling,n=a(f);p;){if(i=p.nextSibling,f.isSameNode&&f.isSameNode(p)){f=l,p=i;continue e}o=a(p);var m=p.nodeType,h=void 0;if(m===f.nodeType&&(1===m?(n?n!==o&&((s=y[n])?i===s?h=!1:(e.insertBefore(s,p),o?S(o):x(p,e,!0),p=s):h=!1):o&&(h=!1),(h=!1!==h&&c(p,f))&&T(p,f)):3!==m&&8!=m||(h=!0,p.nodeValue!==f.nodeValue&&(p.nodeValue=f.nodeValue))),h){f=l,p=i;continue e}o?S(o):x(p,e,!0),p=i}if(n&&(s=y[n])&&c(s,f))e.appendChild(s),T(s,f);else{var g=u(f);!1!==g&&(g&&(f=g),f.actualize&&(f=f.actualize(e.ownerDocument||r)),e.appendChild(f),E(f))}f=l,p=i}!function(e,t,n){for(;t;){var o=t.nextSibling;(n=a(t))?S(n):x(t,e,!0),t=o}}(e,p,o);var v=d[e.nodeName];v&&v(e,t)}(t,n):d.TEXTAREA(t,n)}!function e(t){if(1===t.nodeType||11===t.nodeType)for(var n=t.firstChild;n;){var o=a(n);o&&(y[o]=n),e(n),n=n.nextSibling}}(t);var A,C,O=t,L=O.nodeType,P=n.nodeType;if(!N)if(1===L)1===P?c(t,n)||(v(t),O=function(e,t){for(var n=e.firstChild;n;){var o=n.nextSibling;t.appendChild(n),n=o}return t}(t,(A=n.nodeName,(C=n.namespaceURI)&&"http://www.w3.org/1999/xhtml"!==C?r.createElementNS(C,A):r.createElement(A)))):O=n;else if(3===L||8===L){if(P===L)return O.nodeValue!==n.nodeValue&&(O.nodeValue=n.nodeValue),O;O=n}if(O===n)v(t);else{if(n.isSameNode&&n.isSameNode(O))return;if(T(O,n,N),w)for(var j=0,F=w.length;j<F;j++){var k=y[w[j]];k&&x(k,k.parentNode,!1)}}return!N&&O!==t&&t.parentNode&&(O.actualize&&(O=O.actualize(t.ownerDocument||r)),t.parentNode.replaceChild(O,t)),O}}((function(e,t){var n,o,r,i,a=t.attributes;if(11!==t.nodeType&&11!==e.nodeType){for(var l=a.length-1;l>=0;l--)o=(n=a[l]).name,r=n.namespaceURI,i=n.value,r?(o=n.localName||o,e.getAttributeNS(r,o)!==i&&("xmlns"===n.prefix&&(o=n.name),e.setAttributeNS(r,o,i))):e.getAttribute(o)!==i&&e.setAttribute(o,i);for(var c=e.attributes,u=c.length-1;u>=0;u--)o=(n=c[u]).name,(r=n.namespaceURI)?(o=n.localName||o,t.hasAttributeNS(r,o)||e.removeAttributeNS(r,o)):t.hasAttribute(o)||e.removeAttribute(o)}}));function m(e,t,n,o,r,i,a,l){let c={timeLoad:i,timeComplete:a,isCacheHit:l},u=function(){var e=null,t=null;"connection"in navigator&&(e=navigator.connection.effectiveType,"rtt"in navigator.connection&&(t=navigator.connection.rtt));return{connectionType:e,rtt:t}}(),d=void 0!==window.orientation||-1!==navigator.userAgent.indexOf("IEMobile");fetch("https://fasterwebio.herokuapp.com/send",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({urlBase:n,urlCurrent:o,urlTarget:r,performance:c,connection:u,isMobile:d,sessionKey:e,country:t})}).then(e=>e.json()).catch(e=>console.log("postDataAnalytics: "+e))}function h(){let e=window.location.port?":"+window.location.port:"";return window.location.protocol+"//"+window.location.hostname+e}function g(e){if(!e)return null;let t=h(),n=e.indexOf(t)>=0?e.substring(t.length):e;return"/"===n.substring(0,1)?n:"/"+n}const v=(e,t)=>new RegExp("^"+t.split("*").join(".*")+"$").test(e),b=(e,t,n,o,r,i)=>{N(t,n).then(t=>{t&&(e=function(e,t){if(!e||!t)return[];return e.filter(e=>{if(!e)return!1;let n=!0;return t.forEach(t=>{v(e,t)&&(n=!1)}),n})}(e=t.urlTargetList,r))&&function(e,t,n=!0,o=!0){t.forEach(t=>{if(function(e){if(!e)return;let t=['a[href="'+e+'"]'],n=[];0===n.length&&e.startsWith("/")&&t.push('a[href="'+e.substring(1)+'"]'),n=document.querySelectorAll(t),n.forEach(e=>e.classList.add("faster-link"))}(t),!e[t])return o&&console.log("Prefetching: "+t),fetch(t).then((function(e){if(!e.ok)throw Error(e.statusText);return e})).then(e=>e.text()).then(n=>e[t]=n).catch(e=>console.log(e))})}(o,e,!0,i)})};const N=async function(e,t){let n=fetch("https://fasterwebio.herokuapp.com/getPrefetch",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({urlBase:e,urlCurrent:t})}).then(e=>e.json()).catch(e=>console.log("getPrefetchUrlList: "+e));return await n};function y(e,t,n){"function"==typeof e&&e(t,n)}const w=(()=>{let e,t,n,o,r,i,a,l,c,u,d;const s=e=>{const t=g(window.location.href);i&&console.log("Go back to: "+t),"#"!==t[t.length-1]?(i&&console.log("Faster: BACK button pressed: reload: "+t),location.reload()):i&&console.log("Faster: No reload with # in url")},f=o=>{o=window.e||o,i&&console.log(" "),e=performance.now();const a=null==(l=o.target)?null:"A"===l.tagName?l.href:"A"===l.parentNode.tagName?l.parentNode.href:"A"===l.parentNode.parentNode.tagName?l.parentNode.parentNode.href:"a"===l.tagName?l.href.baseVal:"a"===l.parentNode.tagName?l.parentNode.href.baseVal:"a"===l.parentNode.parentNode.tagName?l.parentNode.parentNode.href.baseVal:null;var l;const c=g(window.location.href),d=g(a);if(i&&console.log("1. Click: "+c+" -> "+d),((e,t,n,o)=>{if(null===t||""===t||"#"===t||e+"#"===t)return!1;if(t.indexOf("http")>=0)return!1;if(e===t)return!1;if(navigator.connection&&navigator.connection.saveData)return!1;if(void 0!==[".txt",".doc",".docx",".pdf",".ppt",".pptx",".pps",".ppsx",".mp3",".mp4",".avi",".jpeg",".jpg",".png",".svg",".xls",".xlsx",".zip",".csv",".wav",".wma","*.zip","*.epub","*.mobi"].find(e=>t.toLowerCase().endsWith(e)))return!1;if(n.length>0){let e=!1;if(n.forEach(n=>{v(t,n)&&(e=!0)}),!1===e)return console.log("URL "+t+" is NOT in the include List"),!1}if(o.length>0){let e=!0;if(o.forEach(n=>{v(t,n)&&(e=!1)}),!1===e)return!1}return!0})(c,d,r.urlInclude,r.urlExclude)){let e=null===r.elementSelector?document.body:document.querySelector(r.elementSelector);e.classList.add("faster-hidden"),t=performance.now(),u[d]?(i&&console.log("Found prefetch for: "+d),n=performance.now(),N(u[d],c,d,!0)):async function(e,t,n=7e3){return Promise.race([fetch(e,t),new Promise((e,t)=>setTimeout(()=>t(new Error("timeout")),n))])}(d,{},7e3).then((function(e){if(n=performance.now(),!e.ok)throw Error(e.statusText);return e})).then(e=>e.text()).then(e=>N(e,c,d,!1)).catch(t=>{console.log("Error fetch: "+t),window.history.pushState("","",d),e.innerHTML="<h1>Please reload this page</h1>",location.reload()}),o.preventDefault()}};function N(s,f,h,g=!1){window.history.pushState("","",h);const v=(new DOMParser).parseFromString(s,"text/html");let N=document.body,w=v.body;document.title=v.title,null!==r.elementSelector&&(N=document.querySelector(r.elementSelector),w=v.querySelector(r.elementSelector)),r.url[h]&&r.url[h].cssfile&&function(e,t){if("js"==t)(n=document.createElement("script")).setAttribute("type","text/javascript"),n.setAttribute("src",e),n.setAttribute("id","temp_element");else if("css"==t){var n;(n=document.createElement("link")).setAttribute("rel","stylesheet"),n.setAttribute("type","text/css"),n.setAttribute("href",e),n.setAttribute("id","temp_element")}void 0!==n&&document.getElementsByTagName("head")[0].appendChild(n)}(r.url[h].cssfile,"css"),p(N,w),b(d,c,h,u,r.urlDoNotFetch,i),r.url[h]&&y(r.url[h].pageFunction,h,r.externalScriptObject),r.url.all&&y(r.url.all.pageFunction,h,r.externalScriptObject),document.body.scrollTop=0,document.documentElement.scrollTop=0,o=performance.now();let S=(n-t).toFixed(2),x=(o-e).toFixed(2);i&&console.log("Load page in "+S+" ms."),i&&console.log("Completed in "+x+" ms."),m(a,l,c,f,h,S,x,g)}return{init:e=>{if(a=function(){let e=localStorage.getItem("FasterWebSessionKey");if(e)return e;e=(new Date).getTime(),localStorage.setItem("FasterWebSessionKey",e)}(),l=function(){var e=localStorage.getItem("FasterWebSessionCountry");if(e)return e;fetch("https://ssl.geoplugin.net/json.gp?k=69954820a5c412dd").then((function(e){if(!e.ok)throw new Error("HTTP error, status = "+e.status);return e.json()})).then((function(t){e=t.geoplugin_countryName,localStorage.setItem("FasterWebSessionCountry",e)}))}(),r=function(e){return e.debug=e.debug||!1,e.urlInclude=e.urlInclude||[],e.urlExclude=e.urlExclude||[],e.urlDoNotFetch=e.urlDoNotFetch||[],e.elementSelector=e.elementSelector||null,e}(e),i=r.debug,i){const e="font-family: Roboto, serif; font-size: 25px; color: #ec7a16e0; font-weight: bold; font-style: italic",t="font-family: Roboto, serif;font-size: 16px; color: #777";console.log("%c FasterWeb.io",e),console.log("%c Making the web faster, friendlier - v0.6.2",t)}u=[],d=[],c=h();let t=g(window.location.href);((e,t)=>{let n=g(window.location.href);if(e.length>0){let o=!0;return e.forEach(e=>{v(n,e)&&(o=!1)}),!1!==o||(t&&console.log("URL "+n+" is in exclude List"),!1)}})(r.urlExclude,i)&&(b(d,c,t,u,r.urlDoNotFetch,i),document.addEventListener?document.addEventListener("click",f):document.attachEvent("onclick",f),window.addEventListener("popstate",s,!1))},terminate:()=>{document.addEventListener?document.removeEventListener("click",f,!1):document.removeEventListener("onclick",f,!1),window.removeEventListener("popstate",s,!1)}}})();t.default=w;window.faster=w}]);