GithubHelp home page GithubHelp logo

default-browser's Introduction

default-browser

Get the default browser

Works on macOS, Linux, and Windows.

Install

npm install default-browser

Usage

import defaultBrowser from 'default-browser';

console.log(await defaultBrowser());
//=> {name: 'Safari', id: 'com.apple.Safari'}

Related

default-browser's People

Contributors

azazelm3dj3d avatar connor4312 avatar kevva avatar richienb avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

default-browser's Issues

use import instead of require on linux

        return require('xdg-default-browser')();
        ^

ReferenceError: require is not defined in ES module scope, you can use import instead

i can fix this but i am too lazy to do a pr

reduce bundle size by 90%

the current solution for darwin is horrible
cos we need a 50 KB bundle
to solve a problem that can be solved in like 1 KB

when on darwin, we can use platform-dependent child_process.spawnSync calls

#!/bin/bash
# https://stackoverflow.com/a/32465364/10440128

x=~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist; \
plutil -convert xml1 $x; \
grep 'https' -b3 $x | awk 'NR==2 {split($2, arr, "[><]"); print arr[3]}'; \
plutil -convert binary1 $x

this is just one sample solution

at first sight, the file in $x
should not be converted back-and-forth between xml and binary format
only once from binary to xml
or something readable like JSON - yes, plutil can translate to json
also avoid using temp-files

here is a draft version, i have no darwin machine to test = no warranty

// darwin os: get default browser name
// use the `plutil` tool https://www.manpagez.com/man/1/plutil/
const child_process = require('child_process');
const lsf = '~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist';
const lso = JSON.parse(child_process.spawnSync(
  'plutil', ['-convert', 'json', '-o', '-', lsf],
  {maxBuffer: Infinity, encoding: 'utf-8'}).stdout);
// find value in lso
// ....
// print lso
console.log(JSON.stringify(lso, null, 2));

but this is still a bad solution, the convert step should go

it should be even easier with

// darwin os: get default browser name
// use the `defaults` tool https://ss64.com/osx/defaults.html
// defaults accepts short names for lsf
// https://superuser.com/a/20857/951886
const child_process = require('child_process');
const lsf = 'com.apple.LaunchServices';
const keyOfDefaultBrowser = 'TODO_key_path_here';
const val = child_process.spawnSync(
  'defaults', ['read', lsf, keyOfDefaultBrowser]).stdout;

the standard API function is LSCopyDefaultApplicationURLForURL
(this is used by electron to get default apps)
but its closed source, so we cant use that to get the key path

whos got a darwin machine?
what is the structure of com.apple.LaunchServices?

edit: here is a sample output from defaults read
output format is NeXTSTEP plist format

$ defaults read ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist
{
    LSHandlers =     (
                {
            LSHandlerContentType = "org.7-zip.7-zip-archive";
            LSHandlerPreferredVersions =             {
                LSHandlerRoleViewer = "-";
            };
            LSHandlerRoleViewer = "com.aone.keka";
        },
                {
            LSHandlerContentType = "public.html";
            LSHandlerPreferredVersions =             {
                LSHandlerRoleAll = "-";
            };
            LSHandlerRoleAll = "com.apple.safari";
        }
    );
}

ugly! and expensive to parse

so back to
plutil -convert json -o - com.apple.LaunchServices
then the minimal solution should be
(using the logic from default-browser-id)

// darwin os: get default browser name
// use the `plutil` tool https://www.manpagez.com/man/1/plutil/
const child_process = require('child_process');
const getDefaultBrowserOnDarwin = () => (
	JSON.parse(child_process.execSync(
		"plutil -convert json -o - ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist",
		{maxBuffer: 1/0, encoding: "utf-8"}
	)).LSHandlers.filter(h => h.LSHandlerURLScheme == "http")
	.map(h => h.LSHandlerRoleAll).filter(Boolean)[0]
	|| 'com.apple.Safari'
);

that is 414 bytes of code : P

told ya, we can do this in a 1 KB bundle

Failed identifying Firefox on Windows

It seems that the regexp no longer matches Firefox:

C:\Users\eight04>reg QUERY HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice /v ProgId

HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice
    ProgId    REG_SZ    FirefoxURL-6F193CCC56814779

I'm using Firefox Nightly on Windows 10.

Deprecated transitive dep on `cross-spawn-async`

cross-spawn-async is deprecated and replaced by cross-spawn. This dependency comes indirectly from xdg-default-browser which hasn't been updated in a few years.

default-browser 3.1.0
└─┬ xdg-default-browser 2.1.0
  └─┬ execa 0.2.2
    └── cross-spawn-async 2.2.5

Not a big deal, just something I noticed.

Support for windows

It seems like we could get the default browser on Windows by running a command like

reg QUERY HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice /v ProgId

Which prints output like

HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice
    ProgId    REG_SZ    ChromeHTML

Or

HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice
    ProgId    REG_SZ    MSEdgeDHTML

We wouldn't be able to get a similarly compatible id, as far as I can see, but we could categorize and retrieve the name. E.g: https://github.com/microsoft/vscode-js-debug/blob/master/src/common/defaultBrowserProvider.ts

Would you be open to a PR to add support for this?

TypeError: undefined is not a function (OS X 10.9.1)

Install via: npm install --save default-browser

$ node -v
v0.10.24

$ node
> var defaultBrowser = require('default-browser');
undefined
> defaultBrowser();
undefined
>
/Users/Juanito/node_modules/default-browser/index.js:16
            cb(null, {
            ^
TypeError: undefined is not a function
    at /Users/Juanito/node_modules/default-browser/index.js:16:4
    at /Users/Juanito/node_modules/default-browser/node_modules/bundle-name/index.js:12:3
    at ChildProcess.exithandler (child_process.js:635:7)
    at ChildProcess.EventEmitter.emit (events.js:98:17)
    at maybeClose (child_process.js:735:16)
    at Socket.<anonymous> (child_process.js:948:11)
    at Socket.EventEmitter.emit (events.js:95:17)
    at Pipe.close (net.js:466:12)

Then it quits node, back to terminal.

Extract CLI into own module

Was going to do a PR to promisify this and thought you might want to extract the CLI before doing a major release.

Doesn't seem to work on OSX Yosemite?

Hi @sindresorhus,

I was trying to create a cross-platform default-browser package ( https://github.com/jakub-g/x-default-browser ) and wanted to reuse yours on Mac. However it doesn't seem to work for me.

I'm checking on OSX 10.10.1. I npm install-ed default-browser locally
I changed default browser to Chrome / Firefox from within the browsers.
When I use open http://example.org from command line it uses the proper browser.
But when I run node ./default-browser/cli.js it always prints Safari.
Am I doing something wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.