Skip to content

Commit

Permalink
Inverse and regex filtering (closes #21)
Browse files Browse the repository at this point in the history
  • Loading branch information
Heath123 committed Apr 12, 2021
1 parent 5836af0 commit 4906787
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
16 changes: 14 additions & 2 deletions html/mainPage/js/filteringLogic.js
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -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) {
Expand Down
32 changes: 29 additions & 3 deletions html/mainPage/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)


Expand Down
12 changes: 6 additions & 6 deletions html/mainPage/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)

Expand Down Expand Up @@ -91,7 +91,7 @@ exports.setup = function (passedSharedVars) {

// Call change function
if (changeFunctions[settingId]) {
changeFunctions[settingId](getSetting(settingId))
changeFunctions[settingId](exports.getSetting(settingId))
}
}
}
12 changes: 12 additions & 0 deletions html/mainPage/js/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 4906787

Please sign in to comment.