Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Library: JIL

  1. #1
    New Coder
    Join Date
    Dec 2008
    Posts
    21
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Library: JIL

    JIL stands for jData Interface Library and it is a library for interacting with a jData host. Note: This script requires json2.js in the same directory as jil.js OR a native JSON-supporting browser like Firefox 3.1.
    As like all jData interfaces, they require postMessage and localStorage (or globalStorage) support.
    jData is basically a shared localStorage object that can be read by any website but writing requires user confirmation.
    Methods:
    • JIL.get(item [, callback]) // Gets value of an item
    • JIL.set(item, value [, callback]) // Sets value of an item
    • JIL.remove(item [, callback]) // Removes an item
    • JIL.list([callback]) // Lists every item
    • JIL.length([callback]) // Gets amount of items
    • JIL.trust([callback]) // Requests to become trusted host
    • JIL.untrust([callback]) // Requests to become an untrusted host (always returns true)


    JavaScript code:
    PHP Code:
    /*
    JIL (jData Interface Library) - v0.0.2 - code.eligrey.com/jdata/jil/
    @requires json2.js OR native JSON.parse & native JSON.stringify
    @desc jData interface library to interact with a jData host
    @license http://creativecommons.org/licenses/LGPL/2.1/
    @author Elijah Grey - www.eligrey.com
    const Contact_Email_Address = ['\x65\x6c\x69\x6a\x61\x68','\x67\x72\x65\x79\x2e\x6e\x61\x6d\x65'].join('\x40');
    */

    if ( typeof JSON == "undefined" ) { // load JSON if it doesn't already exist
        
    document.getElementsByTagName('head')[0].appendChild(document.createElement('script')).src 'json2.js';
    }

    var 
    JIL = { // The JIL Object
        
    host'http://jdata.eligrey.com'// Host, no forward slash as to be able to use for checking e.origin
        
    framedocument.createElement('iframe'),
        
    requestCallbacks: {},
        
    generateID: function() { // Return Math.random() as a string without the preceding "0."
            
    return Math.random().toString().substr(2);
        },
        
    readyfalse,
        
    handleMessage: function(evt) {
            if ( 
    evt.origin == JIL.host ) { // Only handle messages from JIL host
                
    var message JSON.parse(evt.data);
                if ( 
    typeof message.id != 'undefined' && typeof message.response != 'undefined' ) {
                    
    // do callback, then delete to free up memory
                    
    if ( JIL.requestCallbacks[message.id] ) {
                        if ( 
    typeof JIL.requestCallbacks[message.id] == 'function' JIL.requestCallbacks[message.id](message.response);
                        
    delete JIL.requestCallbacks[message.id];
                    }
                }
            }
        },
        
    query: function(objcallback) { // sends a JSON-encoded object to JIL.frame
          
    if ( JIL.ready == true ) {
            if (
    callback) {
                var 
    requestID JIL.generateID();
                
    JIL.requestCallbacks[requestID] = callback;
            }
            
    JIL.frame.contentWindow.postMessage(JSON.stringify({idrequestIDrequest:obj}), JIL.host);
          }
        },
        
    get: function(itemcallback) { // get item from jData localStorage
            
    if (item && item != ''JIL.query({type"get"itemitem}, callback||false);
        },
        
    set: function(itemvaluecallback) { // set item to jData localStorage
            
    if (item && item != ''JIL.query({type"set"itemitemvaluevalue}, callback||false);
        },
        
    remove: function(itemcallback) { // remove item
            
    if (item && item != ''JIL.query({type"remove"itemitem}, callback||false);
        },
        
    trust: function(callback) { // request to become trusted host
            
    JIL.query({type"trust"}, callback||false);
        },
        
    untrust: function(callback) { // request to become untrusted host
            
    JIL.query({type"untrust"}, callback||false);
        },
        
    length: function(callback) { // get total # of items
            
    JIL.query({type"length"}, callback||false);
        },
        list: function(
    callback) { // get list of all items
            
    JIL.query({type"list"}, callback||false);
        },
        
    addEvent: function( objtypefn ) {
            if ( 
    obj.attachEvent ) {
                
    obj.attachEvent'on'+typefn );
            } else {
                
    obj.addEventListenertypefnfalse );
            }
        },
        
    removeEvent: function removeEventobjtypefn ) {
            if ( 
    obj.detachEvent ) {
                
    obj.detachEvent'on'+typefn );
            } else {
                
    obj.removeEventListenertypefnfalse );
            }
        },
        
    init: function() {
            
    JIL.ready true;
            
    JIL.addEvent(window'message'JIL.handleMessage);
            
    JIL.removeEvent(JIL.frame'load'JIL.init);
        }
    };
    JIL.addEvent(window'message'JIL.handleMessage);

    JIL.frame.setAttribute('src'JIL.host+'/api/postMessage.php');
    JIL.frame.setAttribute('style''display:none');
    document.getElementsByTagName('head')[0].appendChild(JIL.frame);

    JIL.addEvent(JIL.frame'load'JIL.init); 
    A minified (by YUI compressor), than packed (by /packer/) version of json2.js in case you don't want to Google it (2.73kb):
    Code:
    eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('3(!l.m){m={}}(5(){5 f(n){7 n<10?"0"+n:n}3(6 V.q.p!=="5"){V.q.p=5(a){7 l.1o()+"-"+f(l.1p()+1)+"-"+f(l.1q())+"T"+f(l.1r())+":"+f(l.1s())+":"+f(l.1t())+"Z"};L.q.p=1u.q.p=1v.q.p=5(a){7 l.1w()}}w e=/[\\1x\\W\\X-\\Y\\11\\12\\13\\14-\\15\\17-\\18\\19-\\1a\\1b\\1c-\\1d]/g,G=/[\\\\\\"\\1y-\\1z\\1A-\\1B\\W\\X-\\Y\\11\\12\\13\\14-\\15\\17-\\18\\19-\\1a\\1b\\1c-\\1d]/g,8,y,1e={"\\b":"\\\\b","\\t":"\\\\t","\\n":"\\\\n","\\f":"\\\\f","\\r":"\\\\r",\'"\':\'\\\\"\',"\\\\":"\\\\\\\\"},o;5 H(b){G.1f=0;7 G.M(b)?\'"\'+b.z(G,5(a){w c=1e[a];7 6 c==="I"?c:"\\\\u"+("1g"+a.1h(0).N(16)).1i(-4)})+\'"\':\'"\'+b+\'"\'}5 A(a,b){w i,k,v,h,B=8,9,2=b[a];3(2&&6 2==="x"&&6 2.p==="5"){2=2.p(a)}3(6 o==="5"){2=o.J(b,a,2)}1C(6 2){C"I":7 H(2);C"O":7 1D(2)?L(2):"D";C"1E":C"D":7 L(2);C"x":3(!2){7"D"}8+=y;9=[];3(P.q.N.1F(2)==="[x 1G]"){h=2.h;E(i=0;i<h;i+=1){9[i]=A(i,2)||"D"}v=9.h===0?"[]":8?"[\\n"+8+9.K(",\\n"+8)+"\\n"+B+"]":"["+9.K(",")+"]";8=B;7 v}3(o&&6 o==="x"){h=o.h;E(i=0;i<h;i+=1){k=o[i];3(6 k==="I"){v=A(k,2);3(v){9.1j(H(k)+(8?": ":":")+v)}}}}Q{E(k 1k 2){3(P.1l.J(2,k)){v=A(k,2);3(v){9.1j(H(k)+(8?": ":":")+v)}}}}v=9.h===0?"{}":8?"{\\n"+8+9.K(",\\n"+8)+"\\n"+B+"}":"{"+9.K(",")+"}";8=B;7 v}}3(6 m.R!=="5"){m.R=5(a,b,c){w i;8="";y="";3(6 c==="O"){E(i=0;i<c;i+=1){y+=" "}}Q{3(6 c==="I"){y=c}}o=b;3(b&&6 b!=="5"&&(6 b!=="x"||6 b.h!=="O")){1m 1n 1H("m.R")}7 A("",{"":a})}}3(6 m.S!=="5"){m.S=5(c,d){w j;5 U(a,b){w k,v,2=a[b];3(2&&6 2==="x"){E(k 1k 2){3(P.1l.J(2,k)){v=U(2,k);3(v!==1I){2[k]=v}Q{1J 2[k]}}}}7 d.J(a,b,2)}e.1f=0;3(e.M(c)){c=c.z(e,5(a){7"\\\\u"+("1g"+a.1h(0).N(16)).1i(-4)})}3(/^[\\],:{}\\s]*$/.M(c.z(/\\\\(?:["\\\\\\/1K]|u[0-1L-1M-F]{4})/g,"@").z(/"[^"\\\\\\n\\r]*"|1N|1O|D|-?\\d+(?:\\.\\d*)?(?:[1P][+\\-]?\\d+)?/g,"]").z(/(?:^|:|,)(?:\\s*\\[)+/g,""))){j=1Q("("+c+")");7 6 d==="5"?U({"":j},""):j}1m 1n 1R("m.S")}}})();',62,116,'||value|if||function|typeof|return|gap|partial||||||||length||||this|JSON||rep|toJSON|prototype||||||var|object|indent|replace|str|mind|case|null|for||escapable|quote|string|call|join|String|test|toString|number|Object|else|stringify|parse||walk|Date|u00ad|u0600|u0604|||u070f|u17b4|u17b5|u200c|u200f||u2028|u202f|u2060|u206f|ufeff|ufff0|uffff|meta|lastIndex|0000|charCodeAt|slice|push|in|hasOwnProperty|throw|new|getUTCFullYear|getUTCMonth|getUTCDate|getUTCHours|getUTCMinutes|getUTCSeconds|Number|Boolean|valueOf|u0000|x00|x1f|x7f|x9f|switch|isFinite|boolean|apply|Array|Error|undefined|delete|bfnrt|9a|fA|true|false|eE|eval|SyntaxError'.split('|'),0,{}))
    Here are some examples of use with the public.openid variable:
    PHP Code:
    JIL.set('public.openid'document.getElementById('openid').value, function(b) {
    alert('public.openid was '+(b?'not ':'')+'set.')
    }) 
    PHP Code:
    JIL.get('public.openid', function(s){
    if (
    !== null ) {
    doSomethingWithOpenID(s);
    }
    }) 
    I have also have uploaded a demo.

    Update: jData now also fully supports IE8 beta 2!
    Last edited by Sephr; 01-15-2009 at 10:46 PM. Reason: added JIL.untrust()

  2. Users who have thanked Sephr for this post:

    oesxyl (01-16-2009)

  • #2
    New Coder
    Join Date
    Dec 2008
    Posts
    21
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Version 0.0.3 supports changing the host & pathname (JIL.origin & JIL.path) and re-initializing JIL.frame by using JIL.loadFrame([readyCallback]). (for migration of data from one host to another)

    I can't edit my above post, so here I'll put it here:
    PHP Code:
    /*
    JIL (jData Interface Library) - v0.0.3 - code.eligrey.com/jdata/jil/
    @requires json2.js OR native JSON.parse & native JSON.stringify
    @desc jData interface library to interact with a jData host
    @license http://creativecommons.org/licenses/LGPL/2.1/
    @author Elijah Grey - www.eligrey.com
    const Contact_Email_Address = ['\x65\x6c\x69\x6a\x61\x68','\x67\x72\x65\x79\x2e\x6e\x61\x6d\x65'].join('\x40');
    */

    if ( typeof JSON == "undefined" ) { // load JSON if it doesn't already exist
        
    document.getElementsByTagName('head')[0].appendChild(document.createElement('script')).src 'json2.js';
    }

    var 
    JIL = { // The JIL Object
        
    origin'http://jdata.eligrey.com'// Host, no forward slash as to be able to use for checking e.origin
        
    path''// Change to path if host does not have jData host at initial directory
        
    framedocument.createElement('iframe'),
        
    requestCallbacks: {},
        
    generateID: function() { // Return Math.random() as a string without the preceding "0."
            
    return Math.random().toString().substr(2);
        },
        
    readyfalse,
        
    handleMessage: function(evt) {
            if ( 
    evt.origin == JIL.origin ) { // Only handle messages from JIL host
                
    var message JSON.parse(evt.data);
                if ( 
    typeof message.id != 'undefined' && typeof message.response != 'undefined' ) {
                    
    // do callback, then delete to free up memory
                    
    if ( JIL.requestCallbacks[message.id] ) {
                        if ( 
    typeof JIL.requestCallbacks[message.id] == 'function' JIL.requestCallbacks[message.id](message.response);
                        
    delete JIL.requestCallbacks[message.id];
                    }
                }
            }
        },
        
    query: function(objcallback) { // sends a JSON-encoded object to JIL.frame
          
    if ( JIL.ready == true ) {
            if (
    callback) {
                var 
    requestID JIL.generateID();
                
    JIL.requestCallbacks[requestID] = callback;
            }
            if ( 
    typeof obj == "string" obj = {typeobj};
            
    JIL.frame.contentWindow.postMessage(JSON.stringify({idrequestIDrequest:obj}), '*');
          }
        },
        
    get: function(itemcallback) { // get item from jData localStorage
            
    if (item && item != ''JIL.query({type"get"itemitem}, callback||false);
        },
        
    set: function(itemvaluecallback) { // set item to jData localStorage
            
    if (item && item != ''JIL.query({type"set"itemitemvaluevalue||''}, callback||false);
        },
        
    remove: function(itemcallback) { // remove item
            
    if (item && item != ''JIL.query({type"remove"itemitem}, callback||false);
        },
        
    trust: function(callback) { // request to become trusted host
            
    JIL.query("trust"callback||false);
        },
        
    untrust: function(callback) { // request to become untrusted host
            
    JIL.query("untrust"callback||false);
        },
        
    length: function(callback) { // get total # of items
            
    JIL.query("length"callback||false);
        },
        list: function(
    callback) { // get list of all items
            
    JIL.query("list"callback||false);
        },
        
    addEvent: function( objtypefn ) {
            if ( 
    obj.attachEvent ) {
                
    obj.attachEvent'on'+typefn );
            } else {
                
    obj.addEventListenertypefnfalse );
            }
        },
        
    removeEvent: function removeEventobjtypefn ) {
            if ( 
    obj.detachEvent ) {
                
    obj.detachEvent'on'+typefn );
            } else {
                
    obj.removeEventListenertypefnfalse );
            }
        },
        
    frameActivefalse,
        
    listeningfalse,
        
    loadFrame: function(readyCallback) {
            
    JIL.ready false;
            
    JIL.removeEvent(window'message'JIL.handleMessage);
            
    JIL.listening true;
            if ( 
    JIL.frameActive == false ) {
                
    JIL.frame.setAttribute('style''display:none');
                
    document.getElementsByTagName('head').appendChild(JIL.frame);
                
    JIL.frameActive true;
            }
            function 
    init() {
                
    JIL.removeEvent(JIL.frame'load'init);
                
    JIL.addEvent(window'message'JIL.handleMessage);
                
    JIL.listening true;
                
    JIL.ready true;
                if ( 
    readyCallback readyCallback();
            }
            
    JIL.frame.setAttribute('src'JIL.origin+JIL.path+'/api/postMessage.php');
            
    JIL.addEvent(JIL.frame'load'init);
        }
    };
    JIL.loadFrame(); 
    Last edited by Sephr; 01-17-2009 at 02:38 AM. Reason: using php tag for syntax highlighting the js

  • #3
    New Coder
    Join Date
    Dec 2008
    Posts
    21
    Thanks
    0
    Thanked 3 Times in 3 Posts
    I just noticed that my last post says version 0.0.3. I was testing stuff out and I meant to put 0.0.3a for alpha. Here is the real version 0.0.3:
    PHP Code:
    /*
    @name JIL (jData Interface Library)
    @version 0.0.3
    @home http://code.eligrey.com/jdata/jil/
    @requires json2.js OR native JSON.parse & native JSON.stringify
    @desc jData interface library to interact with a jData host
    @license http://creativecommons.org/licenses/LGPL/2.1/
    @author Elijah Grey - www.eligrey.com
    const Contact_Email_Address = ['\x65\x6c\x69\x6a\x61\x68','\x67\x72\x65\x79\x2e\x6e\x61\x6d\x65'].join('\x40');
    */

    if ( typeof JSON == "undefined" ) { // load JSON if it doesn't already exist
        
    document.getElementsByTagName('head')[0].appendChild(document.createElement('script')).src 'json2.js';
    }

    var 
    JIL = { // The JIL Object
        
    origin'http://jdata.eligrey.com'// Host, no forward slash as to be able to use for checking e.origin
        
    path''// Change to path if host does not have jData host at initial directory
        
    framedocument.createElement('iframe'),
        
    requestCallbacks: {},
        
    generateID: function() { // Return Math.random() as a string without the preceding "0."
            
    return Math.random().toString().substr(2);
        },
        
    readyfalse,
        
    handleMessage: function(evt) {
            if ( 
    evt.origin == JIL.origin ) { // Only handle messages from JIL host
                
    var message JSON.parse(evt.data);
                if ( 
    typeof message.id != 'undefined' && typeof message.response != 'undefined' ) {
                    
    // do callback, then delete to free up memory
                    
    if ( JIL.requestCallbacks[message.id] ) {
                        if ( 
    typeof JIL.requestCallbacks[message.id] == 'function' JIL.requestCallbacks[message.id](message.response);
                        
    delete JIL.requestCallbacks[message.id];
                    }
                }
            }
        },
        
    query: function(reqObjcallback) { // sends a JSON-encoded object to JIL.frame
          
    if ( JIL.ready == true ) {
            if (
    callback) {
                var 
    requestID JIL.generateID();
                
    JIL.requestCallbacks[requestID] = callback;
            }
            if ( 
    typeof reqObj == "string" reqObj = {methodreqObj};
            
    reqObj.id requestID;
            
    JIL.frame.contentWindow.postMessage(JSON.stringify(reqObj), '*');
          }
        },
        
    get: function(itemcallback) { // get item from jData localStorage
            
    if (item && item != ''JIL.query({method"get"itemitem}, callback||false);
        },
        
    set: function(itemvaluecallback) { // set item to jData localStorage
            
    if (item && item != ''JIL.query({method"set"itemitemvaluevalue||''}, callback||false);
        },
        
    remove: function(itemcallback) { // remove item
            
    if (item && item != ''JIL.query({method"remove"itemitem}, callback||false);
        },
        
    trust: function(callback) { // request to become trusted host
            
    JIL.query("trust"callback||false);
        },
        
    untrust: function(callback) { // request to become untrusted host
            
    JIL.query("untrust"callback||false);
        },
        
    length: function(callback) { // get total # of items
            
    JIL.query("length"callback||false);
        },
        list: function(
    callback) { // get list of all items
            
    JIL.query("list"callback||false);
        },
        
    addEvent: function( objtypefn ) {
            if ( 
    obj.attachEvent ) {
                
    obj.attachEvent'on'+typefn );
            } else {
                
    obj.addEventListenertypefnfalse );
            }
        },
        
    removeEvent: function removeEventobjtypefn ) {
            if ( 
    obj.detachEvent ) {
                
    obj.detachEvent'on'+typefn );
            } else {
                
    obj.removeEventListenertypefnfalse );
            }
        },
        
    frameActivefalse,
        
    listeningfalse,
        
    loadFrame: function(readyCallback) {
            
    JIL.ready false;
            
    JIL.removeEvent(window'message'JIL.handleMessage);
            
    JIL.listening true;
            if ( 
    JIL.frameActive == false ) {
                
    JIL.frame.setAttribute('style''display:none');
                
    document.getElementsByTagName('head')[0].appendChild(JIL.frame);
                
    JIL.frameActive true;
            }
            function 
    init() {
                
    JIL.removeEvent(JIL.frame'load'init);
                
    JIL.addEvent(window'message'JIL.handleMessage);
                
    JIL.listening true;
                
    JIL.ready true;
                if ( 
    readyCallback readyCallback();
            }
            
    JIL.frame.setAttribute('src'JIL.origin+JIL.path+'/api/postMessage.php');
            
    JIL.addEvent(JIL.frame'load'init);
        }
    };
    JIL.loadFrame(); 
    Last edited by Sephr; 01-27-2009 at 03:34 AM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •