<commit_before>// ==UserScript==
// @name        Zendesk Chat Notifications
// @namespace   http://www.software-architects.at
// @description Adds desktop notifications, sound looping and always on to Zendesk chat.
// @match       https://*.zendesk.com/agent/*
// @grant       none
// @version     1.6
// @copyright   2014 software architects gmbh
// ==/UserScript==

var currentNotifications = [];
var chatsDiv = null;
var settingsDiv = null;
var loopTimer = null;
var enableDesktop = false;
var enableLoop = false;
var alwaysOn = false;
var alwaysOnTimer = null;
var onlineButton = null;

function getStorageOrDefault(key, defaultValue) {
    var text = localStorage.getItem(key);
    console.debug('ZendeskChat: getStorage for ' + key + ' = ' + text + ' (' + typeof text + ')');
    if (text === null) {
        console.debug('ZendeskChat: getStorage returning default ' + defaultValue + ' (' + typeof defaultValue + ')');
        return defaultValue;
    } else {
        var val = JSON.parse(text);
        console.debug('ZendeskChat: getStorage returning ' + val + ' (' + typeof val + ')');
        return val;
    }
}

function setStorage(key, val) {
    var text = JSON.stringify(val);
    console.debug('ZendeskChat: setStorage for ' + key + ' = ' + val + ' (' + typeof val + ') as ' + text + ' (' + typeof text + ')');
    localStorage.setItem(key, text);
}

function processChatsAdded(event) {
    if ((enableDesktop && Notification.permission === "granted") || enableLoop) {
        // find invite nodes
        var invites = chatsDiv.find('.ember-view.invite');
        $.each(invites, function( index, value ) {
        
            // only handle each request once
            if ($.inArray(value, currentNotifications) < 0) {
                var username = chatsDiv.find('a.username').text();
                var subject = chatsDiv.find('div.chat-subject').text();
                
                // only if we could gather all the info
                if (username && subject) {
                    console.debug('ZendeskChat: new chat request - notifying');
                    var title = "Chat request: " + username;
                    var options = {
                        body : subject,
                        icon : 'http://www.zendesk.com/favicon.ico'
                    };
                    
                    if (enableDesktop) {
                        var notification = new Notification(title, options);
                        notification.onclick = function(x) { window.focus(); };
                    }
                    currentNotifications.push(value);
                    
                    // sound looping
                    if (enableLoop && loopTimer === null) {
                        console.debug('ZendeskChat: looping sound');
                        loopTimer = window.setInterval(function () {
                            soundManager.sounds.invite.play();
                            console.debug('ZendeskChat: beeping');
                        }, 4000);
                    }
                    
                    // when the zendesk-internal notification closes
                    $(value).on("remove", function () {
                        console.debug('ZendeskChat: chat request ended');
                        currentNotifications.splice( $.inArray(this, currentNotifications), 1 );
                        if (currentNotifications.length === 0 && loopTimer !== null) {
                            console.debug('ZendeskChat: ending sound loop');
                            window.clearInterval(loopTimer);
                            loopTimer = null;
                        }
                        
                        notification.close();
                    });
                }
            }
        });
    }
}

function setAlwaysOn(value) {
    console.debug('ZendeskChat: setAlwaysOn' + value);
    if (value === true) {
        if (alwaysOnTimer === null) {
            alwaysOnTimer = window.setInterval(function () {
                if (onlineButton === null) {
                    onlineButton = $('#chat-header').find('button.ember-view.availability');
                    console.debug('ZendeskChat: setAlwaysOn discovered button ' + onlineButton)
                }
                
                
            }, 5000);
            console.debug('ZendeskChat: setAlwaysOn started timer');
        }
    } else {
        if (alwaysOnTimer !== null) {
            window.clearInterval(alwaysOnTimer);
            alwaysOnTimer = null;
            console.debug('ZendeskChat: setAlwaysOn removed timer');
        }
    }
}

