1/* 2 * Copyright 2010 Nicholas C. Zakas. All rights reserved. 3 * BSD Licensed. 4 * modified by wushufeng 2014-07-01 5 */ 6 $.XDStorage = function(origin, path){ 7 this.origin = origin; 8 this.path = path; 9 this._iframe = null; 10 this._iframeReady = false; 11 this._queue = []; 12 this._requests = {}; 13 this._id = 0; 14 } 15 16 $.XDStorage.prototype = { 17 18 op:{ 19 WRITE: 'W', 20 READ: 'R', 21 DEL: 'D', 22 CLEAR: 'X' 23 }, 24 //restore constructor 25 constructor: $.XDStorage, 26 27 //public interface methods 28 29 init: function(){ 30 31 var that = this; 32 33 if (!this._iframe){ 34 if (window.postMessage && window.JSON && window.localStorage){ 35 this._iframe = document.createElement("iframe"); 36 this._iframe.style.cssText = "position:absolute;width:1px;height:1px;left:-9999px;"; 37 document.body.appendChild(this._iframe); 38 39 if (window.addEventListener){ 40 this._iframe.addEventListener("load", function(){ that._iframeLoaded(); }, false); 41 window.addEventListener("message", function(event){ that._handleMessage(event); }, false); 42 } else if (this._iframe.attachEvent){ 43 this._iframe.attachEvent("onload", function(){ that._iframeLoaded(); }, false); 44 window.attachEvent("onmessage", function(event){ that._handleMessage(event); }); 45 } 46 } else { 47 throw new Error("Unsupported browser."); 48 } 49 } 50 51 this._iframe.src = this.origin + this.path; 52 53 }, 54 55 getValue: function(key, callback){ 56 this._toSend({ 57 key: key 58 },callback); 59 }, 60 61 setValue: function(key,value,callback){ 62 63 this._toSend({ 64 key: key, 65 op: this.op.WRITE, 66 value: value 67 },callback); 68 }, 69 delValue: function(key,callback){ 70 this._toSend({ 71 key: key, 72 op: this.op.DEL 73 },callback); 74 }, 75 clearValue: function(callback){ 76 this._toSend({ 77 op: this.op.CLEAR 78 },callback); 79 }, 80 //private methods 81 82 _toSend: function(params,callback){ 83 var data = { 84 request: { 85 key: params.key, 86 id: ++this._id, 87 op: params.op, 88 value: params.value 89 }, 90 callback: callback 91 }; 92 if (this._iframeReady){ 93 this._sendRequest(data); 94 } else { 95 this._queue.push(data); 96 } 97 98 if (!this._iframe){ 99 this.init(); 100 } 101 }, 102 103 _sendRequest: function(data){ 104 this._requests[data.request.id] = data; 105 this._iframe.contentWindow.postMessage(JSON.stringify(data.request), this.origin); 106 }, 107 108 _iframeLoaded: function(){ 109 this._iframeReady = true; 110 111 if (this._queue.length){ 112 for (var i=0, len=this._queue.length; i < len; i++){ 113 this._sendRequest(this._queue[i]); 114 } 115 this._queue = []; 116 } 117 }, 118 119 _handleMessage: function(event){ 120 if (event.origin == this.origin){ 121 var data = JSON.parse(event.data); 122 if(this._requests[data.id]&&this._requests[data.id].callback) 123 this._requests[data.id].callback(data.value); 124 delete this._requests[data.id]; 125 } 126 } 127 128 }; 129
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.