feat: added matching flags

This commit is contained in:
2025-06-27 00:43:31 +03:00
parent b766f61b0c
commit 759307f983
18 changed files with 188 additions and 14 deletions

View File

@@ -91,5 +91,8 @@
}, },
"search_placeholder": { "search_placeholder": {
"message": "Suchen..." "message": "Suchen..."
} },
"options": { "message": "Optionen" },
"match_case": { "message": "Groß-/Kleinschreibung beachten" },
"match_whole": { "message": "Ganzes Wort übereinstimmen" }
} }

View File

@@ -91,5 +91,14 @@
}, },
"search_placeholder": { "search_placeholder": {
"message": "Search..." "message": "Search..."
},
"options": {
"message": "Options"
},
"match_case": {
"message": "Match Case"
},
"match_whole": {
"message": "Match Whole Word"
} }
} }

View File

@@ -91,5 +91,14 @@
}, },
"search_placeholder": { "search_placeholder": {
"message": "Buscar..." "message": "Buscar..."
},
"options": {
"message": "Opciones"
},
"match_case": {
"message": "Coincidir mayúsculas/minúsculas"
},
"match_whole": {
"message": "Coincidir palabra completa"
} }
} }

View File

@@ -91,5 +91,14 @@
}, },
"search_placeholder": { "search_placeholder": {
"message": "Rechercher..." "message": "Rechercher..."
},
"options": {
"message": "Options"
},
"match_case": {
"message": "Respecter la casse"
},
"match_whole": {
"message": "Mot entier seulement"
} }
} }

View File

@@ -91,5 +91,14 @@
}, },
"search_placeholder": { "search_placeholder": {
"message": "खोजें..." "message": "खोजें..."
},
"options": {
"message": "विकल्प"
},
"match_case": {
"message": "केस मिलाएं"
},
"match_whole": {
"message": "पूरा शब्द मिलाएं"
} }
} }

View File

@@ -91,5 +91,14 @@
}, },
"search_placeholder": { "search_placeholder": {
"message": "Cerca..." "message": "Cerca..."
},
"options": {
"message": "Opzioni"
},
"match_case": {
"message": "Maiuscole/minuscole"
},
"match_whole": {
"message": "Solo parola intera"
} }
} }

View File

@@ -91,5 +91,14 @@
}, },
"search_placeholder": { "search_placeholder": {
"message": "検索..." "message": "検索..."
},
"options": {
"message": "オプション"
},
"match_case": {
"message": "大文字と小文字を区別"
},
"match_whole": {
"message": "完全一致"
} }
} }

View File

@@ -91,5 +91,14 @@
}, },
"search_placeholder": { "search_placeholder": {
"message": "검색..." "message": "검색..."
},
"options": {
"message": "옵션"
},
"match_case": {
"message": "대소문자 구분"
},
"match_whole": {
"message": "전체 단어 일치"
} }
} }

View File

@@ -91,5 +91,14 @@
}, },
"search_placeholder": { "search_placeholder": {
"message": "Zoeken..." "message": "Zoeken..."
},
"options": {
"message": "Opties"
},
"match_case": {
"message": "Hoofdlettergevoelig"
},
"match_whole": {
"message": "Alleen volledig woord"
} }
} }

View File

@@ -91,5 +91,14 @@
}, },
"search_placeholder": { "search_placeholder": {
"message": "Szukaj..." "message": "Szukaj..."
},
"options": {
"message": "Opcje"
},
"match_case": {
"message": "Uwzględnij wielkość liter"
},
"match_whole": {
"message": "Tylko całe słowo"
} }
} }

View File

@@ -91,5 +91,8 @@
}, },
"search_placeholder": { "search_placeholder": {
"message": "Pesquisar..." "message": "Pesquisar..."
} },
"options": { "message": "Opções" },
"match_case": { "message": "Diferenciar maiúsculas/minúsculas" },
"match_whole": { "message": "Palavra inteira" }
} }

View File

@@ -91,5 +91,14 @@
}, },
"search_placeholder": { "search_placeholder": {
"message": "Поиск..." "message": "Поиск..."
},
"options": {
"message": "Опции"
},
"match_case": {
"message": "С учетом регистра"
},
"match_whole": {
"message": "Слово целиком"
} }
} }

View File

@@ -91,5 +91,14 @@
}, },
"search_placeholder": { "search_placeholder": {
"message": "Ara..." "message": "Ara..."
},
"options": {
"message": "Seçenekler"
},
"match_case": {
"message": "Büyük/küçük harf duyarlı"
},
"match_whole": {
"message": "Tüm kelimeyle eşleş"
} }
} }

