1.5.8 ā€¢ Published 2 years ago

react-browser-navigator v1.5.8

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

A Single React Module to Access Browser Navigator Properties

This package serves as the React implementation of the Navigator interface (windows.navigator). Among other things, the Navigator interface allows us to get useful information out of the browser such as network connection (onLine) and the geographic coordinates of the browser (geoLocation).

Properties you can use

šŸ”Œ Is there internet connection?
šŸŒŽ What's the location of the user?
šŸ—£ļø What language(s) the user's computer support?
šŸ’» What kind computer does the user use?

GIF

https://i.imgur.com/HbGVRcM.gif

dev.to Article

Read a short introduction about this new react-browser-navigator module: https://dev.to/linecept/access-to-location-network-status-and-other-browser-provided-properties-in-react-24fn

Installation

npm install react-browser-navigator

Usage

Quick Example

// import the module
import useNavigator from "react-browser-navigator";

function App() {
  // list accessible navigator properties (find them all below in a table)
  let { networkStatus } = useNavigator();

  return (
    <div>
      {networkStatus === true ? (
        <span>We are online!</span>
      ) : (
        <span>We are offline!</span>
      )}
    </div>
  );
}

Property Based Examples

The following examples are giving ideas how the module can be used. Note: for some examples we use lodash's isNull.

networkStatus

Implements Navigator.onLine.

// import the module
import useNavigator from "react-browser-navigator";

function App() {
  // importing the property
  let { networkStatus } = useNavigator();

  // using the property as a state within the render
  return (
    <div>
      {networkStatus === true ? (
        <span>We are online!</span>
      ) : (
        <span>We are offline!</span>
      )}
    </div>
  );
}

geoLocation

Implements Navigator.getCurrentPosition. You can use the entire object, but for the sake of simplicity we just show the latitude and longitude here!

// import the module
import useNavigator from "react-browser-navigator";

function App() {
  // importing the property
  let { getCurrentPosition } = useNavigator();

  // you can use it within the useEffect hook OR simply print the
  // string into the return statement
  useEffect(() => {
    if (!isNull(getCurrentPosition)) {
      console.log("getCurrentPosition", getCurrentPosition);
    }
  }, [getCurrentPosition]);

  return (
    <div>
      <span>Latidude:</span> {getCurrentPosition?.coords.latitude}
      <br />
      <br />
      <span>Longitude:</span> {getCurrentPosition?.coords.longitude}
    </div>
  );
}

language

Implements Navigator.language. The preferred language of the user, usually the language of the browser UI. The null value is returned when this is unknown. This gives back a simple string.

// import the module
import useNavigator from "react-browser-navigator";

function App() {
  // importing the property
  let { language } = useNavigator();

  // you can use it within the useEffect hook OR simply print the
  // string into the return statement
  useEffect(() => {
    if (!isNull(language)) {
      console.log("language", language);
    }
  }, [language]);

  return (
    <div>
      <span>Language:</span> {language}
    </div>
  );
}

languages

Implements Navigator.languages. Languages known to the user, by order of preference. This gives back an array of strings.

// import the module
import useNavigator from "react-browser-navigator";

function App() {
  // importing the property
  let { languages } = useNavigator();

  // you can use it within the useEffect hook OR simply print the
  // string into the return statement
  useEffect(() => {
    if (!isNull(languages)) {
      console.log("languages", languages);
    }
  }, [language]);

  return (
    <div>
      <span>Languages:</span> {languages}
    </div>
  );
}

userAgent

Implements Navigator.userAgent. User agent information (a string) for the current browser.

// import the module
import useNavigator from "react-browser-navigator";

function App() {
  // importing the property
  let { userAgent } = useNavigator();

  // you can use it within the useEffect hook OR simply print the
  // string into the return statement
  useEffect(() => {
    if (!isNull(userAgent)) {
      console.log("userAgent", userAgent);
    }
  }, [userAgent]);

  return (
    <div>
      <span>userAgent:</span> {userAgent}
    </div>
  );
}

userAgentData

Implements Navigator.userAgentData. Gives access to information about the browser and operating system of the user. This gives an object.

// import the module
import useNavigator from "react-browser-navigator";

function App() {
  // importing the property
  let { userAgentData } = useNavigator();

  // you can use it within the useEffect hook OR simply print the
  // string into the return statement
  useEffect(() => {
    if (!isNull(userAgentData)) {
      console.log("userAgentData", userAgentData);
    }
  }, [userAgentData]);

  return (
    <div>
      <span>userAgentData:</span> {userAgentData}
    </div>
  );
}

vendor

Implements Navigator.vendor. Always either "Google Inc.", "Apple Computer, Inc.", or (in Firefox) the empty string. This gives back a string.

