Skip to content

Instantly share code, notes, and snippets.

@H7-25
Created January 7, 2021 13:31
Show Gist options
  • Save H7-25/47bfec7732132f5fdc1168d0755efa7a to your computer and use it in GitHub Desktop.
Save H7-25/47bfec7732132f5fdc1168d0755efa7a to your computer and use it in GitHub Desktop.
kiwiirc identcookie plugin
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