function buildSettingsDiv() {
    var div = $('<div class="enable_checkbox" id="ZendeskChatNotificationsSettings" />');
    
    // add notify checkbox
    var notifications = $('<input type="checkbox" style="margin-left:10px;" />');
    notifications.prop('checked', enableDesktop);
    notifications.change(function() {
        enableDesktop = $(this).is(':checked');
        setStorage('ChatNotifications.Desktop', enableDesktop);
        if (enableDesktop && Notification.permission !== "granted" && Notification.permission !== 'denied') {
            console.debug('ZendeskChat: requesting notification permission');
            Notification.requestPermission(function (permission) {
                console.debug('ZendeskChat: permission: ' + permission);
                if(!('permission' in Notification)) {
                    Notification.permission = permission;
                }
            });
        }<commit_msg>immediately checking for online chat<commit_after>// ==UserScript==
// @name        Zendesk Chat Notifications
// @namespace   http://www.software-architects.at
// @description Adds desktop notifications, sound looping and always on to Zendesk chat.
// @match       https://*.zendesk.com/agent/*
// @grant       none
// @version     1.6
// @copyright   2014 software architects gmbh
// ==/UserScript==

var currentNotifications = [];
var chatsDiv = null;
var settingsDiv = null;
var loopTimer = null;
var enableDesktop = false;
var enableLoop = false;
var alwaysOn = false;
var alwaysOnTimer = null;
var onlineButton = null;

function getStorageOrDefault(key, defaultValue) {
    var text = localStorage.getItem(key);
    console.debug('ZendeskChat: getStorage for ' + key + ' = ' + text + ' (' + typeof text + ')');
    if (text === null) {
        console.debug('ZendeskChat: getStorage returning default ' + defaultValue + ' (' + typeof defaultValue + ')');
        return defaultValue;
    } else {
        var val = JSON.parse(text);
        console.debug('ZendeskChat: getStorage returning ' + val + ' (' + typeof val + ')');
        return val;
    }
}

function setStorage(key, val) {
    var text = JSON.stringify(val);
    console.debug('ZendeskChat: setStorage for ' + key + ' = ' + val + ' (' + typeof val + ') as ' + text + ' (' + typeof text + ')');
    localStorage.setItem(key, text);
}

function processChatsAdded(event) {
    if ((enableDesktop && Notification.permission === "granted") || enableLoop) {
        // find invite nodes
        var invites = chatsDiv.find('.ember-view.invite');
        $.each(invites, function( index, value ) {
        
            // only handle each request once
            if ($.inArray(value, currentNotifications) < 0) {
                var username = chatsDiv.find('a.username').text();
                var subject = chatsDiv.find('div.chat-subject').text();
                
                // only if we could gather all the info
                if (username && subject) {
                    console.debug('ZendeskChat: new chat request - notifying');
                    var title = "Chat request: " + username;
                    var options = {
                        body : subject,
                        icon : 'http://www.zendesk.com/favicon.ico'
                    };
                    
                    if (enableDesktop) {
                        var notification = new Notification(title, options);
                        notification.onclick = function(x) { window.focus(); };
                    }
                    currentNotifications.push(value);
                    
                    // sound looping
                    if (enableLoop && loopTimer === null) {
                        console.debug('ZendeskChat: looping sound');
                        loopTimer = window.setInterval(function () {
                            soundManager.sounds.invite.play();
                            console.debug('ZendeskChat: beeping');
                        }, 4000);
                    }
                    
                    // when the zendesk-internal notification closes
                    $(value).on("remove", function () {
                        console.debug('ZendeskChat: chat request ended');
                        currentNotifications.splice( $.inArray(this, currentNotifications), 1 );
                        if (currentNotifications.length === 0 && loopTimer !== null) {
                            console.debug('ZendeskChat: ending sound loop');
                            window.clearInterval(loopTimer);
                            loopTimer = null;
                        }
                        
                        notification.close();
                    });
                }
            }
        });
    }
}

function ensureOnlineChat() {
    if (onlineButton === null) {
        onlineButton = $('#chat-header').find('button.ember-view.availability');
        console.debug('ZendeskChat: setAlwaysOn discovered button');
    }
    
    if (onlineButton.hasClass('unavailable')) {
        console.debug('ZendeskChat: setAlwaysOn chat seems offline - clicking');
        onlineButton.click();
    }
}

function setAlwaysOn(value) {
    console.debug('ZendeskChat: setAlwaysOn' + value);
    if (value === true) {
        if (alwaysOnTimer === null) {
            alwaysOnTimer = window.setInterval(ensureOnlineChat, 5000);
            console.debug('ZendeskChat: setAlwaysOn started timer');
            ensureOnlineChat();
        }
    } else {
        if (alwaysOnTimer !== null) {
            window.clearInterval(alwaysOnTimer);
            alwaysOnTimer = null;
            console.debug('ZendeskChat: setAlwaysOn removed timer');
        }
    }
}

function buildSettingsDiv() {
    var div = $('<div class="enable_checkbox" id="ZendeskChatNotificationsSettings" />');
    
    // add notify checkbox
    var notifications = $('<input type="checkbox" style="margin-left:10px;" />');
    notifications.prop('checked', enableDesktop);
    notifications.change(function() {
        enableDesktop = $(this).is(':checked');
        setStorage('ChatNotifications.Desktop', enableDesktop);
        if (enableDesktop && Notification.permission !== "granted" && Notification.permission !== 'denied') {
            console.debug('ZendeskChat: requesting notification permission');
            Notification.requestPermission(function (permission) {
                console.debug('ZendeskChat: permission: ' + permission);
                if(!('permission' in Notification)) {
                    Notification.permission = permission;
                }
            });
        }
{'fraction_non_alphanumeric': 0.12137906522125466, 'fraction_numerical': 0.003376577216989515, 'mean_word_length': 1.758578431372549, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4489894', 'n_tokens_mistral': 2892, 'n_tokens_neox': 2712, 'n_words': 631}