Skip to content

Commit

Permalink
Fix data-text issue (#3)
Browse files Browse the repository at this point in the history
* Fix data-text issue

* Improve posts filter performance
Use Array.find() to stop searching for other words in the list if one has been found
  • Loading branch information
mohamedgomran authored and zeyadetman committed Apr 20, 2019
1 parent 7e5eb1c commit 9d7a564
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
11 changes: 7 additions & 4 deletions content.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const state = {
postsLength: 0,
wordsLength: 0,
lastPostIndex: 0,
};

const methodsCases = {
Expand All @@ -11,13 +12,15 @@ const methodsCases = {
if (allPosts.length === state.postsLength && currentWords.length === state.wordsLength) return;
state.postsLength = allPosts.length;
state.wordsLength = currentWords.length;
allPosts.forEach((post) => {
currentWords.forEach((word) => {
if (post.style.display === 'none') return;
allPosts.slice(state.lastPostIndex).forEach((post, i) => {
currentWords.find((word) => {
if (post.style.display === 'none') return true;
if (post.textContent.toLowerCase().includes(word.toLowerCase())) {
post.style.display = 'none';
return undefined; // critical return
state.lastPostIndex = i;
return true;
}
return false;
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion popup.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function listWords() {
window.chrome.storage.sync.get(['words'], (res) => {
const { words = '[]' } = res;
const listItems = JSON.parse(words).reduce((agg, itm) => agg.concat(`<li>${itm}<span data-text=${itm} class="delete-word">X</span></li>`), '');
const listItems = JSON.parse(words).reduce((agg, itm) => agg.concat(`<li>${itm}<span data-text="${itm}" class="delete-word">X</span></li>`), '');
document.getElementById('currentwords').innerHTML = listItems;
});
}
Expand Down

0 comments on commit 9d7a564

Please sign in to comment.