2015-12-11 12:00:28 +00:00
|
|
|
# bt
|
2015-12-16 23:32:36 +00:00
|
|
|
|
|
|
|
Manage commnunication between browser tabs.
|
|
|
|
this js lib can perform several actions on browser tabs like :
|
2015-12-24 03:35:55 +00:00
|
|
|
- (*new) search and kill zombies tabs (enable onload by default)
|
2015-12-16 23:32:36 +00:00
|
|
|
- append/rewrite/synchro node on all (other) tabs or a specific tab (and possibly on specific frame context) eventually with callback.
|
|
|
|
- reload all tabs or a specific tab with specified url or tab 's current url
|
|
|
|
- perform your custom actions on all tabs or specific tab
|
2015-12-11 12:00:28 +00:00
|
|
|
|
|
|
|
### require
|
|
|
|
|
2015-12-16 23:32:36 +00:00
|
|
|
html5 localStorage svan (pluie.org small vanilla jquery-like lib)
|
2015-12-11 12:00:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
### Initialize
|
|
|
|
|
2015-12-16 23:32:36 +00:00
|
|
|
$(document).ready(function() {
|
2015-12-24 03:35:55 +00:00
|
|
|
// $bt.zkillonload = false // disable zombkill cmd at statup
|
|
|
|
$bt.init(optionalCallback);
|
2015-12-11 12:00:28 +00:00
|
|
|
}
|
|
|
|
|
2015-12-13 02:53:48 +00:00
|
|
|
|
2015-12-11 12:00:28 +00:00
|
|
|
### Internal Commands
|
|
|
|
|
|
|
|
// append data on node to other browser tabs
|
2015-12-13 02:53:48 +00:00
|
|
|
$bt.append('#test', "<b>it's cool to append</b>");
|
2015-12-16 23:32:36 +00:00
|
|
|
|
2015-12-11 12:00:28 +00:00
|
|
|
// rewrite content on node to other browser tabs
|
2015-12-13 02:53:48 +00:00
|
|
|
$bt.html('#test', "<b>it's cool to rewrite</b>");
|
|
|
|
|
2015-12-16 23:32:36 +00:00
|
|
|
// append content to specific browser tab
|
|
|
|
$bt.append('#test', "<b>it's cool to rewrite</b>", null, null, '1449974562012');
|
|
|
|
|
|
|
|
// rewrite content to specified browser tab with callback
|
2015-12-24 23:34:11 +00:00
|
|
|
// callback must be define before command with method $bt.setCallback('callbackname', callback);
|
|
|
|
$bt.html('#test', "<b>it's cool to rewrite</b>", null, 'callbackname', '1449974562012');
|
2015-12-16 23:32:36 +00:00
|
|
|
|
|
|
|
// append content to specified browser tab on specific frame
|
|
|
|
$bt.append('#test', "<b>it's cool to rewrite</b>", 'frameName', null, '1449974562012');
|
2015-12-13 02:53:48 +00:00
|
|
|
|
|
|
|
// perform a node synchro to other browser tabs
|
|
|
|
$bt.sync('#test');
|
|
|
|
|
2015-12-16 23:32:36 +00:00
|
|
|
// perform a node synchro to specified browser tab on specific frame with callback
|
2015-12-24 23:34:11 +00:00
|
|
|
$bt.sync('#test', 'frameName', 'callbackname', '1449974562012');
|
2015-12-16 23:32:36 +00:00
|
|
|
|
2015-12-13 02:53:48 +00:00
|
|
|
// reload other browser tabs
|
|
|
|
$bt.reload();
|
|
|
|
|
2015-12-16 23:32:36 +00:00
|
|
|
// reload specific browser tab to specific url
|
|
|
|
$bt.reload(window.location.path+"?reloaded=1", '1449974562012');
|
2015-12-13 02:53:48 +00:00
|
|
|
|
2015-12-24 03:35:55 +00:00
|
|
|
// search and kill zombies tabs
|
|
|
|
$bt.zombkill(callback, timeout);
|
|
|
|
|
2015-12-13 02:53:48 +00:00
|
|
|
// get browser tab list
|
|
|
|
$bt.list;
|
|
|
|
|
|
|
|
// current browser tab id
|
|
|
|
$bt.id;
|
|
|
|
|
2015-12-11 12:00:28 +00:00
|
|
|
|
|
|
|
### Custom Commands
|
|
|
|
|
|
|
|
// define a new custom cmd
|
|
|
|
$bt.CMD_CUSTOM = "customCmd";
|
|
|
|
// treat custom command to other browser tabs
|
|
|
|
$bt.on = function(cmd) {
|
|
|
|
switch (cmd.name) {
|
|
|
|
case $bt.CMD_CUSTOM :
|
|
|
|
// do stuff
|
|
|
|
$bt.log("custom command `'+cmd.name+'` with `'+cmd.customKey+'`');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-24 23:34:11 +00:00
|
|
|
// it's also possible to define a before and after function :
|
|
|
|
$bt.before = function(cmd) {
|
|
|
|
$bt.log("i'm fired before every command");
|
|
|
|
}
|
|
|
|
|
|
|
|
$bt.after = function(cmd) {
|
|
|
|
$bt.log("i'm fired after every command");
|
|
|
|
}
|
|
|
|
|
2015-12-11 12:00:28 +00:00
|
|
|
// send a custom command to other browser tabs
|
|
|
|
$bt.send({ name : $bt.CMD_CUSTOM, customKey : 'customValue' });
|
|
|
|
|
|
|
|
|
2015-12-13 03:20:16 +00:00
|
|
|
### Bonus
|
|
|
|
|
|
|
|
// alias localStorage : clear|rem|get|set
|
2015-12-16 23:32:36 +00:00
|
|
|
$l
|
2015-12-13 03:20:16 +00:00
|
|
|
// alias json : str|obj
|
|
|
|
$j
|
|
|
|
|
|
|
|
|
2015-12-11 12:00:28 +00:00
|
|
|
### Demo
|
|
|
|
|
2015-12-13 01:20:07 +00:00
|
|
|
on chromium/chrome browser make sure to test on a web server (not directly file://)
|
2015-12-11 12:00:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
enjoy !
|