View File

@@ -91,5 +91,14 @@
}, },
"search_placeholder": { "search_placeholder": {
"message": "搜索..." "message": "搜索..."
} },
"options": {
"message": "选项"
},
"match_case": {
"message": "区分大小写"
},
"match_whole": {
"message": "全词匹配"
}
} }

19
main.js
View File

@@ -1,5 +1,7 @@
let currentLists = []; let currentLists = [];
let isGlobalHighlightEnabled = true; let isGlobalHighlightEnabled = true;
let matchCase = false;
let matchWhole = false;
function escapeRegex(s) { function escapeRegex(s) {
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
@@ -62,7 +64,12 @@ function processNodes() {
const wordMap = new Map(); const wordMap = new Map();
for (const word of activeWords) wordMap.set(word.text.toLowerCase(), word); for (const word of activeWords) wordMap.set(word.text.toLowerCase(), word);
const pattern = new RegExp(`(${Array.from(wordMap.keys()).map(escapeRegex).join('|')})`, 'gi'); let flags = matchCase ? 'g' : 'gi';
let wordsPattern = Array.from(wordMap.keys()).map(escapeRegex).join('|');
if (matchWhole) {
wordsPattern = `\\b(?:${wordsPattern})\\b`;
}
const pattern = new RegExp(`(${wordsPattern})`, flags);
for (const node of textNodes) { for (const node of textNodes) {
if (!pattern.test(node.nodeValue)) continue; if (!pattern.test(node.nodeValue)) continue;
@@ -102,12 +109,14 @@ function debounce(func, wait) {
} }
// Initial highlight on load // Initial highlight on load
chrome.storage.local.get(["lists", "globalHighlightEnabled"], ({ lists, globalHighlightEnabled }) => { chrome.storage.local.get(["lists", "globalHighlightEnabled", "matchCaseEnabled", "matchWholeEnabled"], ({ lists, globalHighlightEnabled, matchCaseEnabled, matchWholeEnabled }) => {
if (Array.isArray(lists)) setListsAndUpdate(lists); if (Array.isArray(lists)) setListsAndUpdate(lists);
if (globalHighlightEnabled !== undefined) { if (globalHighlightEnabled !== undefined) {
isGlobalHighlightEnabled = globalHighlightEnabled; isGlobalHighlightEnabled = globalHighlightEnabled;
} }
processNodes(); // Initial processing matchCase = !!matchCaseEnabled;
matchWhole = !!matchWholeEnabled;
processNodes();
}); });
// Listen for updates from the popup and re-apply highlights // Listen for updates from the popup and re-apply highlights
@@ -119,6 +128,10 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
} else if (message.type === "GLOBAL_TOGGLE_UPDATED") { } else if (message.type === "GLOBAL_TOGGLE_UPDATED") {
isGlobalHighlightEnabled = message.enabled; isGlobalHighlightEnabled = message.enabled;
processNodes(); processNodes();
} else if (message.type === "MATCH_OPTIONS_UPDATED") {
matchCase = !!message.matchCase;
matchWhole = !!message.matchWhole;
processNodes();
} }
}); });

View File

