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

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

Open
gignac-cha opened this issue Mar 5, 2024 · 0 comments

Comments

@gignac-cha
Copy link

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

The error comes from the following source code:

async function expandRange(range, type) {
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);
}
}

I've tested with the next code: gignac-cha@21f7472

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.

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

1 participant