yomichan icon indicating copy to clipboard operation
yomichan copied to clipboard

Copy and paste text with furigana in Chrome

Open quriousxr opened this issue 3 years ago • 6 comments

I found that in Chrome when some text with furigana is copied, furigana will be copied as well

On search page image

飛び移って -> 飛とび移うつって when copied and pasted

Same thing happens in dictionary entries image

飛び移る -> 飛とび移うつる

However, in Firefox, furigana are ignored when text is copied image

飛び移る -> 飛び移る

Would it be possible to copy text without furigana being copied in Chrome? Am I missing something obvious?

quriousxr avatar Apr 09 '21 03:04 quriousxr

That's a browser difference that is unrelated to Yomichan.

うつ

toasted-nutbread avatar Apr 09 '21 21:04 toasted-nutbread

The best you'll able to do with Yomichan is to add this custom CSS rule:

rt {
    user-select: none;
}

but that will only affect text inside Yomichan's search/popup page.

toasted-nutbread avatar Apr 09 '21 21:04 toasted-nutbread

Thanks for the help. I tried the CSS rule but this problem occurs #1152.

I guess I will just use Firefox then. It is easier for me to copy and paste word for look up.

quriousxr avatar Apr 10 '21 12:04 quriousxr

The issue described in #1152 should be fixed in the latest release. If you describe the issue you're seeing, I can take a look at that also; the additional CSS rule should not affect behaviour.

toasted-nutbread avatar Apr 10 '21 14:04 toasted-nutbread

Maybe this is just my problem but when I added the additional CSS rule , the issue #1152 occurred, it went back to normal if I removed the CSS rule. Thanks again for the help for an issue unrelated to Yomichan. I think I can just use Firefox, it works very well for my usage.

quriousxr avatar Apr 13 '21 11:04 quriousxr

I am experiencing the same issue here as well.

A fix can be implemented by intercepting the copy event, hiding the kana, then copying the content, then re-showing the kana. I've written an extension to do this on some sites, but I think it would have to be done within the Yomichan extension itself to work in this case, and probably gated with a user setting because sometimes folks probably intend to copy the kana 🤔

If anyone more familiar with the codebase could point me at a good location to try adding this within the project, I'd be happy to try and hack something together myself 🤷🏻

document.addEventListener("copy", (event) => {
  // 1. Hide furigana
  const furigana = document.querySelectorAll(".furigana, .dc-hiragana, .fg, rt");
  if (furigana[0]) {
    Array.from(furigana, (f) => { f.style.display = "none" });
  }

  // 2. Get selection
  const selection = document.getSelection();
  let textToCopy = selection.toString().trim();

  // 3. Remove line breaks from array
  const lineBreaks = /(\r\n|\n|\r)/gm;
  textToCopy = textToCopy
    .split(lineBreaks)
    .filter((item) => !item.match(lineBreaks))
    .join("");

  // 4. Re-show furigana if they were hidden
  if (furigana[0]) {
    Array.from(furigana, (f) => { f.style.display = "" });
  }

  event.clipboardData.setData("text/plain", textToCopy);
  event.preventDefault();
});

jhunschejones avatar Aug 12 '22 20:08 jhunschejones