
David Walsh
1,000 FOLLOWERS
A blog featuring tutorials about JavaScript, HTML5, AJAX, PHP, CSS, WordPress, and everything else development. David Walsh is Mozilla's senior web developer and the core developer for the MooTools Javascript Framework.
David Walsh
2w ago
A user’s clipboard is a “catch all” between the operating system and the apps employed on it. When you use a web browser, you can highlight text or right-click an image and select “Copy Image”. That made me think about how developers can detect what is in the clipboard.
You can retrieve the contents of the user’s clipboard using the navigator.clipboard API. This API requires user permission as the clipboard could contain sensitive data. You can employ the following JavaScript to get permission to use the clipboard API:
const result = await navigator.permissions.query({name: "clipboard-write ..read more
David Walsh
1M ago
We all love beautifully styled form controls but, due to the differences between operating system displays, styling them can be painful. Due to that pain, we’ve created scores of libraries to mock these controls. Unfortunately that sometimes comes at the cost of accessibility, performance, etc.
One control that has traditionally been tough to style is the input[type=file] element. Said input variation visually contains a button and text, all being clickable. Bit of a Frankenstein’s monster if you ask me. Can we style the button part though? We can!
To style the button button portion of input[t ..read more
David Walsh
1M ago
I love the Brave web browser for many reasons: ad blocking, Brave rewards, crypto integration, and even a Tor tab feature. I’ll often use the Tor feature but wanted to know how I could automated opening Tor windows from command line.
To open a Brave Tor tab, you can use the following command:
open -a "Brave Browser" --args --incognito --tor
Any time I want to remotely open a Tor tab, I can do so via a shell script. Commands are such an underused but valuable utility for apps!
The post How to Open a Tor Brave Window from Command Line appeared first on David Walsh Blog ..read more
David Walsh
1M ago
Many engineers like myself live in the command line, and perform actions from command line that most others would click an icon for. I’ve always found opening apps from command line on Macs painful. You need to references the Applications directory, add .app to the name, etc. I just want to open apps by name.
To open an app from any directory by its simple name, you can use the -a argument to open:
open -a Cyberduck
# Works regardless of case as well
open -a CyBeRdUcK
I love -a for a command like open. Being able to open any app by name is exactly what I want!
The post How to Open an App f ..read more
David Walsh
1M ago
When I was a child, I loved looking for Waldo in the “Where’s Waldo?” book series. These days I’m a sucker for TMZ’s “What’s the Big Frigin Difference” images, where TMZ slightly changes an image and you have to spot the differences between the two. That got me to thinking — how easily could I automate diff’ing two images? This StackOverflow post was gold.
To create a diff of two similar images, we’ll use ImageMagick’s convert command line utility with a large host of configurations:
convert '(' image1.png -flatten -grayscale Rec709Luminance ')' \
'(' image2.png -flatten -grayscale Re ..read more
David Walsh
2M ago
The start of a new year is usually a time when we start looking for ways to make something a little better. That something could be our life, work, or what we produce. Web designers, for example, might look for ways to make their designs more interesting or effective.
In this post we will focus on 5 web design trends that are designed to help users get the most from the websites they visit and we will use 10 pre-built websites from BeTheme to demonstrate how best to implement those trends .
BeTheme is one of the world’s most popular and highly-rated WordPress Themes with 268,000+ sales and a 4 ..read more
David Walsh
2M ago
A few years back I wrote a blog post about how write a fetch Promise that times out. The function was effective but the code wasn’t great, mostly because AbortController , which allows you to cancel a fetch Promise, did not yet exist. With AbortController and AbortSignal available, let’s create a better JavaScript function for fetching with a timeout:
Much like the original function, we’ll use setTimeout to time to the cancellation but we’ll use the signal with the fetch request:
async function fetchWithTimeout(url, opts = {}, timeout = 5000) {
// Create the AbortController instance, get Ab ..read more
David Walsh
2M ago
Form validation has always been my least favorite part of web development. You need to duplicate validation on both client and server sides, handle loads of events, and worry about form element styling. To aid form validation, the HTML spec added some new form attributes like required and pattern to act as very basic validation. Did you know, however, that you can control native form validation using JavaScript?
validity
Each form element (input, for example) provides a validity property which represents a ValidityState. ValidityState looks something like this:
// input.validity
{
badInput ..read more
David Walsh
2M ago
Promises have changed the landscape of JavaScript. Many old APIs have been reincarnated to use Promises (XHR to fetch, Battery API), while new APIs trend toward Promises. Developers can use async/await to handle promises, or then/catch/finally with callbacks, but what Promises don’t tell you is their status. Wouldn’t it be great if the Promise.prototype provided developers a status property to know whether a promise is rejected, resolved, or just done?
My research led me to this gist which I found quite clever. I took some time to modify a bit of code and add comments. The following solution p ..read more
David Walsh
2M ago
A few years ago I wrote an article about how to detect VR support with JavaScript. Since that time, a whole lot has changed. “Augmented reality” became a thing and terminology has moved to “XR”, instead of VR or AR. As such, the API has needed to evolve.
The presence of navigator.xr signals that the browser supports the WebXR API and XR devices:
const supportsXR = 'xr' in window.navigator;
I really like using in for feature checking rather than if(navigator.xr), as simply invoking that could cause some initialization to take place. In future posts we’ll explore identifying and connecting to ..read more