Skip to content

Commit

Permalink
Refactor away the array forEach calls
Browse files Browse the repository at this point in the history
Modern JS for loops appear to be more readable.
  • Loading branch information
c3er committed Jan 4, 2025
1 parent 57324b2 commit 7498c28
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
34 changes: 17 additions & 17 deletions app/lib/contentBlockingRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,22 @@ function hasBlockedElements() {
function unblockURL(url) {
ipc.send(ipc.messages.unblockURL, url)

const elements = _blockedElements[url]
if (elements) {
elements.forEach(element => {
element.removeAttribute("style")

// Force element to reload without recreating the DOM element.
// Recreating the DOM element would cause the attached event handlers to be lost.
const attributes = element.attributes
for (let i = 0; i < attributes.length; i++) {
const attr = attributes[i]
const value = attr.nodeValue
if (value === url) {
element.setAttribute(attr.nodeName, value)
}
const elements = _blockedElements[url] ?? []
for (const element of elements) {
element.removeAttribute("style")

// Force element to reload without recreating the DOM element.
// Recreating the DOM element would cause the attached event handlers to be lost.
const attributes = element.attributes
for (let i = 0; i < attributes.length; i++) {
const attr = attributes[i]
const value = attr.nodeValue
if (value === url) {
element.setAttribute(attr.nodeName, value)
}
})
delete _blockedElements[url]
}
}
delete _blockedElements[url]

if (!hasBlockedElements()) {
changeInfoElementVisiblity(false)
Expand Down Expand Up @@ -97,7 +95,9 @@ exports.init = (document, window, shallForceInitialization) => {

ipc.listen(ipc.messages.contentBlocked, url => {
const elements = (_blockedElements[url] = searchElementsWithAttributeValue(url))
elements.forEach(element => (element.onclick = () => unblockURL(url)))
for (const element of elements) {
element.onclick = () => unblockURL(url)
}

changeInfoElementVisiblity(true)
_document.getElementById(_elementIDs.textContainer).onclick = unblockAll
Expand Down
12 changes: 9 additions & 3 deletions test/mocking.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,15 @@ class IpcChannel {
_targetAssertionCallbacks = []

send(event, ...args) {
this._targetCallbacks.forEach(callback => callback(event, ...args))
this._sourceAssertionCallbacks.forEach(callback => callback(event, ...args))
this._targetAssertionCallbacks.forEach(callback => callback(event, ...args))
for (const callback of this._targetCallbacks) {
callback(event, ...args)
}
for (const callback of this._sourceAssertionCallbacks) {
callback(event, ...args)
}
for (const callback of this._targetAssertionCallbacks) {
callback(event, ...args)
}
}

addTarget(callback) {
Expand Down

0 comments on commit 7498c28

Please sign in to comment.