Is it possible to utilize an AJAX request in a Chrome Extension to determine if a URL supports HTTP2

Is it possible for a user to input their website URL into an input box within a Chrome Extension, allowing the extension to use AJAX or a similar method to determine if the server behind the URL can send responses via HTTP2?

Could the WebRequest API or Fetch API provide a way to gather this information? Is there a way for the request to communicate to the server that only HTTP2 replies are accepted? The solution is not immediately apparent.

I am aware that using

window.chrome.loadTimes().connectionInfo
can reveal the protocol of the current page, but this approach involves loading the entire page, which may not be ideal.

Here are some example URLs:

Delivered over HTTP2:

Delivered over HTTP 1.1:

Answer №1

It is important to note that HTTP/2 responses require a "status" response header according to the specifications found at . To determine if a response is using HTTP/2, you can utilize the

chrome.webRequest.onHeadersReceived
event along with "responseHeaders" in extraInfoSpec. For instance, consider the following test cases:

chrome.webRequest.onHeadersReceived.addListener(function(details) {
    var isHttp2 = details.responseHeaders.some(function(header) {
        return header.name === 'status';
    });
    console.log('Request to ' + details.url + ', http2 = ' + isHttp2);
}, {
    urls: ['https://cdn.sstatic.net/*', 'http://stackoverflow.com/*'],
    types: ['xmlhttprequest']
}, ['responseHeaders']);

// Tests:
fetch('http://stackoverflow.com');
fetch('https://cdn.sstatic.net');

Answer №2

UPDATE: I recently discovered a clever method using the iframe along with the webRequest technique! Although I have not tested it myself, here is a useful gist that explains how to implement it:

https://gist.github.com/username/repo123

PREVIOUS RESPONSE

It seems challenging to accomplish this task without utilizing an external API due to certain limitations.

1) To make ajax requests, the server of the target URL must return CORS headers in order for the browser to accept them.

2) One workaround involves dynamically creating an iframe and accessing

chrome.loadTimes().connectionInfo
within its contentWindow, but if the server sends a X-Frame-Options: Deny header, loading the URL in the iframe will be blocked by the browser.

3) Attempting to bypass the X-frame headers using the webRequest API as discussed in this post

Bypassing X-Frame-Options DENY in a Browser Extension

might not produce the desired outcome; Chrome extensions are typically prohibited from altering response bodies.

Potential Solutions

1) Address these challenges by deploying a simple proxy that includes the necessary headers. This guide demonstrates how to achieve this using Nginx

http://example.com/nginx-proxy

2) Alternatively, consider developing a custom API to handle the request and verify HTTP/2 support on the server-side. In case your extension gains popularity, scaling up could involve strategies like caching and horizontal scaling.

I hope these suggestions prove beneficial!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enhance your table with jQuery for expanding and collapsing views with just a

Interactive Table Display I am looking to create a table that shows more rows when the "View More" button is clicked. Once all rows are shown, the button text should change to "View Less" and clicking it will hide the extra rows. The functionality should ...

Seeking advice on extracting data using Angular and transferring it using Node.js?

