From 4906787e0eb904698d1436b860ed3bc36dbcb584 Mon Sep 17 00:00:00 2001 From: Heath123 Date: Mon, 12 Apr 2021 21:42:12 +0100 Subject: [PATCH] Inverse and regex filtering (closes #21) --- html/mainPage/js/filteringLogic.js | 16 +++++++++++++-- html/mainPage/js/main.js | 32 +++++++++++++++++++++++++++--- html/mainPage/js/settings.js | 12 +++++------ html/mainPage/js/settings.json | 12 +++++++++++ 4 files changed, 61 insertions(+), 11 deletions(-) diff --git a/html/mainPage/js/filteringLogic.js b/html/mainPage/js/filteringLogic.js index 4fbf04a..4679f88 100644 --- a/html/mainPage/js/filteringLogic.js +++ b/html/mainPage/js/filteringLogic.js @@ -1,4 +1,4 @@ -exports.packetFilteredByFilterBox = function (packet, filter, hiddenPackets) { +exports.packetFilteredByFilterBox = function (packet, filter, hiddenPackets, inverseFiltering, regexFilter) { if (hiddenPackets[packet.direction].includes(packet.meta.name)) { return true } @@ -8,7 +8,19 @@ exports.packetFilteredByFilterBox = function (packet, filter, hiddenPackets) { } const comparisonString = packet.hexIdString + ' ' + packet.meta.name + ' ' + JSON.stringify(packet.data) - return !comparisonString.includes(filter) + + let result + if (regexFilter) { + result = filter.test(comparisonString) + } else { + result = comparisonString.includes(filter) + } + + if (inverseFiltering) { + return result + } else { + return !result + } } exports.packetCollapsed = function (packet, filter, hiddenPackets) { diff --git a/html/mainPage/js/main.js b/html/mainPage/js/main.js index 9deba06..788b145 100644 --- a/html/mainPage/js/main.js +++ b/html/mainPage/js/main.js @@ -72,10 +72,24 @@ function updateFilterBox () { } function updateFiltering () { + const inverseFiltering = sharedVars.settings.getSetting('inverseFiltering') + const regexFilter = sharedVars.settings.getSetting('regexFilter') + let regex + if (regexFilter) { + try { + regex = new RegExp(sharedVars.lastFilter) + } catch (err) { + // TODO: handle + regex = new RegExp("") + } + } sharedVars.allPacketsHTML.forEach(function (item, index, array) { - if (!filteringLogic.packetFilteredByFilterBox(sharedVars.allPackets[index], - sharedVars.lastFilter, - sharedVars.hiddenPackets)) { + if (sharedVars.lastFilter === '' || + !filteringLogic.packetFilteredByFilterBox(sharedVars.allPackets[index], + regexFilter ? regex : sharedVars.lastFilter, + sharedVars.hiddenPackets, + inverseFiltering, + regexFilter)) { // If it's hidden, show it array[index] = [item[0].replace('filter-hidden', 'filter-shown')] } else { @@ -188,6 +202,18 @@ sharedVars.settings.bindToSettingChange('showTimes', (newValue) => { document.body.classList.add('timeNotShown') } }) +sharedVars.settings.bindToSettingChange('inverseFiltering', (newValue) => { + try { + deselectPacket() + updateFiltering() + } catch (e) {} +}) +sharedVars.settings.bindToSettingChange('regexFilter', (newValue) => { + try { + deselectPacket() + updateFiltering() + } catch (e) {} +}) sharedVars.settings.setup(sharedVars) diff --git a/html/mainPage/js/settings.js b/html/mainPage/js/settings.js index f7a19ec..6d10267 100644 --- a/html/mainPage/js/settings.js +++ b/html/mainPage/js/settings.js @@ -10,21 +10,21 @@ exports.bindToSettingChange = function (settingId, f) { changeFunctions[settingId] = f } -function getSetting (id) { +exports.getSetting = function (id) { if (!sharedVars.store.get('settings.' + id)) { sharedVars.store.set('settings.' + id, settingsJson[id].default) } return sharedVars.store.get('settings.' + id) } -function setSetting (settingId, value) { +exports.setSetting = function (settingId, value) { sharedVars.store.set('settings.' + settingId, value) if (changeFunctions[settingId]) { changeFunctions[settingId](value) } } -window.setSetting = setSetting +window.setSetting = exports.setSetting function createToggle (settingId) { const toggleElement = document.createElement('label') @@ -33,9 +33,9 @@ function createToggle (settingId) { const input = document.createElement('input') input.id = settingId input.type = 'checkbox' - input.checked = getSetting(settingId) + input.checked = exports.getSetting(settingId) input.addEventListener('change', () => { - setSetting(settingId, input.checked) + exports.setSetting(settingId, input.checked) }); toggleElement.appendChild(input) @@ -91,7 +91,7 @@ exports.setup = function (passedSharedVars) { // Call change function if (changeFunctions[settingId]) { - changeFunctions[settingId](getSetting(settingId)) + changeFunctions[settingId](exports.getSetting(settingId)) } } } diff --git a/html/mainPage/js/settings.json b/html/mainPage/js/settings.json index 152a950..385db88 100644 --- a/html/mainPage/js/settings.json +++ b/html/mainPage/js/settings.json @@ -4,5 +4,17 @@ "description": "Shows information about the timing of packets on the right side of the packet list", "type": "boolean", "default": false + }, + "inverseFiltering": { + "name": "Inverse filtering", + "description": "Hide, rather than show, packets matching the filter text box", + "type": "boolean", + "default": false + }, + "regexFilter": { + "name": "Filter by regex", + "description": "Treat the filter box as a regular expression", + "type": "boolean", + "default": false } } \ No newline at end of file