ttop32 / MouseTooltipTranslator

Mouseover Translate Any Language At Once - Chrome Extension: PDF Translator, EBOOK, EPUB, OCR, TTS, YOUTUBE DUAL SUBTITLES, GOOGLE DOCS, AI, VIEWER, GMAIL, WRITING, IMAGE, DUAL SUBS, MANGA, HOVER, DICTIONARY, WEBTOON, EDGE, JAPANESE, ENGLISH
MIT License
628 stars 99 forks source link

Errors in console `TypeError: Failed to execute 'expand' on 'Range': The node provided is null.` #104

Open gignac-cha opened 6 months ago

gignac-cha commented 6 months ago

There are errors in console:

TypeError: Failed to execute 'expand' on 'Range': The node provided is null.

or,

DOMException: Failed to execute 'setEndAfter' on 'Range': the given Node has no parent.
Screenshot ![image](https://github.com/ttop32/MouseTooltipTranslator/assets/38489680/2845e69c-97a1-4fef-9d87-6c89a660816e)

The error comes from the following source code: https://github.com/ttop32/MouseTooltipTranslator/blob/baa4cee68b6ae835cd106dfc86a8a3d0d6a7ab1b/src/event/mouseover.js#L96-L109

I've tested with the next code: https://github.com/gignac-cha/MouseTooltipTranslator/commit/21f7472b96b4fbd916f6e40750243466f0aa6d50

Diff ```diff var clientX = 0; var clientY = 0; var _win; var _isIframe = false; var styleElement; const PARENT_TAGS_TO_EXCLUDE = ["STYLE", "SCRIPT", "TITLE"]; +const rangeNodeMap = new Map(); async function expandRange(range, type) { + if (!rangeNodeMap.get(range)) { + return; + } try { if (type == "container" || !range.expand) { range.setStartBefore(range.startContainer); range.setEndAfter(range.startContainer); range.setStart(range.startContainer, 0); } else { range.expand(type); // "word" or "sentence" // await expandRangeWithSeg(range, type); } } catch (error) { console.log(error); } } export function getTextRange(textNode) { var range = document.createRange(); range.setStart(textNode, 0); range.setEnd(textNode, textNode.length); + rangeNodeMap.set(range, textNode); return range; } export function getCharRanges(textNode) { var ranges = []; for (let i = 0; i < textNode.length - 1; i++) { var range = document.createRange(); range.setStart(textNode, i); range.setEnd(textNode, i + 1); ranges.push(range); + rangeNodeMap.set(range, textNode); } return ranges; } ```

then no more error.

No errors in incognito mode, so it seems to be a conflict with other extensions. (I think the problem comes from Video Speed Controller...) (and I don't know why but the error occurs in https://github.com/<organization>/<repository>/pull/<pull_request_number>/files even in incognito mode!)

If you don't need to fix this, please ignore it.