I built a chrome extension that manipulates the DOM - i.e. highlight, underline, etc via the following functions.
function getSelectedText() {
let selected = window.getSelection();
if (selected.rangeCount && selected.getRangeAt) {
range = selected.getRangeAt(0);
}
if (range) {
selected.removeAllRanges();
selected.addRange(range);
}
}
function createSpan(addClass) {
try {
let newElement = document.createElement('span');
newElement.classList.add(addClass);
range.surroundContents(newElement);
} catch (error) {
if (error instanceof DOMException) {
alert('XYZ');
}
}
}
The paramter "addClass" attaches to its respective CSS script, which is appended to the html at the beginning of my content script. This works fine, but now I am not sure how to re-build the range object in order to load all of the changes made on tab refresh or reopen.
Some of my attempts include the following;
- saving the innerHTML of the parentElement, so it includes the newly created span tags and overwriting the html innerHTML with it on page load. I received a warning that stated "reverting mutation of childList in component" and this did not work
- trying to save the range object as a whole, returned an {} empty object
Unable to store/retrieve an object using chrome.storage.sync in chrome extension
Then, I came across another post in which the user was also having trouble because node objects, required for range.setStart and .setEnd, are not JSONifiable and hence cannot be saved to chrome.storage.
Is there a workaround for this please? Or if you think my entire approach of using .surroundContents to manipulate the DOM should be changed, I would kindly appreciate any insights as this is my very first extension and I am about 2 months into javascript.
A sample of a DOM manipulated node would look something like this;
<div>ABC <span class="highlighted">DEF</span> GHI</div>