@@ -257,6 +257,19 @@ button.danger:hover {
flex-wrap: wrap; flex-wrap: wrap;
} }
.button-row label {
display: flex;
align-items: center;
gap: 6px;
cursor: pointer;
font-size: 1em;
}
.button-row input[type="checkbox"] {
margin: 0;
vertical-align: middle;
}
.color-row { .color-row {
display: flex; display: flex;
gap: 24px; gap: 24px;

View File

@@ -77,19 +77,27 @@
<button id="disableSelectedBtn"><span data-i18n="disable_selected">Disable</span></button> <button id="disableSelectedBtn"><span data-i18n="disable_selected">Disable</span></button>
<button id="deleteSelectedBtn" class="danger"><span data-i18n="delete_selected">Delete</span></button> <button id="deleteSelectedBtn" class="danger"><span data-i18n="delete_selected">Delete</span></button>
</div> </div>
<input type="text" id="wordSearch" data-i18n="search_placeholder" placeholder="Search..."/> <input type="text" id="wordSearch" data-i18n="search_placeholder" placeholder="Search..." />
<div id="wordList"></div> <div id="wordList"></div>
</div> </div>
<div class="section"> <div class="section">
<div class="button-row"> <h2><i class="fa-solid fa-sliders"></i> <span data-i18n="options">Options</span></h2>
<button id="importBtn"><i class="fa-solid fa-upload"></i> <span data-i18n="import_list">Import</span></button> <div class="button-row" style="margin-bottom:8px;">
<input type="file" id="importInput" accept="application/json" hidden /> <label><input type="checkbox" id="matchCase" /> <span data-i18n="match_case">Match Case</span></label>
<button id="exportBtn"><i class="fa-solid fa-download"></i> <span data-i18n="export_list">Export</span></button> <label><input type="checkbox" id="matchWhole" /> <span data-i18n="match_whole">Match Whole Word</span></label>
</div> </div>
</div> </div>
</div> </div>
<div class="section">
<div class="button-row">
<button id="importBtn"><i class="fa-solid fa-upload"></i> <span data-i18n="import_list">Import</span></button>
<input type="file" id="importInput" accept="application/json" hidden />
<button id="exportBtn"><i class="fa-solid fa-download"></i> <span data-i18n="export_list">Export</span></button>
</div>
</div>
<script src="../storage.js"></script> <script src="../storage.js"></script>
<script src="popup.js"></script> <script src="popup.js"></script>
</body> </body>

View File

@@ -6,12 +6,16 @@ const listActive = document.getElementById("listActive");
const bulkPaste = document.getElementById("bulkPaste"); const bulkPaste = document.getElementById("bulkPaste");
const wordList = document.getElementById("wordList"); const wordList = document.getElementById("wordList");
const importInput = document.getElementById("importInput"); const importInput = document.getElementById("importInput");
const matchCase = document.getElementById("matchCase");
const matchWhole = document.getElementById("matchWhole");
let lists = []; let lists = [];
let currentListIndex = 0; let currentListIndex = 0;
let saveTimeout; let saveTimeout;
let selectedCheckboxes = new Set(); let selectedCheckboxes = new Set();
let globalHighlightEnabled = true; let globalHighlightEnabled = true;
let wordSearchQuery = ""; let wordSearchQuery = "";
let matchCaseEnabled = false;
let matchWholeEnabled = false;
function escapeHtml(str) { function escapeHtml(str) {
return str.replace(/[&<>"']/g, function (m) { return str.replace(/[&<>"']/g, function (m) {
@@ -35,7 +39,9 @@ async function debouncedSave() {
async function save() { async function save() {
await chrome.storage.local.set({ await chrome.storage.local.set({
lists: lists, lists: lists,
globalHighlightEnabled: globalHighlightEnabled globalHighlightEnabled: globalHighlightEnabled,
matchCaseEnabled,
matchWholeEnabled
}); });
renderLists(); renderLists();
renderWords(); renderWords();
@@ -48,6 +54,11 @@ async function save() {
type: "GLOBAL_TOGGLE_UPDATED", type: "GLOBAL_TOGGLE_UPDATED",
enabled: globalHighlightEnabled enabled: globalHighlightEnabled
}); });
chrome.tabs.sendMessage(tab.id, {
type: "MATCH_OPTIONS_UPDATED",
matchCase: matchCaseEnabled,
matchWhole: matchWholeEnabled
});
} }
} }
}); });
@@ -70,10 +81,16 @@ async function updateGlobalToggleState() {
async function load() { async function load() {
const res = await chrome.storage.local.get({ const res = await chrome.storage.local.get({
lists: [], lists: [],
globalHighlightEnabled: true globalHighlightEnabled: true,
matchCaseEnabled: false,
matchWholeEnabled: false
}); });
lists = res.lists; lists = res.lists;
globalHighlightEnabled = res.globalHighlightEnabled !== false; // Default to true if undefined globalHighlightEnabled = res.globalHighlightEnabled !== false;
matchCaseEnabled = !!res.matchCaseEnabled;
matchWholeEnabled = !!res.matchWholeEnabled;
matchCase.checked = matchCaseEnabled;
matchWhole.checked = matchWholeEnabled;
if (!lists.length) { if (!lists.length) {
lists.push({ lists.push({
@@ -429,5 +446,14 @@ document.addEventListener('DOMContentLoaded', () => {
renderWords(); renderWords();
}); });
matchCase.addEventListener('change', () => {
matchCaseEnabled = matchCase.checked;
save();
});
matchWhole.addEventListener('change', () => {
matchWholeEnabled = matchWhole.checked;
save();
});
load(); load();
}); });