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": {
"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": {
"message": "Search..."
},
"options": {
"message": "Options"
},
"match_case": {
"message": "Match Case"
},
"match_whole": {
"message": "Match Whole Word"
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

19
main.js
View File

@@ -1,5 +1,7 @@
let currentLists = [];
let isGlobalHighlightEnabled = true;
let matchCase = false;
let matchWhole = false;
function escapeRegex(s) {
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
@@ -62,7 +64,12 @@ function processNodes() {
const wordMap = new Map();
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) {
if (!pattern.test(node.nodeValue)) continue;
@@ -102,12 +109,14 @@ function debounce(func, wait) {
}
// 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 (globalHighlightEnabled !== undefined) {
isGlobalHighlightEnabled = globalHighlightEnabled;
}
processNodes(); // Initial processing
matchCase = !!matchCaseEnabled;
matchWhole = !!matchWholeEnabled;
processNodes();
});
// 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") {
isGlobalHighlightEnabled = message.enabled;
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;
}
.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 {
display: flex;
gap: 24px;

View File

@@ -81,6 +81,15 @@
<div id="wordList"></div>
</div>
<div class="section">
<h2><i class="fa-solid fa-sliders"></i> <span data-i18n="options">Options</span></h2>
<div class="button-row" style="margin-bottom:8px;">
<label><input type="checkbox" id="matchCase" /> <span data-i18n="match_case">Match Case</span></label>
<label><input type="checkbox" id="matchWhole" /> <span data-i18n="match_whole">Match Whole Word</span></label>
</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>
@@ -88,7 +97,6 @@
<button id="exportBtn"><i class="fa-solid fa-download"></i> <span data-i18n="export_list">Export</span></button>
</div>
</div>
</div>
<script src="../storage.js"></script>
<script src="popup.js"></script>

View File

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