I have decided to switch my node.js application from using the EJS template engine to Angular. After successfully installing and setting up Angular, I am now trying to retrieve data using the $http service: (function($) { app.controller('EventCtrl ...

Understanding the origin of a post request from a PhoneGap or Cordova app on a PHP server

I have a Phonegap / cordova app and I need to send POST and GET requests to my server using AJAX. How can I ensure that my PHP file only processes requests if they come from my app? For example: if($_POST["key"]==$secret_key_got_from_server) { // Do t ...

What is the best way to implement the async function in ES6?

As a newcomer to React, I am trying to implement async-await in the useEffect hook but I am encountering a syntax issue that I can't resolve. Here's my code: useEffect(()=>{ checkLogin=async()=>{ await checkUserLogin=()=>{ ...

Parsing Google API in JSON format

What is the best way to convert this URL into JSON format? https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=&rsz=8" public class MainActivity extends ListActivity { // Code for parsing URL to JSON goes here... } I'm trying t ...

Loop through all the textarea fields within a designated class to remove emojis from a field that contains "Gift Message" in its name

I need a pure JavaScript solution for extracting the value of a textarea field with "Gift Message" in its name. I'm currently stuck and unable to achieve this. Can someone assist me in resolving this issue? Below is the HTML code snippet: <span cl ...

The problem of removing issue divs persists despite Jquery's efforts

Just yesterday, I successfully created a Commentbox using PHP, HTML, and ajax. The beauty of the ajax part is that it allows me to delete a comment without having to refresh the page. To achieve this, I assign a unique id to each comment (div) through the ...

Tips for releasing a dual npm package with both CommonJS and module support to ensure consistent imports of submodules

Trying to figure out how to package an NPM package so that it includes both CommonJS and ES modules that can be imported using the same absolute module path has been a challenge for me. I want to ensure that regardless of whether it's in a node or bro ...

When activated multiple times, Element pays no attention to jQuery

As I work on making my website responsive, I have encountered an issue with the jQuery script. When rapidly changing the window size, it seems to ignore the conditions set for elements to be displayed or hidden. The CSS is straightforward: body:after{dis ...

JS Tips for using the .push() function with Multidimensional Arrays in JavaScript

After coming across this code example online, I decided to give it a try. The code snippet involved using the JavaScript array push method to add new elements to an inner sub-array, which was exactly what I needed to achieve! The code successfully demons ...

Issue encountered when multiple Ajax requests are sent to Wicket server simultaneously

Whenever a user clicks, I am utilizing AJAX to send data to the Wicket server. The challenge arises when I try to retrieve the data upon page load using an AJAX GET request. Initially, everything works smoothly with just one request. However, on subsequent ...

AngularJS Default Option Selection

I am encountering some difficulties with the preselection of a select-input in angularJS. The select-box is being populated by an array. <select class="form-control" ng-model="userCtrl.selected_country" ng-options="country.name for country in userCt ...

Trying out the Deezer app and receiving the message: "Please provide a valid redirect URI."

While testing an application using the Deezer JavaScript SDK, I encountered an issue when trying to login as it prompted me with a "You must enter a valid redirect uri" message. Here is the setup: DZ.init({ appId: '000000', channelUrl: ...

Differences in jQuery selector behavior when targeting elements in parent windows compared to popup windows

In order for my userscript to function correctly, I have implemented a temporary solution. To create code that is easier to understand and maintain, it's essential for me to gain a deeper understanding of WHY the temporary fix works. Here is some code ...

Asynchronous Middleware in ExpressJS Routing

I'm having trouble implementing a middleware in Express. Whenever I make a request, I end up with infinite loading times. I've searched on various platforms but couldn't find any examples that utilize an async await middleware stored in a se ...

Are there any tools available that can convert ThreeJS code into WebGL?

Are there any existing tools that can convert ThreeJS to WebGL? Or, could you provide guidance on creating a converter for ThreeJS to WebGL? ...

Can someone provide details on how to reset the auto-scroll timer on my content slideshow after each click?

I'm currently building a content slideshow and you can find it at Below is the javascript code: jQuery(document).ready(function($) { setInterval(function() { moveRight(); }, 8000); var slideCount = $('#slider-isp ul li').length; ...

Error message: Icons failing to display in conjunction with template output | 404 error code

I'm trying to display icons based on a search, but I keep getting a 404 error for http://localhost:3000/static when I input a value. My current folder structure looks like this: Root directory > Public > Icons and Root directory > index.js. ...

When running a callback function, the "this" of an Angular 2 component becomes undefined

One issue I'm facing is with a component that fetches data from a RESTful endpoint using a service, which requires a callback function to be executed after fetching the data. The problem arises when attempting to use the callback function to append t ...

Using Jquery to encase an element in a div while scrolling down and removing it while scrolling up

After some experimentation, I've managed to wrap an element inside a div using jQuery. My next challenge is to wrap it as you scroll down and unwrap it as you scroll up. Do you think this is achievable? Although I have succeeded in wrapping it while ...