This article explains how to use the Network API.
On iOS browsers, the Network API can only be used to intercept and alter DRM-related requests and responses.
The technical documentation on the Network API can be found here.
Web SDK | Android SDK (Legacy) | iOS SDK (Legacy) | tvOS SDK | Android TV SDK (Legacy) | Chromecast SDK |
---|---|---|---|---|---|
Yes | Through JS injection | Through JS injection | No | Through JS injection | Yes |
The request interceptor can, as the handle suggests, be used to intercept requests. The method is available under player.network
and requires a request object. Within the request object, you can change headers, redirect a license request, etc. You can't change the response with this interceptor, because that's covered with the response interceptor below.
In the example below, the request interceptor is used to redirect certain URL to another URL.
// Add a request interceptor
player.network.addRequestInterceptor(request => {
// The interceptor intercepts ALL requests and we only want to redirect one specific url
if (request.url == "https://link-to-specific-url") {
// Redirect the request
request.redirect({
url: "https://link-to-new-url",
method: request.method, // Keep the methods the same
headers: request.headers, // Keep the headers the same
closed: request.closed,
useCredentials: request.useCredentials,
type: request.type,
subType: request.subType,
responseType: request.responseType,
body: request.body
});
}
});
Add the above JavaScript snippet to your legacy Android (TV) SDK project as demonstrated at How to add CSS or JavaScript files to an Android/iOS project.
Add the above JavaScript snippet to your legacy iOS SDK project as demonstrated at How to add CSS or JavaScript files to an Android/iOS project.
The response interceptor is used to intercept/change responses. You can overwrite any response that the player receives. It is important to respond in the proper format (with JSON, base-64 body, ...)
Like the example above, this example will overwrite a response:
player.network.addResponseInterceptor(response => {
// If the response comes from a specific URL
if (response.url == "https://specific-url") {
// Requests run async, therefore make sure to execute after the request is finished
response.waitUntil(done => {
response.respondWith({
closed: response.closed,
headers: response.headers,
request: response.request,
respondWith: response.respondWith,
status: 201, // Updated status code as example
statusText: "All cool bro", // Updated status text as example
url: response.url,
waitUntil: response.waitUntil,
body: "New body"
});
done();
});
}
});
Add the above JavaScript snippet to your legacy Android (TV) SDK project as demonstrated at How to add CSS or JavaScript files to an Android/iOS project.
Add the above JavaScript snippet to your legacy iOS SDK project as demonstrated at How to add CSS or JavaScript files to an Android/iOS project.
If the status code is set to a code between 200-299, the player responds with a successful response, in any other cases the player will respond with an HTTP error.
If the player originally responded with an HTTP error, the interceptor can change the response to a successful response and vice versa.
The 'online' and 'offline' events can be added to the network as follows:
player.network.addEventListener('online', handleOnlineEvent);
Events can be removed similarly, as follows:
player.network.removeEventListener('online', handleOnlineEvent);