Skip to content

Commit

Permalink
use synchronous highlight, async scroll markers
Browse files Browse the repository at this point in the history
  • Loading branch information
neaumusic committed Nov 6, 2020
1 parent 9310076 commit 557dad8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/chrome_extension/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "Selection Highlighter",
"version": "2.37",
"version": "2.39",
"description": "Highlight occurrences of selected text, with or without a keypress.",
"options_page": "${OPTIONS_PAGE_PATH}/options_page.html",
"content_scripts": [{
Expand Down
34 changes: 13 additions & 21 deletions src/highlighter/highlighter.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,29 +135,21 @@ function initialize () {
if (!options.isWindowLocationValid(window.location)) return;
if (!options.areKeysPressed(pressedKeys)) return;

removeThenHighlight(latestStartTime);
};

function removeThenHighlight (startTime) {
requestAnimationFrame(() => {
if (startTime !== latestStartTime) return;

document.querySelectorAll('.' + options.highlightedClassName).forEach(element => {
const parent = element.parentNode;
if (parent) {
parent.replaceChild(new Text(element.textContent || ''), element);
parent.normalize();
}
});
if (options.areScrollMarkersEnabled()) {
document.querySelectorAll('.' + options.scrollMarkerClassName).forEach(element => {
document.body.removeChild(element);
});
document.querySelectorAll('.' + options.highlightedClassName).forEach(element => {
const parent = element.parentNode;
if (parent) {
parent.replaceChild(new Text(element.textContent || ''), element);
parent.normalize();
}

highlight(startTime);
});
}
if (options.areScrollMarkersEnabled()) {
document.querySelectorAll('.' + options.scrollMarkerClassName).forEach(element => {
document.body.removeChild(element);
});
}

highlight(latestStartTime);
};

function highlight (startTime) {
const selection = document.getSelection();
Expand Down

0 comments on commit 557dad8

Please sign in to comment.