copylockr
Version:
Librería para proteger el contenido de tu sitio web contra copias no autorizadas.
3 lines (2 loc) • 3.83 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).CopyLockr=t()}(this,(function(){"use strict";var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function t(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var n={exports:{}};return function(t){var n,s;n="undefined"!=typeof self?self:e,s=function(){return{enabled:!1,version:"2.0.0",settings:{disableContextMenu:!0,disableCopy:!0,disableCut:!0,disableDrag:!0,disableSelect:!0,disableDevTools:!0,password:null,customStyles:!0,debug:!1,onBlock:null},_styleElement:null,_observer:null,_handleEvent:function(e){return this.settings.onBlock&&this.settings.onBlock(e.type,e),this.settings.debug&&console.log("Blocked event:",e.type),e.preventDefault(),e.stopPropagation(),!1},_handleKeydown:function(e){var t=e.ctrlKey||e.metaKey;[t&&85===e.keyCode,123===e.keyCode,t&&e.shiftKey&&(73===e.keyCode||74===e.keyCode||67===e.keyCode)].some(Boolean)&&this._handleEvent(e)},_applyStyles:function(){this.settings.disableSelect&&this.settings.customStyles&&(this._styleElement=document.createElement("style"),this._styleElement.textContent="\n body * {\n -webkit-user-select: none !important;\n -moz-user-select: none !important;\n -ms-user-select: none !important;\n user-select: none !important;\n }\n img, [data-copylockr-allow] {\n -webkit-user-drag: none !important;\n -moz-user-drag: none !important;\n -ms-user-drag: none !important;\n user-drag: none !important;\n }\n ",document.head.appendChild(this._styleElement))},_observeDOM:function(){this.settings.disableSelect&&(this._observer=new MutationObserver((function(e){e.forEach((function(e){Array.from(e.addedNodes).forEach((function(e){1===e.nodeType&&(e.style.userSelect="none")}))}))})),this._observer.observe(document.body,{childList:!0,subtree:!0}))},_attachListeners:function(){var e={contextmenu:this.settings.disableContextMenu,copy:this.settings.disableCopy,cut:this.settings.disableCut,dragstart:this.settings.disableDrag};Object.entries(e).forEach((([e,t])=>{t&&document.addEventListener(e,this._handleEvent.bind(this),!0)})),this.settings.disableDevTools&&document.addEventListener("keydown",this._handleKeydown.bind(this),!0)},_detachListeners:function(){document.removeEventListener("contextmenu",this._handleEvent,!0),document.removeEventListener("copy",this._handleEvent,!0),document.removeEventListener("cut",this._handleEvent,!0),document.removeEventListener("dragstart",this._handleEvent,!0),document.removeEventListener("keydown",this._handleKeydown,!0),this._styleElement&&(document.head.removeChild(this._styleElement),this._styleElement=null),this._observer&&(this._observer.disconnect(),this._observer=null)},_validatePassword:function(e){if(this.settings.password&&this.settings.password!==e)throw new Error("Invalid password");return!0},enable:function(e){Object.assign(this.settings,e),this.enabled&&this.disable(),this._applyStyles(),this._observeDOM(),this._attachListeners(),this.enabled=!0},disable:function(e){try{return this._validatePassword(e),this._detachListeners(),this.enabled=!1,!0}catch(e){return this.settings.debug&&console.error(e.message),!1}},toggle:function(e,t){return"boolean"==typeof e?e?this.enable():this.disable(t):this[this.enabled?"disable":"enable"](t)},updateSettings:function(e){Object.assign(this.settings,e),this.enabled&&(this.disable(),this.enable())}}},t.exports?t.exports=s():n.CopyLockr=s()}(n),t(n.exports)}));
//# sourceMappingURL=copylockr.min.js.map