Bighted19
Bighted19

Reputation: 129

disable connection to a website with javascript

Is it possible to disable the internet of a tab in a browser using javascript and cut the connection to a server? Like disabling the outgoing connections in firewall.

Upvotes: 4

Views: 1354

Answers (2)

Madacol
Madacol

Reputation: 4276

You can set a CSP policy in js that blocks every connection, like this:

{   // Go Offline
    const meta = document.createElement('meta');
    meta.httpEquiv = "Content-Security-Policy";
    meta.content = "default-src 'none';";
    document.head.appendChild(meta);
}

Upvotes: 0

Filip Dupanović
Filip Dupanović

Reputation: 33670

Probably the simplest way you can simulate network errors is with Service Workers, by intercepting requests and hijacking the response with your own content:

self.addEventListener('fetch', event => event.respondWith(
  event.request.url.matches(BLOCK) ? Response.error() : fetch(event.request)
));

Another viable approach, perhaps one that better conveys what is it that your actually attempting to accomplish, is by adding appropriate Content Security Policy directives. You could achieve this with your server, by adding them through response headers, statically in your HTML markup or by dynamically adding elements to the DOM.

Upvotes: 3

Related Questions