Thursday, May 23, 2024
HomeSoftware DevelopmentThe right way to Detect Failed Requests through Net Extensions

The right way to Detect Failed Requests through Net Extensions


Among the best issues that ever occurred to t he consumer expertise of the online has been net extensions. Browsers are highly effective however extensions convey a brand new degree of performance. Whether or not it is crypto wallets, media gamers, or different widespread plugins, net extensions have change into important to day by day duties.

Engaged on MetaMask, I’m thrust right into a world of creating every little thing Ethereum-centric work. A type of functionalities is guaranteeing that .eth domains resolve to ENS when enter to the deal with bar. Requests to https://vitalik.ethnaturally fail, since .eth is not a natively supported high degree area, so we have to intercept this errant request.

// Add an onErrorOccurred occasion through the browser.webRequest extension API
browser.webRequest.onErrorOccurred.addListener((particulars) => {
  const { tabId, url } = particulars;
  const { hostname } = new URL(url);

  if(hostname.endsWith('.eth')) {
    // Redirect to wherever I would like the consumer to go
    browser.tabs.replace(tabId, { url: `https://app.ens.domains/${hostname}}` });
  }
},
{
  urls:[`*://*.eth/*`],
  sorts: ['main_frame'],
});

Net extensions present a browser.webRequest.onErrorOccurred methodology that builders can plug into to hear for errant requests. This API does not catch 4** and 5** response errors. Within the case above, we search for .eth hostnames and redirect to ENS.

You could possibly make use of onErrorOccurred for any variety of causes, however detecting customized hostnames is a good one!

  • 7 Essential JavaScript Functions

    I bear in mind the early days of JavaScript the place you wanted a easy perform for almost every little thing as a result of the browser distributors applied options otherwise, and never simply edge options, primary options, like addEventListener and attachEvent.  Occasions have modified however there are nonetheless just a few features every developer ought to…

  • Responsive and Infinitely Scalable JS Animations

    Again in late 2012 it was not simple to search out open supply tasks utilizing requestAnimationFrame() – that is the hook that permits Javascript code to synchronize with an online browser’s native paint loop. Animations utilizing this methodology can run at 60 fps and ship improbable…

  • Create Spinning Rays with CSS3: Revisited
  • Comment Preview Using MooTools

    Remark previewing is an superior addition to any weblog. I’ve seen actually easy remark previewing and a few actually complicated remark previewing. The next is a tutorial on creating very primary remark previewing utilizing MooTools. The XHTML You’ll be able to arrange your XHTML any manner you want.


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments