epub.js icon indicating copy to clipboard operation
epub.js copied to clipboard

Moby Dick demo doesn't work with Chrome translator extension

Open anatoly314 opened this issue 9 years ago • 4 comments

Hi, I'm using this Chrome extension to translate unknown words while I'm reading: ImTranslator It works like this:

  • I double click on word or select text section and a small grey balloon appear on top of the selection

  • When I tap on it, translation appears:

But when I'm trying it on the demo book "Moby Dick" it doesn't work. When I double click or select text nothing is happening.

Can it be solved somehow? It's essential feature for me.

Thank you.

anatoly314 avatar Mar 12 '17 00:03 anatoly314

the same problem with Lingualeo chrome extension

evglevin avatar Nov 04 '18 13:11 evglevin

The same on google translate Chrome extension, does anyone know how to fix it? :cry:

m8524769 avatar Dec 08 '19 12:12 m8524769

Ok, I got a temporary solution here,

html: (where the translation appear, put it anywhere you like)

<span
  id="epubSelection"
  style="position: fixed; top: 42vh; right: calc(50vw - 530px); font-size: 0;"
>
</span>

typescript:

private clientX: number;
private clientY: number;

rendition.once('rendered', () => {
  const epubContainer = document.getElementsByClassName('epub-container')[0] as HTMLElement;
  const docElement = rendition.getContents()[0].documentElement as HTMLElement;

  // Listen to the pointer location in the rendition
  docElement.addEventListener('mouseup', event => {
    const offsetX = epubContainer.firstElementChild.getBoundingClientRect().left;  // epub-view
    const offsetY = epubContainer.scrollTop;
    this.clientX = event.clientX + offsetX;
    this.clientY = event.clientY - offsetY;
  });
});

rendition.on('selected', (cfirange, contents) => {
  // Copy the selected content from the <iframe> to an outside element
  const selection: Selection = contents.window.getSelection();
  const element: HTMLElement = document.getElementById('epubSelection');
  element.innerText = selection.toString();

  // Select the outside element
  const range: Range = document.createRange();
  range.selectNodeContents(element);
  window.getSelection().removeAllRanges();
  window.getSelection().addRange(range);

  // Trigger a mouseup event manually
  element.dispatchEvent(new MouseEvent('mouseup', {
    bubbles: true,
    cancelable: true,
    clientX: this.clientX,
    clientY: this.clientY,
  }));
});

And the result: 2019-12-09-112653_324x177_scrot

2019-12-09-001558_1182x374_scrot

Hope this helps. :smiley:

m8524769 avatar Dec 08 '19 16:12 m8524769

I tried it for a long time, it's actually very simple. in manifest.json file

{
  ...
  "content_scripts": [
    {
      "all_frames": true,//necessary
      "js": ["content.js"],
      "matches": ["\u003Call_urls>"],
      "match_about_blank": true,//necessary,add this
      "run_at": "document_idle"
    }
  ],
  ...
}

result: ss

pyqh avatar Dec 19 '23 04:12 pyqh