Search code examples
macossafaritld

How to add TLD to search exception in Safari


How can I add .locahost to the list of TLDs that Safari will load — instead of searching — without supplying a protocol? Alternatively (or perhaps additionally), how do I get Safari to stop removing the http:// protocol from the URL?


I've been using project.localhost to handle local development for a while, now that .dev is no longer a viable development TLD. But I'm getting frustrated with Safari's default behavior. What I'd like to do is prevent Safari from submitting the domain name to the search engine.

Desired:

  1. enter project.localhost into address bar
  2. browser loads http://project.localhost

I would be satisfied with Safari not removing the http:// in the address bar, but I can't find a way to do that, either.

What actually happens:

  1. enter project.localhost into address bar
  2. browser sends project.localhost to Duck Duck Go
  3. user is tempted to test the ballistic properties of keyboard

Or, after I've loaded http://project.localhost and then try to add something after the current URL:

  1. click in address bar, URL has been shortened to project.localhost
  2. add /test.html to end
  3. browser sends project.localhost/test.html to Duck Duck Go
  4. user considers software violence

Solution

  • Turns out that the Safari extension might be a long term solution. At this moment, I can't manage the developer membership cost. But here's the working code, for Future Me and anyone else looking into this.

    In the extension's global page <script>:

    (function() {
        safari.application.addEventListener("beforeSearch", function(event) {
            if (event.query.split("/")[0].split(".").pop() == "localhost") {
                event.preventDefault();
                safari.application.activeBrowserWindow.activeTab.url = "http://" + event.query;
            }
        }, true);
    })();