Archyblog


Ramblings of yet another developer!
  • JavaScript Remove Event Listeners Without Handler Function

    Posted on May 29, 2024

    JavaScript has a function, removeEventListener, for removing event listeners from HTML elements. The drawback of this is that it requires you to provide the exact handler function to remove.

    Without getting into why you might want to do this, browsers make it impossible for you to remove event listeners from HTML elements without providing the handler. There are pretty good security reasons for this.

    A quick Google search provides multiple very similar answers that don't really address the issue. You either need to be the one adding the event listener in the first place. Or you can use the deprecated getEventListeners function, which is now only available in Chrome DevTools.

    There is one other option, and that is to replace the HTML element in the DOM using the outerHTML property. This works great if you want to remove all listeners, but has the unfortunate side effect of removing the listeners from the child nodes too.

    There is a way around this, however, and that is to store references to the child nodes, remove them, then update the outerHTML property, before finally adding the child nodes back to the HTML element. See below how to do that for the body element:

    var nodes = [];
    document.body.childNodes.forEach((node) => nodes.push(node));
    document.body.innerHTML = "";
    document.body.outerHTML = document.body.outerHTML;
    nodes.forEach((node) => document.body.appendChild(node));

    And that's it. The element should now be free of event listeners!

  • Empty or Zero Percent (0%) Code Coverage With PHPUnit and pcov

    Posted on Feb 14, 2024

    I recently updated my PHP version and, since I use Docker, I upgraded the image version to get the newer version. After doing this, my PHPUnit tests stopped reporting coverage. With v8.5 of PHPUnit I saw all the files in the report but every one was returning 0% coverage. I upgraded to 9.6 in an effort to fix the issues, and the report suddenly showed no files at all. So it was crystal clear there was a separate issue!

  • "Unable to read key" Laravel Error Deploying to Dokku/Heroku

    Posted on Jan 10, 2024

    I've recently had all sorts of issues building a Laravel app for Dokku because the Heroku buildpack it uses builds the application in an isolated container that has no access to the storage/ directory. I consistently found myself getting errors such as the following when running artisan commands: Unable to read key from file file:///tmp/build/storage/oauth-public.key

  • "Trying to get property 'x' of non-object" With Null Coalescing Operator in PHP

    Posted on Aug 06, 2020

    Quick one here but today, thanks to Tez on Stack Overflow, I discovered that PHP's null coalescing operator (the double question mark operator) can fail when you use a function at any part of the statement.

  • Migrating to Partitioned Native Google BigQuery Table From External GCS Files

    Posted on May 22, 2020

    I've recently been working on an Apache Kafka/Confluent data pipeline to analyse event streams. I decided to use Google Cloud BigQuery for the data analysis as it seemed to be easy to get set up with and extremely powerful. But to get up and running I'd need to backfill all my existing data. I also decided to add it to a time-partitioned table to increase performance and reduce costs.