Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moby Dick demo doesn't work with Chrome translator extension #571

Open
anatoly314 opened this issue Mar 12, 2017 · 4 comments
Open

Moby Dick demo doesn't work with Chrome translator extension #571

anatoly314 opened this issue Mar 12, 2017 · 4 comments

Comments

@anatoly314
Copy link

anatoly314 commented Mar 12, 2017

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.

@evglevin
Copy link

evglevin commented Nov 4, 2018

the same problem with Lingualeo chrome extension

@m8524769
Copy link
Contributor

m8524769 commented Dec 8, 2019

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

@m8524769
Copy link
Contributor

m8524769 commented Dec 8, 2019

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. 😃

@pyqh
Copy link

pyqh commented Dec 19, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants