@readium/navigator 2.5.2-beta.1 → 2.5.2-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -3299,13 +3299,17 @@ class ln {
3299
3299
  * Checks if the given keyboard event matches any of the provided key combinations
3300
3300
  */
3301
3301
  match(t, e) {
3302
- for (const i of e) {
3303
- const n = t.keyCode === i.keyCode, s = i.ctrl === void 0 || t.ctrlKey === i.ctrl, o = i.shift === void 0 || t.shiftKey === i.shift, a = i.alt === void 0 || t.altKey === i.alt, l = i.meta === void 0 || t.metaKey === i.meta;
3304
- if (n && s && o && a && l)
3302
+ for (const i of e)
3303
+ if (this.matchesCombo(t, i))
3305
3304
  return !0;
3306
- }
3307
3305
  return !1;
3308
3306
  }
3307
+ matchesCombo(t, e) {
3308
+ return t.keyCode === e.keyCode && this.matchesModifier(t.ctrlKey, e.ctrl) && this.matchesModifier(t.shiftKey, e.shift) && this.matchesModifier(t.altKey, e.alt) && this.matchesModifier(t.metaKey, e.meta);
3309
+ }
3310
+ matchesModifier(t, e) {
3311
+ return e === void 0 ? !t : t === e;
3312
+ }
3309
3313
  /**
3310
3314
  * Creates an event handler that will call the provided handler when any of the key combinations match
3311
3315
  */
@@ -3371,7 +3375,7 @@ class ln {
3371
3375
  if (this.match(o, [a])) {
3372
3376
  const l = a.suppressOnInteractiveElement;
3373
3377
  if (l) {
3374
- const h = document.activeElement;
3378
+ const h = (n?.document ?? document).activeElement;
3375
3379
  if (Array.isArray(l) ? l.some((c) => h?.matches(c)) : rn(h)) return;
3376
3380
  }
3377
3381
  o.preventDefault(), o.stopPropagation(), a.handler(o);
@@ -3656,7 +3660,7 @@ const At = class At extends Et {
3656
3660
  ), Reflect.defineProperty(t.navigator, "epubReadingSystem", {
3657
3661
  value: {
3658
3662
  name: "readium-ts-toolkit",
3659
- version: "2.5.2-beta.1",
3663
+ version: "2.5.2-beta.3",
3660
3664
  hasFeature: (n, s = "") => {
3661
3665
  switch (n) {
3662
3666
  case "dom-manipulation":
@@ -55,7 +55,7 @@
55
55
  body::-webkit-scrollbar {
56
56
  display: none; /* for Chrome, Safari, and Opera */
57
57
  }
58
- `,e.document.head.appendChild(n),this.resizeObserver=new ResizeObserver(()=>{this.resizeDebounce&&this.wnd.clearTimeout(this.resizeDebounce),this.isResizing=!0,this.resizeDebounce=this.wnd.setTimeout(()=>{this.isResizing=!1,this.resizeDebounce=null,this.reportProgress()},50)}),this.resizeObserver.observe(e.document.body),e.addEventListener("scroll",this.handleScroll,{passive:!0}),t.register("force_webkit_recalc",W.moduleName,()=>{Ut(this.wnd);const i=this.doc().scrollTop;i>1?this.doc().scrollTop=i-1:this.doc().scrollTop=i+1,this.doc().scrollTop=i}),t.register("go_progression",W.moduleName,(i,o)=>{const a=i;if(a<0||a>1){t.send("error",{message:"go_progression must be given a position from 0.0 to 1.0"}),o(!1);return}this.wnd.requestAnimationFrame(()=>{this.doc().scrollTop=this.doc().offsetHeight*a,this.reportProgress(),z(this.wnd),o(!0)})}),t.register("go_id",W.moduleName,(i,o)=>{const a=e.document.getElementById(i);if(!a){o(!1);return}this.wnd.requestAnimationFrame(()=>{this.doc().scrollTop=a.getBoundingClientRect().top+e.scrollY-e.innerHeight/2,this.reportProgress(),z(this.wnd),o(!0)})}),t.register("go_text",W.moduleName,(i,o)=>{let a;Array.isArray(i)&&(i.length>1&&(a=i[1]),i=i[0]);const s=oe.deserialize(i),l=Oe(this.wnd.document,new F({href:e.location.href,type:"text/html",text:s,locations:a?new k({otherLocations:new Map([["cssSelector",a]])}):void 0}));if(!l){o(!1);return}this.wnd.requestAnimationFrame(()=>{this.doc().scrollTop=l.getBoundingClientRect().top+e.scrollY-e.innerHeight/2,this.reportProgress(),z(this.wnd),o(!0)})}),t.register("go_start",W.moduleName,(i,o)=>{if(this.doc().scrollTop===0)return o(!1);this.doc().scrollTop=0,this.reportProgress(),o(!0)}),t.register("go_end",W.moduleName,(i,o)=>{if(this.doc().scrollTop===this.doc().scrollHeight-this.doc().offsetHeight)return o(!1);this.doc().scrollTop=this.doc().scrollHeight-this.doc().offsetHeight,this.reportProgress(),o(!0)}),t.register("unfocus",W.moduleName,(i,o)=>{z(this.wnd),o(!0)}),t.register("scroll_protection",W.moduleName,(i,o)=>{this.enableScrollProtection(),o(!0)}),t.register(["go_next","go_prev"],W.moduleName,(i,o)=>o(!1)),t.register("focus",W.moduleName,(i,o)=>{this.reportProgress(),o(!0)}),t.register("first_visible_locator",W.moduleName,(i,o)=>{const a=lt(e,!0);this.comms.send("first_visible_locator",a.serialize()),o(!0)}),t.log("ScrollSnapper Mounted"),!0}unmount(e,t){return t.unregisterAll(W.moduleName),this.resizeObserver.disconnect(),this.handleScroll&&e.removeEventListener("scroll",this.handleScroll),e.document.getElementById(ei)?.remove(),this.patternAnalyzer&&(this.patternAnalyzer.clear(),this.patternAnalyzer=null,this.isScrollProtectionEnabled=!1),t.log("ScrollSnapper Unmounted"),!0}};W.moduleName="scroll_snapper";let Nt=W;const B=class B extends be{constructor(){super(...arguments),this.patternAnalyzer=null,this.lastScrollTime=0,this.isScrollProtectionEnabled=!1,this.initialScrollHandled=!1,this.isScrolling=!1,this.lastScrollTop=0,this.isResizing=!1,this.resizeDebounce=null,this.handleScroll=e=>{if(this.comms.ready&&!this.isResizing){if(!this.initialScrollHandled){this.lastScrollTop=this.doc().scrollTop,this.initialScrollHandled=!0,this.reportProgress();return}this.isScrolling||(this.isScrolling=!0,this.wnd.requestAnimationFrame(()=>{this.reportProgress();const t=this.doc().scrollTop,n=t-this.lastScrollTop;if(this.lastScrollTop=t,this.isScrollProtectionEnabled&&Math.abs(n)>5){const i=Date.now(),o=i-(this.lastScrollTime||i);if(this.patternAnalyzer&&this.patternAnalyzer.analyze(n>0?"down":"up",Math.abs(n),o)){const s=e.target&&"tagName"in e.target?{tagName:e.target.tagName}:null;this.comms?.send("content_protection",{type:"suspicious_scrolling",timestamp:Date.now(),scrollDelta:n,scrollDirection:n>0?"down":"up",targetElement:s})}this.lastScrollTime=i}this.comms.send("scroll",n),this.isScrolling=!1}))}}}doc(){return this.wnd.document.scrollingElement}reportProgress(){if(!this.comms.ready)return;const e=Math.ceil(this.doc().scrollTop),t=this.doc().scrollHeight,n=this.wnd.innerHeight,i=Math.max(0,Math.min(1,e/t)),o=Math.max(0,Math.min(1,(e+n)/t));this.comms.send("progress",{start:i,end:o})}enableScrollProtection(){this.patternAnalyzer||(this.patternAnalyzer=new ct(Mt),this.isScrollProtectionEnabled=!0,this.comms?.log("Scroll protection enabled"))}mount(e,t){return this.wnd=e,this.comms=t,this.initialScrollHandled=!1,this.lastScrollTop=0,this.isResizing=!1,this.resizeDebounce&&(this.wnd.clearTimeout(this.resizeDebounce),this.resizeDebounce=null),this.resizeObserver=new ResizeObserver(()=>{this.resizeDebounce&&this.wnd.clearTimeout(this.resizeDebounce),this.isResizing=!0,this.resizeDebounce=this.wnd.setTimeout(()=>{this.isResizing=!1,this.resizeDebounce=null,this.reportProgress()},50)}),this.resizeObserver.observe(e.document.body),e.addEventListener("scroll",this.handleScroll,{passive:!0}),t.register("force_webkit_recalc",B.moduleName,()=>{Ut(this.wnd);const n=this.doc().scrollTop;n>1?this.doc().scrollTop=n-1:this.doc().scrollTop=n+1,this.doc().scrollTop=n}),t.register("go_progression",B.moduleName,(n,i)=>{const o=n;if(o<0||o>1){t.send("error",{message:"go_progression must be given a position from 0.0 to 1.0"}),i(!1);return}this.wnd.requestAnimationFrame(()=>{this.doc().scrollTop=this.doc().offsetHeight*o,this.reportProgress(),z(this.wnd),i(!0)})}),t.register("go_id",B.moduleName,(n,i)=>{const o=e.document.getElementById(n);if(!o){i(!1);return}this.wnd.requestAnimationFrame(()=>{this.doc().scrollTop=o.getBoundingClientRect().top+e.scrollY-e.innerHeight/2,this.reportProgress(),z(this.wnd),i(!0)})}),t.register("go_text",B.moduleName,(n,i)=>{let o;Array.isArray(n)&&(n.length>1&&(o=n[1]),n=n[0]);const a=oe.deserialize(n),s=Oe(this.wnd.document,new F({href:e.location.href,type:"text/html",text:a,locations:o?new k({otherLocations:new Map([["cssSelector",o]])}):void 0}));if(!s){i(!1);return}this.wnd.requestAnimationFrame(()=>{this.doc().scrollTop=s.getBoundingClientRect().top+e.scrollY-e.innerHeight/2,this.reportProgress(),z(this.wnd),i(!0)})}),t.register("go_start",B.moduleName,(n,i)=>{if(this.doc().scrollTop===0)return i(!1);this.doc().scrollTop=0,this.reportProgress(),i(!0)}),t.register("go_end",B.moduleName,(n,i)=>{if(this.doc().scrollTop===this.doc().scrollHeight-this.doc().offsetHeight)return i(!1);this.doc().scrollTop=this.doc().scrollHeight-this.doc().offsetHeight,this.reportProgress(),i(!0)}),t.register("unfocus",B.moduleName,(n,i)=>{z(this.wnd),i(!0)}),t.register("scroll_protection",B.moduleName,(n,i)=>{this.enableScrollProtection(),i(!0)}),t.register(["go_next","go_prev"],B.moduleName,(n,i)=>i(!1)),t.register("focus",B.moduleName,(n,i)=>{this.reportProgress(),i(!0)}),t.register("first_visible_locator",B.moduleName,(n,i)=>{const o=lt(e,!0);t.send("first_visible_locator",o.serialize()),i(!0)}),t.log("WebPubSnapper Mounted"),!0}unmount(e,t){return t.unregisterAll(B.moduleName),this.resizeObserver.disconnect(),this.handleScroll&&e.removeEventListener("scroll",this.handleScroll),this.patternAnalyzer&&(this.patternAnalyzer.clear(),this.patternAnalyzer=null,this.isScrollProtectionEnabled=!1),t.log("WebPubSnapper Unmounted"),!0}};B.moduleName="webpub_snapper";let It=B;class Do{constructor(e,t){this.window=e,this.copyHistory=[],this.lastSelectionLength=0,this.lastSelectionTime=0,this.options=t}cleanupOldHistory(e){this.copyHistory=this.copyHistory.filter(n=>e-n.timestamp<1e4),this.copyHistory.length>this.options.historySize&&(this.copyHistory=this.copyHistory.slice(-this.options.historySize))}isSuspiciousPattern(e){return this.copyHistory.length<3?!1:this.copyHistory.filter(i=>e-i.timestamp<2e3).length>=3?!0:this.copyHistory.slice().sort((i,o)=>i.timestamp-o.timestamp).every((i,o,a)=>o===0?!0:i.length>a[o-1].length*1.5)}shouldAllowCopy(e){if(!this.options.enabled)return!0;const t=this.window.getSelection();if(!t)return!0;const i=t.toString().length,o=this.window.document.body.innerText.length,a=Date.now();if(this.cleanupOldHistory(a),i<this.options.minThreshold)return this.copyHistory.push({timestamp:a,length:i,wasBlocked:!1}),!0;const l=a-this.lastSelectionTime<100&&i>this.lastSelectionLength*1.5,c=Math.min(o*this.options.maxSelectionPercent,this.options.absoluteMaxChars),h=this.isSuspiciousPattern(a),u=i>c||l||h;return this.copyHistory.push({timestamp:a,length:i,wasBlocked:u}),u?(e?.preventDefault(),!1):(this.lastSelectionLength=i,this.lastSelectionTime=a,!0)}destroy(){this.lastSelectionLength=0,this.lastSelectionTime=0,this.copyHistory=[],this.options.enabled=!1}}class Ho{constructor(e=Jn){this.options=e,this.events=[],this.selectionStartTime=0,this.lastSelectionTime=0,this.lastSelectionPosition=0,this.selectionPatterns=[],this.lastSelectedText=""}analyze(e){if(!e)return this.clear(),!1;const t=e.toString();if(t.length===0)return this.clear(),!1;if(e.type!=="Range"||!e.rangeCount)return!1;const n=Date.now();if(t.length<=50||t===this.lastSelectedText)return!1;if(this.selectionStartTime===0)return this.selectionStartTime=n,this.lastSelectedText=t,!1;if(n-this.selectionStartTime<500)return!1;n-this.lastSelectionTime>1e3&&(this.lastSelectionTime=n),this.selectionStartTime===0&&(this.selectionStartTime=n),this.lastSelectedText=t;const o=this.analyzeSelectionPattern(e,n);return this.cleanup(n),o}analyzeSelectionPattern(e,t){if(!e.rangeCount)return!1;const n=e.getRangeAt(0),i=n.toString(),o=(t-this.selectionStartTime)/1e3;if(i.length/Math.max(1,o)>this.options.maxSelectionsPerSecond)return!0;const s=n.startOffset,l=Math.abs(s-this.lastSelectionPosition);return this.selectionPatterns.push(l),this.selectionPatterns.length>this.options.historySize&&(this.selectionPatterns.shift(),this.calculateVariance(this.selectionPatterns)<this.options.minVariance)?!0:(this.lastSelectionPosition=s,!1)}calculateVariance(e){if(e.length===0)return 0;const t=e.reduce((n,i)=>n+i,0)/e.length;return e.reduce((n,i)=>n+Math.pow(i-t,2),0)/e.length}cleanup(e){this.events=this.events.filter(t=>e-t.timestamp<=1e3)}clear(){this.events=[],this.selectionStartTime=0,this.lastSelectionTime=0,this.lastSelectionPosition=0,this.selectionPatterns=[],this.lastSelectedText=""}}class ti{match(e,t){for(const n of t){const i=e.keyCode===n.keyCode,o=n.ctrl===void 0||e.ctrlKey===n.ctrl,a=n.shift===void 0||e.shiftKey===n.shift,s=n.alt===void 0||e.altKey===n.alt,l=n.meta===void 0||e.metaKey===n.meta;if(i&&o&&a&&s&&l)return!0}return!1}createKeyHandler(e,t){return n=>{this.match(n,e)&&(n.preventDefault(),n.stopPropagation(),t(n))}}createActivityEvent(e,t,n,i){let o,a;if(i){const s=i.getSelection(),l=s?.toString()||"",h=(l&&s?.rangeCount?s.getRangeAt(0)?.getClientRects():null)?.[0];h&&l&&(o={text:l,x:h.x,y:h.y,width:h.width,height:h.height});const u=i.document.activeElement;u&&u!==i.document.body&&(a=Tt(u)?.outerHTML)}return{type:t,timestamp:Date.now(),key:e.key,code:e.code,keyCode:e.keyCode,ctrlKey:e.ctrlKey,altKey:e.altKey,shiftKey:e.shiftKey,metaKey:e.metaKey,targetFrameSrc:n,selectedText:o,interactiveElement:a}}createKeyboardHandlers(e,t,n,i){const o=[];return t.forEach(a=>{o.push(...a.keyCombos.map(s=>({...s,handler:l=>{const c=a.type,h=this.createActivityEvent(l,c,e,i);n(h)}})))}),o}createUnifiedHandler(e,t,n,i){const o=this.createKeyboardHandlers(e,t,n,i);return a=>{for(const s of o)if(this.match(a,[s])){const l=s.suppressOnInteractiveElement;if(l){const c=document.activeElement;if(Array.isArray(l)?l.some(h=>c?.matches(h)):Kn(c))return}a.preventDefault(),a.stopPropagation(),s.handler(a);return}}}}const Ce=class Ce extends fe{constructor(){super(...arguments),this.configApplied=!1,this.cleanupCallbacks=[],this.pointerMoved=!1,this.isContextMenuEnabled=!1,this.isDragAndDropEnabled=!1,this.isSelectionMonitoringEnabled=!1,this.isBulkCopyProtectionEnabled=!1,this.selectionAnalyzer=null,this.currentSelection=null,this.bulkCopyProtector=null,this.keyManager=new ti,this.keyDownHandler=null,this.preventBulkCopy=e=>{if(!this.isBulkCopyProtectionEnabled||!this.bulkCopyProtector)return!0;if(!this.bulkCopyProtector.shouldAllowCopy(e)){e.preventDefault();const t=this.wnd.getSelection(),n=t?.toString()||"",o=(n?t?.getRangeAt(0)?.getClientRects():null)?.[0],a={type:"bulk_copy",timestamp:Date.now(),clipboardTypes:e.clipboardData?.types?[...e.clipboardData.types]:[],selectedText:o?{text:n,x:o.x,y:o.y,width:o.width,height:o.height}:void 0,selectionLength:n.length,targetFrameSrc:this.wnd.location.href};return this.comms?.send("content_protection",a),!1}return!0},this.handleSelection=e=>{if(!this.isSelectionMonitoringEnabled||!this.wnd||!this.selectionAnalyzer)return;const t=this.wnd.getSelection();if(t){if(this.currentSelection=t.toString(),this.selectionAnalyzer.analyze(t)&&this.currentSelection){const i=this.wnd.getSelection(),o=i?.toString()||"",s=(o&&i?.rangeCount?i.getRangeAt(0)?.getClientRects():null)?.[0],l={type:"suspicious_selection",timestamp:Date.now(),selectionLength:o.length,selectedText:{text:o,x:s?.x??0,y:s?.y??0,width:s?.width??0,height:s?.height??0},eventType:e?.type||"selectionchange",targetFrameSrc:this.wnd.location.href};this.comms?.send("content_protection",l)}}else this.currentSelection=null},this.onDragOver=e=>{this.isDragAndDropEnabled&&(e.preventDefault(),e.stopPropagation())},this.onDragStart=e=>{if(this.isDragAndDropEnabled){e.preventDefault();const t={type:"drag_detected",timestamp:Date.now(),dataTransferTypes:e.dataTransfer?.types?[...e.dataTransfer.types]:[],targetFrameSrc:this.wnd.location.href};return this.comms?.send("content_protection",t),!1}else return!0},this.onDrop=e=>{if(this.isDragAndDropEnabled){e.preventDefault();const t=e.dataTransfer,n={type:"drop_detected",timestamp:Date.now(),dataTransferTypes:t?.types?[...t.types]:[],fileCount:t?.files?.length||0,targetFrameSrc:this.wnd.location.href};return this.comms?.send("content_protection",n),!1}else return!0},this.onContext=e=>{if(this.isContextMenuEnabled){e.preventDefault();const t=this.wnd.getSelection(),n=t?.toString()||"",o=(n&&t?.rangeCount?t.getRangeAt(0)?.getClientRects():null)?.[0],a={timestamp:Date.now(),clientX:e.clientX,clientY:e.clientY,...o&&{selectedText:{text:n,x:o.x,y:o.y,width:o.width,height:o.height}},targetFrameSrc:this.wnd.location.href};this.comms?.send("context_menu",a)}},this.onPointerUp=this.onPointUp.bind(this),this.onPointerMove=this.onPointMove.bind(this),this.onPointerDown=this.onPointDown.bind(this),this.onClicker=this.onClick.bind(this)}addContextMenuPrevention(){this.isContextMenuEnabled||!this.wnd||(this.wnd.document.addEventListener("contextmenu",this.onContext),this.isContextMenuEnabled=!0)}removeContextMenuPrevention(){!this.isContextMenuEnabled||!this.wnd||(this.wnd.document.removeEventListener("contextmenu",this.onContext),this.isContextMenuEnabled=!1)}addDragAndDropPrevention(){this.isDragAndDropEnabled||!this.wnd||(this.wnd.document.addEventListener("dragstart",this.onDragStart),this.wnd.document.addEventListener("dragover",this.onDragOver),this.wnd.document.addEventListener("drop",this.onDrop),this.isDragAndDropEnabled=!0)}removeDragAndDropPrevention(){!this.isDragAndDropEnabled||!this.wnd||(this.wnd.document.removeEventListener("dragstart",this.onDragStart),this.wnd.document.removeEventListener("dragover",this.onDragOver),this.wnd.document.removeEventListener("drop",this.onDrop),this.isDragAndDropEnabled=!1)}enableKeyboardPeripherals(e=[]){this.disableKeyboardPeripherals();const t=n=>{this.comms?.send("keyboard_peripherals",n)};this.keyDownHandler=this.keyManager.createUnifiedHandler(this.wnd.location.href,e,t,this.wnd),this.wnd&&this.wnd.document.addEventListener("keydown",this.keyDownHandler,{capture:!0})}disableKeyboardPeripherals(){this.wnd&&this.keyDownHandler&&(this.wnd.document.removeEventListener("keydown",this.keyDownHandler,{capture:!0}),this.keyDownHandler=null)}addBulkCopyProtection(e={}){if(this.isBulkCopyProtectionEnabled||!this.wnd)return;const t=No,n=e?{...t,...e}:t;this.bulkCopyProtector=new Do(this.wnd,n),this.wnd.document.addEventListener("copy",this.preventBulkCopy,!0),this.wnd.document.addEventListener("cut",this.preventBulkCopy,!0),this.isBulkCopyProtectionEnabled=!0}removeBulkCopyProtection(){!this.isBulkCopyProtectionEnabled||!this.wnd||(this.wnd.document.removeEventListener("copy",this.preventBulkCopy,!0),this.wnd.document.removeEventListener("cut",this.preventBulkCopy,!0),this.bulkCopyProtector?.destroy(),this.bulkCopyProtector=null,this.isBulkCopyProtectionEnabled=!1)}addSelectionMonitoring(e){if(this.isSelectionMonitoringEnabled||!this.wnd)return;const t=e||Jn;this.selectionAnalyzer=new Ho(t),this.wnd.document.addEventListener("selectionchange",this.handleSelection),this.isSelectionMonitoringEnabled=!0}removeSelectionMonitoring(){!this.isSelectionMonitoringEnabled||!this.wnd||(this.wnd.document.removeEventListener("selectionchange",this.handleSelection),this.selectionAnalyzer?.clear(),this.selectionAnalyzer=null,this.isSelectionMonitoringEnabled=!1)}onPointUp(e){const t=this.wnd.getSelection();if(t&&t.toString()?.length>0){const i=t.getRangeAt(0)?.getClientRects();if(!i||i.length===0)return;const o=i[0],a={text:t.toString(),x:o.x,y:o.y,width:o.width,height:o.height,targetFrameSrc:this.wnd?.location?.href};this.comms.send("text_selected",a)}if(this.pointerMoved){this.pointerMoved=!1;return}if(!t?.isCollapsed||!e.isPrimary)return;const n=this.wnd.devicePixelRatio;e.preventDefault(),this.comms.send(e.pointerType==="touch"?"tap":"click",{defaultPrevented:e.defaultPrevented,x:e.clientX*n,y:e.clientY*n,targetFrameSrc:this.wnd.location.href,targetElement:e.target.outerHTML,interactiveElement:Tt(e.target)?.outerHTML,cssSelector:this.wnd._readium_cssSelectorGenerator.getCssSelector(e.target)}),this.pointerMoved=!1}onPointMove(e){if(e.movementY!==void 0&&e.movementX!==void 0){(Math.abs(e.movementX)>1||Math.abs(e.movementY)>1)&&(this.pointerMoved=!0);return}this.pointerMoved=!0}onPointDown(){this.pointerMoved=!1}onClick(e){if(e.preventDefault(),!e.isTrusted){const t=new PointerEvent("pointerup",{isPrimary:!0,pointerType:"mouse",clientX:e.clientX,clientY:e.clientY});Object.defineProperty(t,"target",{writable:!1,value:e.target}),Object.defineProperty(t,"defaultPrevented",{writable:!1,value:e.defaultPrevented}),this.onPointUp(t)}}registerProtectionHandlers(){this.comms?.register("peripherals_protection",Ce.moduleName,(e,t)=>{const n=e;if(!this.configApplied){if(this.configApplied=!0,n.monitorSelection){const i=typeof n.monitorSelection=="boolean"?void 0:n.monitorSelection;this.addSelectionMonitoring(i),this.comms?.log("Selection monitoring enabled")}typeof n.protectCopy=="object"?(this.addBulkCopyProtection({enabled:!0,...n.protectCopy}),this.comms?.log("Copy protection enabled (limited)")):n.protectCopy===!0&&(this.addBulkCopyProtection({enabled:!0,maxSelectionPercent:0,minThreshold:0,absoluteMaxChars:0}),this.comms?.log("Copy protection enabled")),n.disableContextMenu&&(this.addContextMenuPrevention(),this.comms?.log("Context menu protection enabled")),n.disableDragAndDrop&&(this.addDragAndDropPrevention(),this.comms?.log("Drag and drop protection enabled"))}t(!0)}),this.comms?.register("keyboard_peripherals",Ce.moduleName,(e,t)=>{const n=e;n&&n.length>0&&(this.enableKeyboardPeripherals(n),this.comms?.log(`Keyboard peripherals enabled: ${n.map(i=>i.type).join(", ")}`)),t(!0)})}mount(e,t){return this.wnd=e,this.comms=t,this.registerProtectionHandlers(),e.document.addEventListener("pointerdown",this.onPointerDown),e.document.addEventListener("pointerup",this.onPointerUp),e.document.addEventListener("pointermove",this.onPointerMove),e.document.addEventListener("click",this.onClicker),t.log("Peripherals Mounted"),!0}unmount(e,t){return this.removeBulkCopyProtection(),this.removeSelectionMonitoring(),this.removeContextMenuPrevention(),this.removeDragAndDropPrevention(),this.disableKeyboardPeripherals(),this.cleanupCallbacks.forEach(n=>n()),this.cleanupCallbacks=[],e.document.removeEventListener("pointerdown",this.onPointerDown),e.document.removeEventListener("pointerup",this.onPointerUp),e.document.removeEventListener("pointermove",this.onPointerMove),e.document.removeEventListener("click",this.onClicker),t.unregisterAll(Ce.moduleName),this.configApplied=!1,t.log("Peripherals Unmounted"),!0}};Ce.moduleName="peripherals";let Dt=Ce;const Ze=class Ze extends fe{constructor(){super(...arguments),this.mediaPlayingCount=0,this.allAnimations=new Set}wndOnErr(e){this.comms?.send("error",{message:e.message,filename:e.filename,lineno:e.lineno,colno:e.colno})}unblock(e){for(e._readium_blockEvents=!1;e._readium_blockedEvents?.length>0;){const t=e._readium_blockedEvents.shift();switch(t[0]){case 0:Reflect.apply(t[1],t[2],t[3]);break;case 1:const n=t[1],i=t[2];e.removeEventListener(n.type,e._readium_eventBlocker,!0);const o=new Event(n.type,{bubbles:n.bubbles,cancelable:n.cancelable});i?i.dispatchEvent(o):e.dispatchEvent(o);break}}}onMediaPlayEvent(){this.mediaPlayingCount++,this.comms?.send("media_play",this.mediaPlayingCount)}onMediaPauseEvent(){this.mediaPlayingCount>0&&this.mediaPlayingCount--,this.comms?.send("media_pause",this.mediaPlayingCount)}pauseAllMedia(e){const t=e.document.querySelectorAll("audio,video");for(let n=0;n<t.length;n++)t[n].pause()}mount(e,t){this.comms=t,e.addEventListener("error",this.wndOnErr,!1),Reflect.defineProperty(e.navigator,"epubReadingSystem",{value:{name:"readium-ts-toolkit",version:"2.5.2-beta.1",hasFeature:(i,o="")=>{switch(i){case"dom-manipulation":return!0;case"layout-changes":return!0;case"touch-events":return!0;case"mouse-events":return!0;case"keyboard-events":return!0;case"spine-scripting":return!0;case"embedded-web-content":return!0;default:return!1}}},writable:!1}),"getAnimations"in e.document&&e.document.getAnimations().forEach(i=>{i.cancel(),this.allAnimations.add(i)}),t.register("activate",Ze.moduleName,(i,o)=>{this.allAnimations.forEach(a=>{a.cancel(),a.play()}),o(!0)}),t.register("unfocus",Ze.moduleName,(i,o)=>{this.pauseAllMedia(e),this.allAnimations.forEach(a=>a.pause()),o(!0)});const n=e.document.querySelectorAll("audio,video");for(let i=0;i<n.length;i++){const o=n[i];o.addEventListener("play",this.onMediaPlayEvent,{passive:!0}),o.addEventListener("pause",this.onMediaPauseEvent,{passive:!0})}return t.log("Setup Mounted"),!0}unmount(e,t){return e.removeEventListener("error",this.wndOnErr),e.removeEventListener("play",this.onMediaPlayEvent),e.removeEventListener("pause",this.onMediaPauseEvent),this.allAnimations.forEach(n=>n.cancel()),this.allAnimations.clear(),t.log("Setup Unmounted"),!0}};Ze.moduleName="setup";let ht=Ze;const ni="readium-viewport",ne=class ne extends ht{onViewportWidthChanged(e){const t=e.target;Ae(t,"--RS__viewportWidth",`${t.innerWidth}px`)}mount(e,t){if(!super.mount(e,t))return!1;const n=e.document.createElement("meta");return n.dataset.readium="true",n.setAttribute("name","viewport"),n.setAttribute("id",ni),n.setAttribute("content","width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no"),e.document.head.appendChild(n),e.addEventListener("orientationchange",this.onViewportWidthChanged),e.addEventListener("resize",this.onViewportWidthChanged),this.onViewportWidthChanged({target:e}),t.register("get_properties",ne.moduleName,(i,o)=>{Et(e),o(!0)}),t.register("update_properties",ne.moduleName,(i,o)=>{i["--RS__viewportWidth"]=`${e.innerWidth}px`,Wn(e,i),o(!0)}),t.register("set_property",ne.moduleName,(i,o)=>{const a=i;Ae(e,a[0],a[1]),o(!0)}),t.register("remove_property",ne.moduleName,(i,o)=>{rt(e,i),o(!0)}),t.register("activate",ne.moduleName,(i,o)=>{this.unblock(e),o(!0)}),t.log("ReflowableSetup Mounted"),!0}unmount(e,t){return t.unregisterAll(ne.moduleName),e.document.head.querySelector(`#${ni}`)?.remove(),e.removeEventListener("orientationchange",this.onViewportWidthChanged),t.log("ReflowableSetup Unmounted"),super.unmount(e,t)}};ne.moduleName="reflowable_setup";let Ht=ne;const ii="readium-fixed-style",J=class J extends ht{mount(e,t){if(!super.mount(e,t))return!1;e.navigator.epubReadingSystem&&(e.navigator.epubReadingSystem.layoutStyle="paginated");const n=e.document.createElement("style");return n.id=ii,n.dataset.readium="true",n.textContent=`
58
+ `,e.document.head.appendChild(n),this.resizeObserver=new ResizeObserver(()=>{this.resizeDebounce&&this.wnd.clearTimeout(this.resizeDebounce),this.isResizing=!0,this.resizeDebounce=this.wnd.setTimeout(()=>{this.isResizing=!1,this.resizeDebounce=null,this.reportProgress()},50)}),this.resizeObserver.observe(e.document.body),e.addEventListener("scroll",this.handleScroll,{passive:!0}),t.register("force_webkit_recalc",W.moduleName,()=>{Ut(this.wnd);const i=this.doc().scrollTop;i>1?this.doc().scrollTop=i-1:this.doc().scrollTop=i+1,this.doc().scrollTop=i}),t.register("go_progression",W.moduleName,(i,o)=>{const a=i;if(a<0||a>1){t.send("error",{message:"go_progression must be given a position from 0.0 to 1.0"}),o(!1);return}this.wnd.requestAnimationFrame(()=>{this.doc().scrollTop=this.doc().offsetHeight*a,this.reportProgress(),z(this.wnd),o(!0)})}),t.register("go_id",W.moduleName,(i,o)=>{const a=e.document.getElementById(i);if(!a){o(!1);return}this.wnd.requestAnimationFrame(()=>{this.doc().scrollTop=a.getBoundingClientRect().top+e.scrollY-e.innerHeight/2,this.reportProgress(),z(this.wnd),o(!0)})}),t.register("go_text",W.moduleName,(i,o)=>{let a;Array.isArray(i)&&(i.length>1&&(a=i[1]),i=i[0]);const s=oe.deserialize(i),l=Oe(this.wnd.document,new F({href:e.location.href,type:"text/html",text:s,locations:a?new k({otherLocations:new Map([["cssSelector",a]])}):void 0}));if(!l){o(!1);return}this.wnd.requestAnimationFrame(()=>{this.doc().scrollTop=l.getBoundingClientRect().top+e.scrollY-e.innerHeight/2,this.reportProgress(),z(this.wnd),o(!0)})}),t.register("go_start",W.moduleName,(i,o)=>{if(this.doc().scrollTop===0)return o(!1);this.doc().scrollTop=0,this.reportProgress(),o(!0)}),t.register("go_end",W.moduleName,(i,o)=>{if(this.doc().scrollTop===this.doc().scrollHeight-this.doc().offsetHeight)return o(!1);this.doc().scrollTop=this.doc().scrollHeight-this.doc().offsetHeight,this.reportProgress(),o(!0)}),t.register("unfocus",W.moduleName,(i,o)=>{z(this.wnd),o(!0)}),t.register("scroll_protection",W.moduleName,(i,o)=>{this.enableScrollProtection(),o(!0)}),t.register(["go_next","go_prev"],W.moduleName,(i,o)=>o(!1)),t.register("focus",W.moduleName,(i,o)=>{this.reportProgress(),o(!0)}),t.register("first_visible_locator",W.moduleName,(i,o)=>{const a=lt(e,!0);this.comms.send("first_visible_locator",a.serialize()),o(!0)}),t.log("ScrollSnapper Mounted"),!0}unmount(e,t){return t.unregisterAll(W.moduleName),this.resizeObserver.disconnect(),this.handleScroll&&e.removeEventListener("scroll",this.handleScroll),e.document.getElementById(ei)?.remove(),this.patternAnalyzer&&(this.patternAnalyzer.clear(),this.patternAnalyzer=null,this.isScrollProtectionEnabled=!1),t.log("ScrollSnapper Unmounted"),!0}};W.moduleName="scroll_snapper";let Nt=W;const B=class B extends be{constructor(){super(...arguments),this.patternAnalyzer=null,this.lastScrollTime=0,this.isScrollProtectionEnabled=!1,this.initialScrollHandled=!1,this.isScrolling=!1,this.lastScrollTop=0,this.isResizing=!1,this.resizeDebounce=null,this.handleScroll=e=>{if(this.comms.ready&&!this.isResizing){if(!this.initialScrollHandled){this.lastScrollTop=this.doc().scrollTop,this.initialScrollHandled=!0,this.reportProgress();return}this.isScrolling||(this.isScrolling=!0,this.wnd.requestAnimationFrame(()=>{this.reportProgress();const t=this.doc().scrollTop,n=t-this.lastScrollTop;if(this.lastScrollTop=t,this.isScrollProtectionEnabled&&Math.abs(n)>5){const i=Date.now(),o=i-(this.lastScrollTime||i);if(this.patternAnalyzer&&this.patternAnalyzer.analyze(n>0?"down":"up",Math.abs(n),o)){const s=e.target&&"tagName"in e.target?{tagName:e.target.tagName}:null;this.comms?.send("content_protection",{type:"suspicious_scrolling",timestamp:Date.now(),scrollDelta:n,scrollDirection:n>0?"down":"up",targetElement:s})}this.lastScrollTime=i}this.comms.send("scroll",n),this.isScrolling=!1}))}}}doc(){return this.wnd.document.scrollingElement}reportProgress(){if(!this.comms.ready)return;const e=Math.ceil(this.doc().scrollTop),t=this.doc().scrollHeight,n=this.wnd.innerHeight,i=Math.max(0,Math.min(1,e/t)),o=Math.max(0,Math.min(1,(e+n)/t));this.comms.send("progress",{start:i,end:o})}enableScrollProtection(){this.patternAnalyzer||(this.patternAnalyzer=new ct(Mt),this.isScrollProtectionEnabled=!0,this.comms?.log("Scroll protection enabled"))}mount(e,t){return this.wnd=e,this.comms=t,this.initialScrollHandled=!1,this.lastScrollTop=0,this.isResizing=!1,this.resizeDebounce&&(this.wnd.clearTimeout(this.resizeDebounce),this.resizeDebounce=null),this.resizeObserver=new ResizeObserver(()=>{this.resizeDebounce&&this.wnd.clearTimeout(this.resizeDebounce),this.isResizing=!0,this.resizeDebounce=this.wnd.setTimeout(()=>{this.isResizing=!1,this.resizeDebounce=null,this.reportProgress()},50)}),this.resizeObserver.observe(e.document.body),e.addEventListener("scroll",this.handleScroll,{passive:!0}),t.register("force_webkit_recalc",B.moduleName,()=>{Ut(this.wnd);const n=this.doc().scrollTop;n>1?this.doc().scrollTop=n-1:this.doc().scrollTop=n+1,this.doc().scrollTop=n}),t.register("go_progression",B.moduleName,(n,i)=>{const o=n;if(o<0||o>1){t.send("error",{message:"go_progression must be given a position from 0.0 to 1.0"}),i(!1);return}this.wnd.requestAnimationFrame(()=>{this.doc().scrollTop=this.doc().offsetHeight*o,this.reportProgress(),z(this.wnd),i(!0)})}),t.register("go_id",B.moduleName,(n,i)=>{const o=e.document.getElementById(n);if(!o){i(!1);return}this.wnd.requestAnimationFrame(()=>{this.doc().scrollTop=o.getBoundingClientRect().top+e.scrollY-e.innerHeight/2,this.reportProgress(),z(this.wnd),i(!0)})}),t.register("go_text",B.moduleName,(n,i)=>{let o;Array.isArray(n)&&(n.length>1&&(o=n[1]),n=n[0]);const a=oe.deserialize(n),s=Oe(this.wnd.document,new F({href:e.location.href,type:"text/html",text:a,locations:o?new k({otherLocations:new Map([["cssSelector",o]])}):void 0}));if(!s){i(!1);return}this.wnd.requestAnimationFrame(()=>{this.doc().scrollTop=s.getBoundingClientRect().top+e.scrollY-e.innerHeight/2,this.reportProgress(),z(this.wnd),i(!0)})}),t.register("go_start",B.moduleName,(n,i)=>{if(this.doc().scrollTop===0)return i(!1);this.doc().scrollTop=0,this.reportProgress(),i(!0)}),t.register("go_end",B.moduleName,(n,i)=>{if(this.doc().scrollTop===this.doc().scrollHeight-this.doc().offsetHeight)return i(!1);this.doc().scrollTop=this.doc().scrollHeight-this.doc().offsetHeight,this.reportProgress(),i(!0)}),t.register("unfocus",B.moduleName,(n,i)=>{z(this.wnd),i(!0)}),t.register("scroll_protection",B.moduleName,(n,i)=>{this.enableScrollProtection(),i(!0)}),t.register(["go_next","go_prev"],B.moduleName,(n,i)=>i(!1)),t.register("focus",B.moduleName,(n,i)=>{this.reportProgress(),i(!0)}),t.register("first_visible_locator",B.moduleName,(n,i)=>{const o=lt(e,!0);t.send("first_visible_locator",o.serialize()),i(!0)}),t.log("WebPubSnapper Mounted"),!0}unmount(e,t){return t.unregisterAll(B.moduleName),this.resizeObserver.disconnect(),this.handleScroll&&e.removeEventListener("scroll",this.handleScroll),this.patternAnalyzer&&(this.patternAnalyzer.clear(),this.patternAnalyzer=null,this.isScrollProtectionEnabled=!1),t.log("WebPubSnapper Unmounted"),!0}};B.moduleName="webpub_snapper";let It=B;class Do{constructor(e,t){this.window=e,this.copyHistory=[],this.lastSelectionLength=0,this.lastSelectionTime=0,this.options=t}cleanupOldHistory(e){this.copyHistory=this.copyHistory.filter(n=>e-n.timestamp<1e4),this.copyHistory.length>this.options.historySize&&(this.copyHistory=this.copyHistory.slice(-this.options.historySize))}isSuspiciousPattern(e){return this.copyHistory.length<3?!1:this.copyHistory.filter(i=>e-i.timestamp<2e3).length>=3?!0:this.copyHistory.slice().sort((i,o)=>i.timestamp-o.timestamp).every((i,o,a)=>o===0?!0:i.length>a[o-1].length*1.5)}shouldAllowCopy(e){if(!this.options.enabled)return!0;const t=this.window.getSelection();if(!t)return!0;const i=t.toString().length,o=this.window.document.body.innerText.length,a=Date.now();if(this.cleanupOldHistory(a),i<this.options.minThreshold)return this.copyHistory.push({timestamp:a,length:i,wasBlocked:!1}),!0;const l=a-this.lastSelectionTime<100&&i>this.lastSelectionLength*1.5,c=Math.min(o*this.options.maxSelectionPercent,this.options.absoluteMaxChars),h=this.isSuspiciousPattern(a),u=i>c||l||h;return this.copyHistory.push({timestamp:a,length:i,wasBlocked:u}),u?(e?.preventDefault(),!1):(this.lastSelectionLength=i,this.lastSelectionTime=a,!0)}destroy(){this.lastSelectionLength=0,this.lastSelectionTime=0,this.copyHistory=[],this.options.enabled=!1}}class Ho{constructor(e=Jn){this.options=e,this.events=[],this.selectionStartTime=0,this.lastSelectionTime=0,this.lastSelectionPosition=0,this.selectionPatterns=[],this.lastSelectedText=""}analyze(e){if(!e)return this.clear(),!1;const t=e.toString();if(t.length===0)return this.clear(),!1;if(e.type!=="Range"||!e.rangeCount)return!1;const n=Date.now();if(t.length<=50||t===this.lastSelectedText)return!1;if(this.selectionStartTime===0)return this.selectionStartTime=n,this.lastSelectedText=t,!1;if(n-this.selectionStartTime<500)return!1;n-this.lastSelectionTime>1e3&&(this.lastSelectionTime=n),this.selectionStartTime===0&&(this.selectionStartTime=n),this.lastSelectedText=t;const o=this.analyzeSelectionPattern(e,n);return this.cleanup(n),o}analyzeSelectionPattern(e,t){if(!e.rangeCount)return!1;const n=e.getRangeAt(0),i=n.toString(),o=(t-this.selectionStartTime)/1e3;if(i.length/Math.max(1,o)>this.options.maxSelectionsPerSecond)return!0;const s=n.startOffset,l=Math.abs(s-this.lastSelectionPosition);return this.selectionPatterns.push(l),this.selectionPatterns.length>this.options.historySize&&(this.selectionPatterns.shift(),this.calculateVariance(this.selectionPatterns)<this.options.minVariance)?!0:(this.lastSelectionPosition=s,!1)}calculateVariance(e){if(e.length===0)return 0;const t=e.reduce((n,i)=>n+i,0)/e.length;return e.reduce((n,i)=>n+Math.pow(i-t,2),0)/e.length}cleanup(e){this.events=this.events.filter(t=>e-t.timestamp<=1e3)}clear(){this.events=[],this.selectionStartTime=0,this.lastSelectionTime=0,this.lastSelectionPosition=0,this.selectionPatterns=[],this.lastSelectedText=""}}class ti{match(e,t){for(const n of t)if(this.matchesCombo(e,n))return!0;return!1}matchesCombo(e,t){return e.keyCode===t.keyCode&&this.matchesModifier(e.ctrlKey,t.ctrl)&&this.matchesModifier(e.shiftKey,t.shift)&&this.matchesModifier(e.altKey,t.alt)&&this.matchesModifier(e.metaKey,t.meta)}matchesModifier(e,t){return t===void 0?!e:e===t}createKeyHandler(e,t){return n=>{this.match(n,e)&&(n.preventDefault(),n.stopPropagation(),t(n))}}createActivityEvent(e,t,n,i){let o,a;if(i){const s=i.getSelection(),l=s?.toString()||"",h=(l&&s?.rangeCount?s.getRangeAt(0)?.getClientRects():null)?.[0];h&&l&&(o={text:l,x:h.x,y:h.y,width:h.width,height:h.height});const u=i.document.activeElement;u&&u!==i.document.body&&(a=Tt(u)?.outerHTML)}return{type:t,timestamp:Date.now(),key:e.key,code:e.code,keyCode:e.keyCode,ctrlKey:e.ctrlKey,altKey:e.altKey,shiftKey:e.shiftKey,metaKey:e.metaKey,targetFrameSrc:n,selectedText:o,interactiveElement:a}}createKeyboardHandlers(e,t,n,i){const o=[];return t.forEach(a=>{o.push(...a.keyCombos.map(s=>({...s,handler:l=>{const c=a.type,h=this.createActivityEvent(l,c,e,i);n(h)}})))}),o}createUnifiedHandler(e,t,n,i){const o=this.createKeyboardHandlers(e,t,n,i);return a=>{for(const s of o)if(this.match(a,[s])){const l=s.suppressOnInteractiveElement;if(l){const c=(i?.document??document).activeElement;if(Array.isArray(l)?l.some(h=>c?.matches(h)):Kn(c))return}a.preventDefault(),a.stopPropagation(),s.handler(a);return}}}}const Ce=class Ce extends fe{constructor(){super(...arguments),this.configApplied=!1,this.cleanupCallbacks=[],this.pointerMoved=!1,this.isContextMenuEnabled=!1,this.isDragAndDropEnabled=!1,this.isSelectionMonitoringEnabled=!1,this.isBulkCopyProtectionEnabled=!1,this.selectionAnalyzer=null,this.currentSelection=null,this.bulkCopyProtector=null,this.keyManager=new ti,this.keyDownHandler=null,this.preventBulkCopy=e=>{if(!this.isBulkCopyProtectionEnabled||!this.bulkCopyProtector)return!0;if(!this.bulkCopyProtector.shouldAllowCopy(e)){e.preventDefault();const t=this.wnd.getSelection(),n=t?.toString()||"",o=(n?t?.getRangeAt(0)?.getClientRects():null)?.[0],a={type:"bulk_copy",timestamp:Date.now(),clipboardTypes:e.clipboardData?.types?[...e.clipboardData.types]:[],selectedText:o?{text:n,x:o.x,y:o.y,width:o.width,height:o.height}:void 0,selectionLength:n.length,targetFrameSrc:this.wnd.location.href};return this.comms?.send("content_protection",a),!1}return!0},this.handleSelection=e=>{if(!this.isSelectionMonitoringEnabled||!this.wnd||!this.selectionAnalyzer)return;const t=this.wnd.getSelection();if(t){if(this.currentSelection=t.toString(),this.selectionAnalyzer.analyze(t)&&this.currentSelection){const i=this.wnd.getSelection(),o=i?.toString()||"",s=(o&&i?.rangeCount?i.getRangeAt(0)?.getClientRects():null)?.[0],l={type:"suspicious_selection",timestamp:Date.now(),selectionLength:o.length,selectedText:{text:o,x:s?.x??0,y:s?.y??0,width:s?.width??0,height:s?.height??0},eventType:e?.type||"selectionchange",targetFrameSrc:this.wnd.location.href};this.comms?.send("content_protection",l)}}else this.currentSelection=null},this.onDragOver=e=>{this.isDragAndDropEnabled&&(e.preventDefault(),e.stopPropagation())},this.onDragStart=e=>{if(this.isDragAndDropEnabled){e.preventDefault();const t={type:"drag_detected",timestamp:Date.now(),dataTransferTypes:e.dataTransfer?.types?[...e.dataTransfer.types]:[],targetFrameSrc:this.wnd.location.href};return this.comms?.send("content_protection",t),!1}else return!0},this.onDrop=e=>{if(this.isDragAndDropEnabled){e.preventDefault();const t=e.dataTransfer,n={type:"drop_detected",timestamp:Date.now(),dataTransferTypes:t?.types?[...t.types]:[],fileCount:t?.files?.length||0,targetFrameSrc:this.wnd.location.href};return this.comms?.send("content_protection",n),!1}else return!0},this.onContext=e=>{if(this.isContextMenuEnabled){e.preventDefault();const t=this.wnd.getSelection(),n=t?.toString()||"",o=(n&&t?.rangeCount?t.getRangeAt(0)?.getClientRects():null)?.[0],a={timestamp:Date.now(),clientX:e.clientX,clientY:e.clientY,...o&&{selectedText:{text:n,x:o.x,y:o.y,width:o.width,height:o.height}},targetFrameSrc:this.wnd.location.href};this.comms?.send("context_menu",a)}},this.onPointerUp=this.onPointUp.bind(this),this.onPointerMove=this.onPointMove.bind(this),this.onPointerDown=this.onPointDown.bind(this),this.onClicker=this.onClick.bind(this)}addContextMenuPrevention(){this.isContextMenuEnabled||!this.wnd||(this.wnd.document.addEventListener("contextmenu",this.onContext),this.isContextMenuEnabled=!0)}removeContextMenuPrevention(){!this.isContextMenuEnabled||!this.wnd||(this.wnd.document.removeEventListener("contextmenu",this.onContext),this.isContextMenuEnabled=!1)}addDragAndDropPrevention(){this.isDragAndDropEnabled||!this.wnd||(this.wnd.document.addEventListener("dragstart",this.onDragStart),this.wnd.document.addEventListener("dragover",this.onDragOver),this.wnd.document.addEventListener("drop",this.onDrop),this.isDragAndDropEnabled=!0)}removeDragAndDropPrevention(){!this.isDragAndDropEnabled||!this.wnd||(this.wnd.document.removeEventListener("dragstart",this.onDragStart),this.wnd.document.removeEventListener("dragover",this.onDragOver),this.wnd.document.removeEventListener("drop",this.onDrop),this.isDragAndDropEnabled=!1)}enableKeyboardPeripherals(e=[]){this.disableKeyboardPeripherals();const t=n=>{this.comms?.send("keyboard_peripherals",n)};this.keyDownHandler=this.keyManager.createUnifiedHandler(this.wnd.location.href,e,t,this.wnd),this.wnd&&this.wnd.document.addEventListener("keydown",this.keyDownHandler,{capture:!0})}disableKeyboardPeripherals(){this.wnd&&this.keyDownHandler&&(this.wnd.document.removeEventListener("keydown",this.keyDownHandler,{capture:!0}),this.keyDownHandler=null)}addBulkCopyProtection(e={}){if(this.isBulkCopyProtectionEnabled||!this.wnd)return;const t=No,n=e?{...t,...e}:t;this.bulkCopyProtector=new Do(this.wnd,n),this.wnd.document.addEventListener("copy",this.preventBulkCopy,!0),this.wnd.document.addEventListener("cut",this.preventBulkCopy,!0),this.isBulkCopyProtectionEnabled=!0}removeBulkCopyProtection(){!this.isBulkCopyProtectionEnabled||!this.wnd||(this.wnd.document.removeEventListener("copy",this.preventBulkCopy,!0),this.wnd.document.removeEventListener("cut",this.preventBulkCopy,!0),this.bulkCopyProtector?.destroy(),this.bulkCopyProtector=null,this.isBulkCopyProtectionEnabled=!1)}addSelectionMonitoring(e){if(this.isSelectionMonitoringEnabled||!this.wnd)return;const t=e||Jn;this.selectionAnalyzer=new Ho(t),this.wnd.document.addEventListener("selectionchange",this.handleSelection),this.isSelectionMonitoringEnabled=!0}removeSelectionMonitoring(){!this.isSelectionMonitoringEnabled||!this.wnd||(this.wnd.document.removeEventListener("selectionchange",this.handleSelection),this.selectionAnalyzer?.clear(),this.selectionAnalyzer=null,this.isSelectionMonitoringEnabled=!1)}onPointUp(e){const t=this.wnd.getSelection();if(t&&t.toString()?.length>0){const i=t.getRangeAt(0)?.getClientRects();if(!i||i.length===0)return;const o=i[0],a={text:t.toString(),x:o.x,y:o.y,width:o.width,height:o.height,targetFrameSrc:this.wnd?.location?.href};this.comms.send("text_selected",a)}if(this.pointerMoved){this.pointerMoved=!1;return}if(!t?.isCollapsed||!e.isPrimary)return;const n=this.wnd.devicePixelRatio;e.preventDefault(),this.comms.send(e.pointerType==="touch"?"tap":"click",{defaultPrevented:e.defaultPrevented,x:e.clientX*n,y:e.clientY*n,targetFrameSrc:this.wnd.location.href,targetElement:e.target.outerHTML,interactiveElement:Tt(e.target)?.outerHTML,cssSelector:this.wnd._readium_cssSelectorGenerator.getCssSelector(e.target)}),this.pointerMoved=!1}onPointMove(e){if(e.movementY!==void 0&&e.movementX!==void 0){(Math.abs(e.movementX)>1||Math.abs(e.movementY)>1)&&(this.pointerMoved=!0);return}this.pointerMoved=!0}onPointDown(){this.pointerMoved=!1}onClick(e){if(e.preventDefault(),!e.isTrusted){const t=new PointerEvent("pointerup",{isPrimary:!0,pointerType:"mouse",clientX:e.clientX,clientY:e.clientY});Object.defineProperty(t,"target",{writable:!1,value:e.target}),Object.defineProperty(t,"defaultPrevented",{writable:!1,value:e.defaultPrevented}),this.onPointUp(t)}}registerProtectionHandlers(){this.comms?.register("peripherals_protection",Ce.moduleName,(e,t)=>{const n=e;if(!this.configApplied){if(this.configApplied=!0,n.monitorSelection){const i=typeof n.monitorSelection=="boolean"?void 0:n.monitorSelection;this.addSelectionMonitoring(i),this.comms?.log("Selection monitoring enabled")}typeof n.protectCopy=="object"?(this.addBulkCopyProtection({enabled:!0,...n.protectCopy}),this.comms?.log("Copy protection enabled (limited)")):n.protectCopy===!0&&(this.addBulkCopyProtection({enabled:!0,maxSelectionPercent:0,minThreshold:0,absoluteMaxChars:0}),this.comms?.log("Copy protection enabled")),n.disableContextMenu&&(this.addContextMenuPrevention(),this.comms?.log("Context menu protection enabled")),n.disableDragAndDrop&&(this.addDragAndDropPrevention(),this.comms?.log("Drag and drop protection enabled"))}t(!0)}),this.comms?.register("keyboard_peripherals",Ce.moduleName,(e,t)=>{const n=e;n&&n.length>0&&(this.enableKeyboardPeripherals(n),this.comms?.log(`Keyboard peripherals enabled: ${n.map(i=>i.type).join(", ")}`)),t(!0)})}mount(e,t){return this.wnd=e,this.comms=t,this.registerProtectionHandlers(),e.document.addEventListener("pointerdown",this.onPointerDown),e.document.addEventListener("pointerup",this.onPointerUp),e.document.addEventListener("pointermove",this.onPointerMove),e.document.addEventListener("click",this.onClicker),t.log("Peripherals Mounted"),!0}unmount(e,t){return this.removeBulkCopyProtection(),this.removeSelectionMonitoring(),this.removeContextMenuPrevention(),this.removeDragAndDropPrevention(),this.disableKeyboardPeripherals(),this.cleanupCallbacks.forEach(n=>n()),this.cleanupCallbacks=[],e.document.removeEventListener("pointerdown",this.onPointerDown),e.document.removeEventListener("pointerup",this.onPointerUp),e.document.removeEventListener("pointermove",this.onPointerMove),e.document.removeEventListener("click",this.onClicker),t.unregisterAll(Ce.moduleName),this.configApplied=!1,t.log("Peripherals Unmounted"),!0}};Ce.moduleName="peripherals";let Dt=Ce;const Ze=class Ze extends fe{constructor(){super(...arguments),this.mediaPlayingCount=0,this.allAnimations=new Set}wndOnErr(e){this.comms?.send("error",{message:e.message,filename:e.filename,lineno:e.lineno,colno:e.colno})}unblock(e){for(e._readium_blockEvents=!1;e._readium_blockedEvents?.length>0;){const t=e._readium_blockedEvents.shift();switch(t[0]){case 0:Reflect.apply(t[1],t[2],t[3]);break;case 1:const n=t[1],i=t[2];e.removeEventListener(n.type,e._readium_eventBlocker,!0);const o=new Event(n.type,{bubbles:n.bubbles,cancelable:n.cancelable});i?i.dispatchEvent(o):e.dispatchEvent(o);break}}}onMediaPlayEvent(){this.mediaPlayingCount++,this.comms?.send("media_play",this.mediaPlayingCount)}onMediaPauseEvent(){this.mediaPlayingCount>0&&this.mediaPlayingCount--,this.comms?.send("media_pause",this.mediaPlayingCount)}pauseAllMedia(e){const t=e.document.querySelectorAll("audio,video");for(let n=0;n<t.length;n++)t[n].pause()}mount(e,t){this.comms=t,e.addEventListener("error",this.wndOnErr,!1),Reflect.defineProperty(e.navigator,"epubReadingSystem",{value:{name:"readium-ts-toolkit",version:"2.5.2-beta.3",hasFeature:(i,o="")=>{switch(i){case"dom-manipulation":return!0;case"layout-changes":return!0;case"touch-events":return!0;case"mouse-events":return!0;case"keyboard-events":return!0;case"spine-scripting":return!0;case"embedded-web-content":return!0;default:return!1}}},writable:!1}),"getAnimations"in e.document&&e.document.getAnimations().forEach(i=>{i.cancel(),this.allAnimations.add(i)}),t.register("activate",Ze.moduleName,(i,o)=>{this.allAnimations.forEach(a=>{a.cancel(),a.play()}),o(!0)}),t.register("unfocus",Ze.moduleName,(i,o)=>{this.pauseAllMedia(e),this.allAnimations.forEach(a=>a.pause()),o(!0)});const n=e.document.querySelectorAll("audio,video");for(let i=0;i<n.length;i++){const o=n[i];o.addEventListener("play",this.onMediaPlayEvent,{passive:!0}),o.addEventListener("pause",this.onMediaPauseEvent,{passive:!0})}return t.log("Setup Mounted"),!0}unmount(e,t){return e.removeEventListener("error",this.wndOnErr),e.removeEventListener("play",this.onMediaPlayEvent),e.removeEventListener("pause",this.onMediaPauseEvent),this.allAnimations.forEach(n=>n.cancel()),this.allAnimations.clear(),t.log("Setup Unmounted"),!0}};Ze.moduleName="setup";let ht=Ze;const ni="readium-viewport",ne=class ne extends ht{onViewportWidthChanged(e){const t=e.target;Ae(t,"--RS__viewportWidth",`${t.innerWidth}px`)}mount(e,t){if(!super.mount(e,t))return!1;const n=e.document.createElement("meta");return n.dataset.readium="true",n.setAttribute("name","viewport"),n.setAttribute("id",ni),n.setAttribute("content","width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no"),e.document.head.appendChild(n),e.addEventListener("orientationchange",this.onViewportWidthChanged),e.addEventListener("resize",this.onViewportWidthChanged),this.onViewportWidthChanged({target:e}),t.register("get_properties",ne.moduleName,(i,o)=>{Et(e),o(!0)}),t.register("update_properties",ne.moduleName,(i,o)=>{i["--RS__viewportWidth"]=`${e.innerWidth}px`,Wn(e,i),o(!0)}),t.register("set_property",ne.moduleName,(i,o)=>{const a=i;Ae(e,a[0],a[1]),o(!0)}),t.register("remove_property",ne.moduleName,(i,o)=>{rt(e,i),o(!0)}),t.register("activate",ne.moduleName,(i,o)=>{this.unblock(e),o(!0)}),t.log("ReflowableSetup Mounted"),!0}unmount(e,t){return t.unregisterAll(ne.moduleName),e.document.head.querySelector(`#${ni}`)?.remove(),e.removeEventListener("orientationchange",this.onViewportWidthChanged),t.log("ReflowableSetup Unmounted"),super.unmount(e,t)}};ne.moduleName="reflowable_setup";let Ht=ne;const ii="readium-fixed-style",J=class J extends ht{mount(e,t){if(!super.mount(e,t))return!1;e.navigator.epubReadingSystem&&(e.navigator.epubReadingSystem.layoutStyle="paginated");const n=e.document.createElement("style");return n.id=ii,n.dataset.readium="true",n.textContent=`
59
59
  html, body {
60
60
  text-size-adjust: none;
61
61
  -ms-text-size-adjust: none;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@readium/navigator",
3
- "version": "2.5.2-beta.1",
3
+ "version": "2.5.2-beta.3",
4
4
  "type": "module",
5
5
  "description": "Next generation SDK for publications in Web Apps",
6
6
  "author": "readium",