Created
June 13, 2019 18:19
-
-
Save H7-25/3fdc86fea08c63eada50993fc1bfa0bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function randString(n) { | |
if(!n) | |
{ | |
n = 5; | |
} | |
var text = ''; | |
var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | |
for(var i=0; i < n; i++) | |
{ | |
text += possible.charAt(Math.floor(Math.random() * possible.length)); | |
} | |
return text; | |
} | |
// Cookies | |
function createCookie(name, value, days) { | |
if (days) { | |
var date = new Date(); | |
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); | |
var expires = "; expires=" + date.toGMTString(); | |
} | |
else var expires = ""; | |
document.cookie = name + "=" + value + expires + "; path=/"; | |
} | |
function readCookie(name) { | |
var nameEQ = name + "="; | |
var ca = document.cookie.split(';'); | |
for (var i = 0; i < ca.length; i++) { | |
var c = ca[i]; | |
while (c.charAt(0) == ' ') c = c.substring(1, c.length); | |
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); | |
} | |
return null; | |
} | |
function eraseCookie(name) { | |
createCookie(name, "", -1); | |
} | |
function getUsername() { | |
var cookie = readCookie('cookie_name'); | |
if (!cookie) { | |
cookie = randString(9); | |
createCookie('cookie_name', cookie, 30); | |
} | |
var username = cookie; | |
if (!'localStorage' in window) { | |
return username; | |
} | |
username = window.localStorage.getItem('username'); | |
if (!username) { | |
username = randString(9); | |
window.localStorage.setItem('username', username); | |
} | |
return username; | |
} | |
kiwi.plugin('ident', function(kiwi) { | |
kiwi.state.$on('network.new', function(event) { | |
event.network.username = getUsername(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment