Home

Awesome

<img src="https://raw.githubusercontent.com/wingify/across-tabs/master/across-tabs.png" alt="Across tabs" width="50" height="50"/> AcrossTabs - Easy communication between cross-origin browser tabs.

npm version npm Build Status Coverage Status

NPM

<a href="https://news.ycombinator.com/item?id=14041400"><img src="https://raw.githubusercontent.com/wingify/across-tabs/master/images/hn.png" width="150" height="20"/></a> <a href="https://www.producthunt.com/posts/across-tabs"><img src="https://raw.githubusercontent.com/wingify/across-tabs/master/images/product_hunt.png" width="100" height="20"/></a>

LIVE DEMO

Features

  1. Safely enables cross-origin communication among different browser tabs. Uses PostMessage API for communication.
  2. Easy to hook custom callback at various levels. Eg: executing a custom method in Child's tab on receiving a message from Parent tab.
  3. Option to provide data-tab-opener="name" attribute on the target link/button(which opens up a new tab), so that it remains to disable until Child tab initiates a handshake and is received by the Parent tab
  4. Fully fledged API to get information regarding the tabs(Parent and Child tabs) and other communication-related methods.
  5. Exports in a UMD format i.e. library works everywhere.
  6. Only ~4 KB gzipped.

Installation

  1. Via npm
$ npm install across-tabs
  1. Via bower
$ bower install across-tabs
  1. Via cdnjs, using <script> tag
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/across-tabs/1.0.0/across-tabs.min.js"></script>

Note: Use the desired version of the library in place of 1.0.0. Checkout cdnjs across-tabs.

Flow Diagram

<img src="https://raw.githubusercontent.com/wingify/across-tabs/master/parent-tab-communication.jpg" />

Explanation of diagram


Total Tabs Associated: 1 | Opened Tabs: 1 | Closed Tabs: 0



Total Tabs Associated: 2 | Opened Tabs: 2 | Closed Tabs: 0



Total Tabs Associated: 2 | Opened Tabs: 1 | Closed Tabs: 1



Total Tabs Associated: 3 | Opened Tabs: 2 | Closed Tabs: 1


Usage

Create an instance / reference before using.

Opener(Parent) tab

var config = {
  onHandshakeCallback: function () { ... },
  onPollingCallback: function () { ... },
  onChildCommunication: function () { ... }
}
var parent = new AcrossTabs.Parent(config);

Constructor accepts a configurable Object with the following keys.

Config Keysdefaultaccepts
heartBeatInterval500 msecA number representing milliseconds
removeClosedTabsfalseBoolean
shouldInitImmediatelytrueBoolean
onHandshakeCallbackUndefinedFunction as callback
onChildCommunicationUndefinedFunction as callback
onPollingCallbackUndefinedFunction as callback
origin'*'String(url)
parseJSON.parseFunction
stringifyJSON.stringifyFunction

New(Child tab)

var config = {
  onReady: onReady,
  onInitialize: onInitialize,
  isSiteInsideFrame: false, // dont set if not required
  handshakeExpiryLimit: 4000, // msec
  onParentDisconnect: onParentDisconnect,
  onParentCommunication: onParentCommunication
};
var child =  new AcrossTabs.Child(config);

Constructor accepts a configurable Object with the following keys.

Config Keysdefaultaccepts
handshakeExpiryLimit5000 msecA number representing milliseconds
isSiteInsideFramenullIf child tab has actual site in a fram
onReadyUndefinedFunction as callback
onInitializeUndefinedFunction as callback
onParentDisconnectUndefinedFunction as callback
onParentCommunicationUndefinedFunction as callback
origin'*'String(url)
parseJSON.parseFunction
stringifyJSON.stringifyFunction

Example is included in the example folder. Vanilla JS and Vue js versions are there to test out. Note: Run npm install if you wish to run vuejs example since the example needs the vue-js library to work.

API

Refer above section on how to create an instance before hitting API methods.

Opener Tab(Parent) Methods
New Tab(Child) Methods

Browser Support

Tested in Chrome, Firefox and IE(versions >= 9).

Sites using across-tabs

Development Stack

Process

ES6 source files
       |
       |
    webpack
       |
       +--- babel, eslint
       |
       o--- tests and covergae
       |
  ready to use
     library
  in UMD format

Scripts

Contributing

  1. Fork the repo on GitHub.
  2. Clone the repo on a machine.
  3. Execute npm install and npm run dev.
  4. Create a new branch <fix-typo> and do your work.
  5. Run npm run build to build dist files and npm run test to ensure all test cases are passing.
  6. Commit your changes to the branch.
  7. Submit a Pull request.

Note: If adding site to the list of sites using across-tabs, please mention where to verify this in the PR description.

Roadmap

Authors

Varun Malhotra @softvar

Copyright and license

The MIT license (MIT)

Copyright (c) 2017-2023 Wingify Software Pvt. Ltd.

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.