diff --git a/src/plugins/plugin.text_selection.js b/src/plugins/plugin.text_selection.js index 714e45df4..20b654d95 100644 --- a/src/plugins/plugin.text_selection.js +++ b/src/plugins/plugin.text_selection.js @@ -15,6 +15,8 @@ export const DEFAULT_OPTIONS = { singlePageDjvuXmlUrl: null, /** Whether to fetch the XML as a jsonp */ jsonp: false, + /** Whether the book is a protected book */ + protected: false, }; /** @typedef {typeof DEFAULT_OPTIONS} TextSelectionPluginOptions */ @@ -71,6 +73,21 @@ export class TextSelectionPlugin { } init() { + if (this.options.protected) { + // Prevent right clicking when selected text + $(document.body).on('contextmenu dragstart copy', (e) => { + const selection = document.getSelection(); + if (selection && selection.toString()) { + const intersectsTextLayer = $('.textSelectionSVG') + .toArray() + .some(el => selection.containsNode(el, true)); + if (intersectsTextLayer) { + e.preventDefault(); + return false; + } + } + }); + } // Only fetch the full djvu xml if the single page url isn't there if (this.options.singlePageDjvuXmlUrl) return; this.djvuPagesPromise = $.ajax({ @@ -126,6 +143,10 @@ export class TextSelectionPlugin { */ interceptCopy($container) { $container[0].addEventListener('copy', (event) => { + if (this.options.protected) { + event.preventDefault(); + return false; + } const selection = document.getSelection(); event.clipboardData.setData('text/plain', selection.toString()); event.preventDefault(); @@ -266,7 +287,8 @@ export class TextSelectionPlugin { export class BookreaderWithTextSelection extends BookReader { init() { - const options = Object.assign({}, DEFAULT_OPTIONS, this.options.plugins.textSelection); + const inheritedOptions = 'protected' in this.options ? { protected: this.options.protected } : {}; + const options = Object.assign({}, DEFAULT_OPTIONS, inheritedOptions, this.options.plugins.textSelection); if (options.enabled) { this.textSelectionPlugin = new TextSelectionPlugin(options, this.options.vars); // Write this back; this way the plugin is the source of truth, and BR just