UNPKG

@jeanfredrik/yett

Version:

A small library that can prevent other scripts from executing.

3 lines (2 loc) 5.13 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 a(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){if("undefined"==typeof Symbol||!(Symbol.iterator in Object(t)))return;var r=[],n=!0,i=!1,o=void 0;try{for(var c,l=t[Symbol.iterator]();!(n=(c=l.next()).done)&&(r.push(c.value),!e||r.length!==e);n=!0);}catch(t){i=!0,o=t}finally{try{n||null==l.return||l.return()}finally{if(i)throw o}}return r}(t,e)||r(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function c(t){return function(t){if(Array.isArray(t))return n(t)}(t)||function(t){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(t))return Array.from(t)}(t)||r(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function r(t,e){if(t){if("string"==typeof t)return n(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?n(t,e):void 0}}function n(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}function s(t){var e=t.getAttribute("src");return f.blacklist&&f.blacklist.every(function(t){return!t.test(e)})||f.whitelist&&f.whitelist.some(function(t){return t.test(e)})}var i,o,u="javascript/blocked",f="undefined"!=typeof window?{blacklist:window.YETT_BLACKLIST,whitelist:window.YETT_WHITELIST}:{},p={blacklisted:[]},l=function(e,t){return e&&(!t||t!==u)&&(!f.blacklist||f.blacklist.some(function(t){return t.test(e)}))&&(!f.whitelist||f.whitelist.every(function(t){return!t.test(e)}))},d="undefined"!=typeof MutationObserver?new MutationObserver(function(t){for(var e=0;e<t.length;e++)for(var i=t[e].addedNodes,r=function(t){var r=i[t];if(1===r.nodeType&&"SCRIPT"===r.tagName){var e=r.src,n=r.type;if(l(e,n)){p.blacklisted.push([r,r.type]),r.type=u;r.addEventListener("beforescriptexecute",function t(e){r.getAttribute("type")===u&&e.preventDefault(),r.removeEventListener("beforescriptexecute",t)}),r.parentElement&&r.parentElement.removeChild(r)}}},n=0;n<i.length;n++)r(n)}):null;var y=new RegExp("[|\\{}()[\\]^$+*?.]","g");t.init=function(){var t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},e=t.blacklist,r=t.whitelist;f.blacklist=(f.blacklist||e)&&[].concat(c(f.blacklist||[]),c(e||[])),f.whitelist=(f.whitelist||r)&&[].concat(c(f.whitelist||[]),c(r||[])),d&&d.observe(document.documentElement,{childList:!0,subtree:!0}),i=document.createElement,o={src:Object.getOwnPropertyDescriptor(HTMLScriptElement.prototype,"src"),type:Object.getOwnPropertyDescriptor(HTMLScriptElement.prototype,"type")},document.createElement=function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];if("script"!==e[0].toLowerCase())return i.bind(document).apply(void 0,e);var n=i.bind(document).apply(void 0,e);try{Object.defineProperties(n,{src:{get:function(){return o.src.get.call(this)},set:function(t){l(t,n.type)&&o.type.set.call(this,u),o.src.set.call(this,t)}},type:{set:function(t){var e=l(n.src,n.type)?u:t;o.type.set.call(this,e)}}}),n.setAttribute=function(t,e){"type"===t||"src"===t?n[t]=e:HTMLScriptElement.prototype.setAttribute.call(n,t,e)}}catch(t){console.warn("Yett: unable to prevent script execution for script src ",n.src,".\n",'A likely cause would be because you are using a third-party browser extension that monkey patches the "document.createElement" function.')}return n}},t.unblock=function(){for(var t=arguments.length,r=new Array(t),e=0;e<t;e++)r[e]=arguments[e];r.length<1?(f.blacklist=[],f.whitelist=[]):(f.blacklist&&(f.blacklist=f.blacklist.filter(function(e){return r.every(function(t){return"string"==typeof t?!e.test(t):t instanceof RegExp?e.toString()!==t.toString():void 0})})),f.whitelist&&(f.whitelist=[].concat(c(f.whitelist),c(r.map(function(e){if("string"==typeof e){var r=".*"+e.replace(y,"\\$&")+".*";if(f.whitelist.every(function(t){return t.toString()!==r.toString()}))return new RegExp(r)}else if(e instanceof RegExp&&f.whitelist.every(function(t){return t.toString()!==e.toString()}))return e;return null}).filter(Boolean)))));for(var n=document.querySelectorAll('script[type="'.concat(u,'"]')),i=0;i<n.length;i++){var o=n[i];s(o)&&(p.blacklisted.push([o,"application/javascript"]),o.parentElement.removeChild(o))}var l=0;c(p.blacklisted).forEach(function(t,e){var r=a(t,2),n=r[0],i=r[1];if(s(n)){var o=document.createElement("script");for(var c in o.setAttribute("src",n.src),o.setAttribute("type",i||"application/javascript"),n)c.startsWith("on")&&(o[c]=n[c]);document.head.appendChild(o),p.blacklisted.splice(e-l,1),l++}}),f.blacklist&&f.blacklist.length<1&&d.disconnect()},Object.defineProperty(t,"__esModule",{value:!0})}); //# sourceMappingURL=yett.min.js.map