// ==UserScript==
// @name SE Chat custom notification sound
// @author Lekensteyn lekensteyn@gmail.com
// @version 1.0.0.0
// @namespace lekensteyn@gmail.com
// @description Customize the notification sound played in the StackExchange chat
// @include http://chat.stackexchange.com/*
// @license MIT/X11
// @website http://stackapps.com/q/2479/6969
// ==/UserScript==
(function (func) {
// helper for injecting the script into the page
var script = document.createElement("script");
// insert a script element...
script.appendChild(
// with a function which is called on document ready
document.createTextNode("$(" + func.toString() + ")")
);
// run the script after other scripts
document.body.appendChild(script);
})(function () {
// an element containing the audio player
var player = $("#jplayer");
var player_element = $(player[0]);
// holds the initial sound file
var original_sound;
// holds the current sound file
var sound_file;
// retrieves the customized audio file from a cookie
var get_preferred_file = function () {
return $.cookie("notify_sound");
};
var set_preferred_file = function (file) {
var save_data = file;
// do not waste space on saving the default sound
if (save_data == original_sound) {
save_data = "";
}
// save the sound file
$.cookie("notify_sound", save_data, {
path: "/",
expires: 90
});
// and use the sound file
player.jPlayer("setFile", file);
};
// modifies the audio file if necessary
var set_custom_file = function (event, mp3_file, /*unused*/ ogg_file) {
sound_file = mp3_file;
if (!original_sound) {
original_sound = sound_file;
}
var preferred_file = get_preferred_file();
// if we have a custom sound file, mark it for use
if (preferred_file && mp3_file != preferred_file) {
player.jPlayer("setFile", preferred_file);
}
};
// catch requests to modify the audio file
player_element.bind("jPlayer.setFile", set_custom_file);
var customize_button = $("
");
// an image created from \u266b in GIMP (Ubuntu Font) crushed with
// http://tools.dynamicdrive.com/imageoptimizer/index.php
var button_image = "data:image/gif;base64,R0lGODlhEAAQAPMMAAAAAA0NDSMjIz" +
"w8PEdHR0xMTFZWVm9vb4eHh52dnbCwsNDQ0P////39/fT09ODg4CH5BAAAAAAALAAAA" +
"AAQABAAAAQ+kMnJmqV4HuWyB+CALN40JEUAEKXEMo7yljNTZ/WN5a1N6b9g5jHiZQQg" +
"gxBzCAhITUOhJ0ECDlQGUZFtRQAAOw==";
// stylize the button
customize_button.css({
"float": "left",
"margin-right": "5px",
"cursor": "pointer",
"height": "16px",
"width": "16px",
"background-image": "url(" + button_image + ")"
});
customize_button.attr("title", "set audio notification file");
customize_button.click(function (event) {
// prevent the popup from disappearing
event.stopPropagation();
// Create a popup at mouse position
var popup = popUp(event.pageX, event.pageY);
// enlarge the popup for the input field
popup.width(330);
$("
").text("Sound URL").appendTo(popup);
var url_input = $("").width(320).appendTo(popup);
// save and set the file name
var save_url = function () {
var new_sound = url_input.val();
// use the new sound URL if it's not empty
if (new_sound) {
set_preferred_file(new_sound);
popup.close();
} else { // if the new sound URL is empty, restore the original one
restore_url();
}
};
var save_and_play_sound = function () {
save_url();
player.jPlayer("play");
};
// provides a function for setting a URL
var url_chooser = function (url) {
return function () {
url_input.val(url);
save_and_play_sound();
};
};
url_input.val(sound_file);
$("