Reactgular
Reactgular

Reputation: 54791

Block localhost quickly in Chrome for testing AJAX failures

When I want to test some Javascript code and how it handles a request failure. I have to stop Apache on my development machine. Make the request and then restart Apache.

It's a time consuming activity.

Is there some Chrome trick or extension where I can quickly toggle access to localhost or a domain?

Upvotes: 0

Views: 418

Answers (1)

Chris McFarland
Chris McFarland

Reputation: 6169

A Chrome extension that uses chrome.webRequest to block requests to localhost could work.

The extension's manifest.json file could look something like this:

{
    "name": "localhost blocker",
    "background": {
        "scripts": ["background.js"],
        "persistent": true
    },
    "permissions": [
        "webRequest",
        "webRequestBlocking",
        "<all_urls>"
    ]
}

And your background.js file could be:

chrome.webRequest.onBeforeRequest.addListener(
    function(details) { return {cancel: true}; },
    {urls: ["*://localhost/*"]},
    ["blocking"]
);

Upvotes: 1

Related Questions