ASL Smartsign Trigger

ASL Test 131018

目前為 2015-03-05 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        ASL Smartsign Trigger
// @namespace   http://m3.cip.gatech.edu/d/ogoldbart3/
// @description ASL Test 131018
// @version     24
// @include http://*/*
// @include https://*/*
// @include chrome://*/*
// @grant metadata
// @grant GM_xmlhttpRequest
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// ==/UserScript==


$(document).ready(function() {
    $('body').append('<div id="lightbox" style="display:none;position:fixed;top:0;center:0;width:100%;height:100%;background:rgba(0,0,0,0.6);text-align:center;z-index:9999"></div>');
    //$('html').prepend('<div id="searchwindow" style="position:fixed;width:15%;height:10%;top:0;right:0;text-align:center;z-index:8888"> <table><tr><td><img src="http://m3.cip.gatech.edu/d/ogoldbart3/w/hosting/c/ssdlogo.JPG" style="vertical-align: top" width="49" height="47"></td><td><textarea id="triggertext" rows="2" cols="20">SMARTSignDictionary</textarea> <button id="triggerbutton">Trigger</button></td></td></table></div>');
    $('html').prepend('<div id="searchwindow" style="position:fixed;width:15%;height:10%;top:0;left:0;text-align:center;z-index:8888"> <table><tr><td></td><td><textarea id="triggertext" rows="1" cols="18">SMARTSignDictionary</textarea></td></td></table></div>');


    $("#triggertext").keyup(function(event){
        if(event.keyCode == 13){
            //$("#triggerbutton").click();
            var t = $("#triggertext").val();
            t = t.replace(/'s/g,"");
            t = t.replace(/'d/g,"");
            t = t.replace(/~~/g,"");
            t = t.replace(/~/g," ");
            t = t.replace(/_/g," ");
            t = t.replace(".", "");
            t = t.replace(",","");
            t = t.replace("!", "");
            t = t.replace("?","");
            t = t.replace(";","");
            t = t.replace(",", "");
            t = t.replace("_","");
            console.log( "testing" );
            console.log( $("#triggertext").val() );
            $("#searchwindow").hide();
            //$("#triggertext").hide();
            $("#triggertext").val( "" );
            showVideos( t );

            //$("#searchwindow").hide();
        }

    });



    $("#searchwindow").click(function() {
        // Gets click
        $("#triggertext").val( "" );

    });

    //showVideos(document.getElementById(\'triggertext\').value)
    console.log("ex3");


    $("body").dblclick(function() {
        // Gets clicked on word (or selected text if text is selected)
        var t = '';
        var s = window.getSelection();
        if (s) {
            if (s.isCollapsed) //nothing is selected. Only clicked.
            {

                s.modify('move', 'forward', 'character');
                s.modify('move', 'backward', 'word');
                s.modify('extend', 'forward', 'word');

                //s.modify('move', 'forward', 'character'); //clear selection
                console.log("happened " + s.toString() );

                t = s.toString();
                t = t.replace(/'s/g,"");
                t = t.replace(/'d/g,"");
                t = t.replace(/~~/g,"");
                t = t.replace(/~/g," ");
                t = t.replace(/_/g," ");
                t = t.replace(".", "");
                t = t.replace(",","");
                t = t.replace(";","");
                t = t.replace("!", "");
                t = t.replace("?","");
                t = t.replace(",", "");
                t = t.replace("_","");

                s.modify('move', 'forward', 'character'); //clear selection
            }
        }
        console.log('http://smartsign.imtc.gatech.edu/videos?keywords=' + t);

        //Ajax call to database, input = t, fails in function, and won't return data
        //however, data is visible, and succeeded in the GET command when viewed
        //from within browser's network tab of console

        if ( ( t !== "" )&&( t.length <= 100 ) ) {
            var idArray;

            t.replace("~", " ");

            showVideos( t );


        }
    });

    $("body").click(function() {
        // Gets clicked on word (or selected text if text is selected)
        var t = '';
        var s = window.getSelection();
        if (s) {
            if (s.isCollapsed) //nothing is selected. Only clicked.
            {

            } else {
                console.log("not happened " + s.toString() );

                t = s.toString();

                t = t.trim();
                t = t.replace(/'s/g,"");
                t = t.replace(/'d/g,"");
                t = t.replace(/~~/g,"");
                t = t.replace(/~/g," ");
                t = t.replace(/_/g," ");
                t = t.replace(".", "");
                t = t.replace(",","");
                t = t.replace(";","");
                t = t.replace("!", "");
                t = t.replace("?","");
                t = t.replace(",", "");
                t = t.replace("_","");

            }
        }
        console.log('http://smartsign.imtc.gatech.edu/videos?keywords=' + t);

        //Ajax call to database, input = t, fails in function, and won't return data
        //however, data is visible, and succeeded in the GET command when viewed
        //from within browser's network tab of console

        if ( ( t !== "" )&&( t.length <= 100 ) ) {
            var idArray;


            t.replace("~", " ");

            showVideos( t );

        }
    });



});

// get video display order keyword, if present - pattern is {#}
function getVideoOrder(vid) {
    var keywords = vid.keywords,
        orderKey, order = 999;

    orderKey = keywords.filter(function(element) {
        return element.match(/^\{\d+\}$/) !== null;
    });
    if (orderKey.length > 0) {
        order = parseInt(orderKey[0].slice(1,-1), 10);
    }
    return order;
}

function showVideos( t ) {

    console.log( "getting to here with " + t);
    $.ajax({
        method: "GET",
        url: "http://smartsign.imtc.gatech.edu/videos?keywords=" + t,
        dataType: 'jsonp',
        success: function(response) {
            console.log( response );
            console.log( response[0] );

            //var data = $.parseJSON(response.responseText);
            var data = response;

            // sort data array
            data.sort(function(a, b) {
                return getVideoOrder(a) - getVideoOrder(b);
            });

            var idArray = new Array(data.length);

            for ( var i = 0; i < data.length; i++ ) {
                idArray[i] = '<iframe width="640" height="360" align:center src="http://www.youtube.com/embed/' + data[i]['id'] + '?rel=0"> </iframe>';
                console.log( idArray[i] );
            }

            console.log( data.length);

            var htmlString = '<div style="width:100%;position=absolute;text-align:center"> <div style="position:relative;left:25%;width:640px;text-align:left;">';

            for(var k=0; k<idArray.length; k++) {
                htmlString += idArray[k];
            }

            $('#lightbox')
                .html(htmlString)
                .css({"line-height":($(window).height()*0)+"px", "overflow":"auto", "display":"block"})
                .fadeIn('fast')
                .on('click', function(){
                    $(this).fadeOut('fast');
                });
            //console.log( htmlString );
            //console.log("running fine");
            //console.log("T = " + t);



            $('#searchwindow').hide();
        }
    });
    // $.ajax({
    //     method: "GET",
    //     dataType: 'jsonp',
    //     url: "http://smartsign.imtc.gatech.edu/videos?keywords=" + t,
    //     onload: function(response) {
    //         var data = $.parseJSON(response.responseText);

    //         idArray= new Array(data.length);
    //         orderedIdArray= new Array(data.length);




    //         for ( var i = 0; i < data.length; i++ ) {
    //             idArray[i] = '<iframe width="420" height="345" align:right src="http://www.youtube.com/embed/' + data[i]['id'] + '?rel=0"> </iframe>';
    //             console.log( idArray[i] );

    //         }

    //         console.log( data.length);

    //         var htmlString = '<div style="width:100%;position=absolute;text-align:right"> <div style="position:relative;left:72%;width:420px;text-align:left;">';



    //         var last = idArray.length - 1;
    //         var currnumber;

    //         for(var j=0; j<idArray.length; j++) {
    //             if ( data[j]['keywords'] ) {
    //                 if ( data[j]['keywords'][ data[j]['keywords'].length - 1 ].charAt(0) == "{" ) {
    //                     //console.log( data[j]['keywords'][ data[j]['keywords'].length - 1 ] );

    //                     currnumber = data[j]['keywords'][ data[j]['keywords'].length - 1 ];
    //                     currnumber = currnumber.slice( 1, currnumber.length - 1 );
    //                     console.log( currnumber );
    //                     orderedIdArray[ currnumber ] = idArray[j];
    //                     //orderedId[ 
    //                 }
    //                 else { 
    //                     console.log( "not found" );
    //                     orderedIdArray[ last ] = idArray[j];
    //                     last--;
    //                 }
    //             } 
    //         }

    //         for(var k=0; k<idArray.length; k++) {

    //             htmlString += orderedIdArray[k];  
    //         }

    //         $('#lightbox')
    //         .html(htmlString + '</div></div>')
    //         .css({"line-height":($(window).height()*0)+"px", "overflow":"auto", "display":"block"})
    //         .fadeIn('fast')
    //         .live('click', function() {
    //             $(this).fadeOut('fast');
    //             $('#searchwindow').show();
    //         });
    //         //console.log( htmlString );
    //         console.log("running fine");
    //         console.log("T = " + t);



    //     	$('#searchwindow').hide();
    //     }

    // });
}