// import the module
import useNavigator from "react-browser-navigator";

function App() {
  // importing the property
  let { vendor } = useNavigator();

  // you can use it within the useEffect hook OR simply print the
  // string into the return statement
  useEffect(() => {
    if (!isNull(vendor)) {
      console.log("vendor", vendor);
    }
  }, [vendor]);

  return (
    <div>
      <span>vendor:</span> {vendor}
    </div>
  );
}

Already Mapped Properties

StatusPropertyNoteTypeExample
āœ…networkStatusDetects if browser is offline or online.Booleanāœ…
āœ…getCurrentPositionGeolocation of browser.Object (GeolocationPosition including coords and timestamp)āœ…
āœ…languageThe preferred language of the user, usually the language of the browser UI. The null value is returned when this is unknown.String or nullāœ…
āœ…languagesLanguages known to the user, by order of preference.Array of Stringāœ…
āœ…userAgentUser agent string for the current browser.Stringāœ…
āœ…userAgentDataGives access to information about the browser and operating system of the user.Objectāœ…
āœ…vendorAlways either "Google Inc.", "Apple Computer, Inc.", or (in Firefox) the empty string.Stringāœ…

Roadmap

We are planning to add more and more properties as well as other features.

All Properties

No.StatusPropertyNotesSource
1āŒconnectionProvides a NetworkInformation object containing information about the network connection of a device.https://developer.mozilla.org/en-US/docs/Web/API/Navigator/connection
2āŒcookieEnabledReturns false if setting a cookie will be ignored and true otherwise.https://developer.mozilla.org/en-US/docs/Web/API/Navigator/cookieEnabled
3āŒpresentationReturns a reference to the Presentation API.https://developer.mozilla.org/en-US/docs/Web/API/Navigator/presentation
4āŒserviceWorkerProvides access to registration, removal, upgrade, and communication for associated documents.https://developer.mozilla.org/en-US/docs/Web/API/Navigator/serviceWorker
5āŒstorageReturns the StorageManager object used for estimating available storage.https://developer.mozilla.org/en-US/docs/Web/API/Navigator/storage
6āŒwebdriverIndicates whether the user agent is controlled by automation.https://developer.mozilla.org/en-US/docs/Web/API/Navigator/webdriver
7āŒdoNotTrackReports the value of the user's do-not-track preference.https://developer.mozilla.org/en-US/docs/Web/API/Navigator/doNotTrack
8āŒoscpuReturns a string that represents the current operating system.https://developer.mozilla.org/en-US/docs/Web/API/Navigator/oscpu
9āŒpluginsReturns a PluginArray listing the plugins installed in the browser.https://developer.mozilla.org/en-US/docs/Web/API/Navigator/plugins
10āŒsetAppBadge()Sets a badge on the icon associated with this app and returns a Promise that resolves with undefined.https://developer.mozilla.org/en-US/docs/Web/API/Navigator/setAppBadge
11āŒshare()Invokes the native sharing mechanism of the current platform.https://developer.mozilla.org/en-US/docs/Web/API/Navigator/share
12āŒvibrate()Causes vibration on devices with support for it. Does nothing if vibration support isn't available.https://developer.mozilla.org/en-US/docs/Web/API/Navigator/vibrate
13āŒgetUserMedia()After permission, returns the audio or video stream associated to a camera or microphone.https://developer.mozilla.org/en-US/docs/Web/API/Navigator/getUserMedia

Additional Features

StatusItemNotes
Partially DoneExamplesExamples for already addded properties.
āŒAdding TestsTest Coverage Creating and adding test cases.
āŒMoving To TypescriptMoving the codebase to TypeScript.

Credits

Special thanks to:

  • MDN Navigator - the Navigator represents the state and the identity of the user agent. It allows scripts to query it and to register themselves to carry on some activities.

Support the Authors

Do you like this project? Star the repository, spread the word - it really helps. You may want to follow me on Twitter.

License

MIT License

Copyright (c) 2022 Linecept, David Kutas, Akos Toth.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1.5.8

2 years ago

1.4.6

2 years ago

1.4.5

2 years ago

1.4.4

2 years ago

1.4.3

2 years ago

1.4.2

2 years ago

1.5.7

2 years ago

1.5.6

2 years ago

1.5.5

2 years ago

1.5.4

2 years ago

1.5.3

2 years ago

1.5.2

2 years ago

1.5.1

2 years ago

1.5.0

2 years ago

1.4.9

2 years ago

1.4.8

2 years ago

1.4.7

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.9

2 years ago

1.3.8

2 years ago

1.3.7

2 years ago

1.3.6

2 years ago

1.3.5

2 years ago

1.3.4

2 years ago

1.3.3

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.9

2 years ago

1.2.8

2 years ago

1.2.7

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago