1/**@license 2 * sysend.js - send messages between browser windows/tabs 3 * Copyright (C) 2014 Jakub Jankiewicz <https://jcubic.pl> 4 * 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <https://www.gnu.org/licenses/>. 17 * 18 * The idea for this implementation came from this StackOverflow question: 19 * https://stackoverflow.com/q/24182409/387194 20 */ 21var sysend = (function() { 22 // we use prefix so `foo' event don't collide with `foo' locaStorage value 23 var uniq_prefix = '___sysend___'; 24 // we use id because storage event is not executed if message was not 25 // changed, and we want it if user send same object twice (before it will 26 // be removed) 27 var id = 0; 28 function get(key) { 29 return localStorage.getItem(uniq_prefix + key); 30 } 31 function set(key, value) { 32 localStorage.setItem(uniq_prefix + key, value); 33 } 34 function remove(key) { 35 localStorage.removeItem(uniq_prefix + key); 36 } 37 function to_json(input) { 38 var obj = [id++]; 39 // undefined in array get stringified as null 40 if (typeof input != 'undefined') { 41 obj.push(input); 42 } 43 return JSON.stringify(obj); 44 } 45 function from_json(json) { 46 return JSON.parse(json); 47 } 48 // object with user events as keys and values arrays of callback functions 49 var callbacks = {}; 50 window.addEventListener('storage', function(e) { 51 // get user key 52 var key = e.key.replace(new RegExp('^' + uniq_prefix), ''); 53 if (callbacks[key]) { 54 var obj = JSON.parse(get(key)); 55 if (obj) { 56 // don't call on remove 57 callbacks[key].forEach(function(fn) { 58 fn(obj[1], key); 59 }); 60 } 61 } 62 }, false); 63 var timer; 64 return { 65 broadcast: function(event, message) { 66 set(event, to_json(message)); 67 clearTimeout(timer); 68 //clean up localstorage 69 timer = setTimeout(function() { 70 remove(event); 71 }, 1000); 72 }, 73 on: function(event, fn) { 74 if (!callbacks[event]) { 75 callbacks[event] = []; 76 } 77 callbacks[event].push(fn); 78 }, 79 off: function(event, fn) { 80 if (callbacks[event]) { 81 if (fn) { 82 for (var i=callbacks[event].length; i--;) { 83 if (callbacks[event][i] == fn) { 84 callbacks[event].splice(i, 1); 85 } 86 } 87 } else { 88 for (var i=callbacks[event].length; i--;) { 89 callbacks[event].splice(i, 1); 90 } 91 } 92 } 93 } 94 }; 95})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.