UNPKG

@meilenstein-io/yett

Version:

A small library that can prevent other scripts from executing.

3 lines (2 loc) 3.78 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t=t||self).yett={})}(this,function(t){"use strict";function l(e,t){return e&&(!t||t!==s)&&(!d.blacklist||d.blacklist.some(function(t){return t.test(e)}))&&(!d.whitelist||d.whitelist.every(function(t){return!t.test(e)}))}function a(t){var e=t.getAttribute("src");return d.blacklist&&d.blacklist.every(function(t){return!t.test(e)})||d.whitelist&&d.whitelist.some(function(t){return t.test(e)})}var s="javascript/blocked",u="ccm-load-before-consent",p="ccm-explicit-block",f={AUTO:"AUTO",EXPLICIT:"EXPLICIT"},d={blacklist:window.YETT_BLACKLIST,whitelist:window.YETT_WHITELIST},b={blacklisted:[]},v={scriptBlockMode:f.AUTO,patchDocumentCreateElement:!0},y=new MutationObserver(function(t){for(var e=0;e<t.length;e++)for(var c=t[e].addedNodes,n=function(t){var e,n,r=c[t];if(v.scriptBlockMode===f.EXPLICIT?1===(n=r).nodeType&&"SCRIPT"===n.tagName&&!n.getAttribute(u)&&n.getAttribute(p):1===(e=r).nodeType&&"SCRIPT"===e.tagName&&!e.getAttribute(u)){var i=r.src,o=r.type;if(l(i,o)){b.blacklisted.push(r.cloneNode()),r.type=s;r.addEventListener("beforescriptexecute",function t(e){r.getAttribute("type")===s&&e.preventDefault(),r.removeEventListener("beforescriptexecute",t)}),r.parentElement&&r.parentElement.removeChild(r)}}},r=0;r<c.length;r++)n(r)});function m(t){return function(t){if(Array.isArray(t)){for(var e=0,n=new Array(t.length);e<t.length;e++)n[e]=t[e];return n}}(t)||function(t){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t))return Array.from(t)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}var g=new RegExp("[|\\{}()[\\]^$+*?.]","g");t.init=function(){!function(){if(v.patchDocumentCreateElement){var o=document.createElement;document.createElement=function(){for(var t=arguments.length,e=new Array(t),n=0;n<t;n++)e[n]=arguments[n];if("script"!==e[0].toLowerCase())return o.bind(document).apply(void 0,e);var r=o.bind(document).apply(void 0,e),i=r.setAttribute.bind(r);return Object.defineProperties(r,{src:{get:function(){return r.getAttribute("src")},set:function(t){return l(t,r.type)&&i("type",s),i("src",t),!0}},type:{set:function(t){var e=l(r.src,r.type)?s:t;return i("type",e),!0}}}),r.setAttribute=function(t,e){"type"===t||"src"===t?r[t]=e:HTMLScriptElement.prototype.setAttribute.call(r,t,e)},r}}}(),y.observe(document.documentElement,{childList:!0,subtree:!0})},t.options=v,t.patterns=d,t.unblock=function(){for(var t=arguments.length,n=new Array(t),e=0;e<t;e++)n[e]=arguments[e];n.length<1?(d.blacklist=[],d.whitelist=[]):(d.blacklist&&(d.blacklist=d.blacklist.filter(function(e){return n.every(function(t){return"string"==typeof t?!e.test(t):t instanceof RegExp?e.toString()!==t.toString():void 0})})),d.whitelist&&(d.whitelist=[].concat(m(d.whitelist),m(n.map(function(e){if("string"==typeof e){var n=".*"+e.replace(g,"\\$&")+".*";if(d.whitelist.every(function(t){return t.toString()!==n.toString()}))return new RegExp(n)}else if(e instanceof RegExp&&d.whitelist.every(function(t){return t.toString()!==e.toString()}))return e;return null}).filter(Boolean)))));for(var r=document.querySelectorAll('script[type="'.concat(s,'"]')),i=0;i<r.length;i++){var o=r[i];a(o)&&(o.type="application/javascript",b.blacklisted.push(o),o.parentElement.removeChild(o))}var c=0;m(b.blacklisted).forEach(function(t,e){if(a(t)){var n=document.createElement("script");n.setAttribute("src",t.src),n.setAttribute("type","application/javascript"),document.head.appendChild(n),b.blacklisted.splice(e-c,1),c++}}),d.blacklist&&d.blacklist.length<1&&y.disconnect()},Object.defineProperty(t,"__esModule",{value:!0})}); //# sourceMappingURL=yett.min.js.map