// ==UserScript== // @id 4pda.ru-QMS-RTC@scriptish // @name 4pdaQmsRtc // @version 1.1.1 // @namespace https://greasyfork.org/users/23 // @author kilowatt36@4pda.ru // @description Добавляет возможность передачи файлов напрямую между пользователями на сайте 4pda.ru // @require https://cdn.firebase.com/js/client/1.0.6/firebase.js // @require https://greasyfork.org/scripts/348/code.user.js // @include http://4pda.ru/forum/index.php?act=qms* // @run-at document-end // @downloadURL none // ==/UserScript== $ = unsafeWindow.$; // нужен jQuery именно из библиотеки QMS, потому что потребуется управлять событиями, связанными с ajax window.Firebase = Firebase; // в библиотеке RTCMultiConnection автор обращается не к Firebase, а к window.Firebase. Так что вот так. var connection; var iwanttosendfile = false; var sessions = { }; var progressHelper = { }; function HangHandlers_dialog() { // Создать UI и повесить все необходимые обработчики на экране диалога inputFile = $(''); button = $(''); $('#btn-bb-codes').before('Отправить файл: ').before(inputFile).before(button); button.click(function() { // Нажатие на кнопку "отправить" if (Object.getOwnPropertyNames(connection.peers).length === 1) { // Если еще ни одной сессии не было создано, создание сессии, после которой автоматом пойдет отправка файла var sessionName = '4pdaSession'; connection.extra = { 'session-name': sessionName || 'Anonymous' }; connection.sessionid = sessionName || 'Anonymous'; connection.maxParticipantsAllowed = 1; printToChat('Отправка файла', 'Ожидание соединения с собеседником...'); connection.open(connection.channel); iwanttosendfile = true; } else {// если сессия уже есть, просто отправить connection.send($('#4pdaQmsRTC')[0].files[0]); } }); $('.icon-back-up').click(HangHandlers_topicchoise); // При щелчке на возврат надо опять расставлять обработчики } function HangHandlers_topicchoise() { // Повесить все необходимые обработчики на экране выбора темы $(document).ajaxComplete(function() { // Чтобы повесить событие на НОВЫЙ список тем, который загрузится через ajax $('#threads-form .list-group-item.text-overflow').each(function(){ $(this).click(function() { // щелчок по теме $(document).ajaxComplete(function() { // После загрузки окна диалога HangHandlers_dialog(); $(document).off('ajaxComplete'); }); }); }); $(document).off('ajaxComplete'); // чтобы функция выше больше не срабатывала при каждом ajax запросе }); } function createConnection(peer) { // создание нового connection в соответствии с id собеседника connection = new RTCMultiConnection([peer, $('.icon-profile').attr('href').split('=')[1]].sort().join('_')); connection.session = { data: true }; connection.autoSaveToDisk = false; sessions = { }; progressHelper = { }; iwanttosendfile = false; connection.onNewSession = function(session) { if (sessions[session.sessionid]) return; sessions[session.sessionid] = session; console.log('new session: '+session.extra['session-name']); connection.join(session); // при каждом создании сессии присоединяемся к ней, не спрашивая разрешения (это же удобно!) }; connection.onmessage = function(e) { console.debug(e.userid, 'posted', e.data); console.log('latency:', e.latency, 'ms'); }; connection.onclose = function(e) { console.log('Data connection is closed between you and ' + e.userid); }; connection.onleave = function(e) { console.log(e.userid + ' left the session.'); }; // когда собеседник подтверждает наш запрос на присоединение к сесси (а это делается автоматически) connection.onopen = function() { if (iwanttosendfile) { connection.send($('#4pdaQmsRTC')[0].files[0]); $('#4pdaQmsRTC').val(''); } iwanttosendfile = false; //console.log('connection.onopen'); }; connection.onFileProgress = function(chunk) { var helper = progressHelper[chunk.uuid]; helper.progress.value = chunk.currentPosition || chunk.maxChunks || helper.progress.max; updateLabel(helper.progress, helper.label); }; connection.onFileStart = function(file) { var div = document.createElement('div'); div.title = file.name; div.innerHTML = ' '; printToChat('Передача файла',div); progressHelper[file.uuid] = { div: div, progress: div.querySelector('progress'), label: div.querySelector('#RTCLabel') }; progressHelper[file.uuid].progress.max = file.maxChunks; }; connection.onFileEnd = function(file) { progressHelper[file.uuid].div.innerHTML = '' + file.name + ''; }; function updateLabel(progress, label) { if (progress.position == -1) return; var position = +progress.position.toFixed(2).split('.')[1] || 100; label.innerHTML = position + '%'; } connection.connect(connection.channel); } function printToChat(title, text) {// добавление ложного "нового сообщения" в чат var new_row = $( '