Advertisement
Mr_Mig

Userscripts XDR wrapper

Oct 18th, 2011
2,168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Requires _opera-xdr-engine.js to handle script-based requests in Opera*/
  2. var xdr = {
  3.         /* request ID, JSONP counter*/
  4.         reqId : 0,
  5.         req : {},
  6.         /* Prevent caching*/   
  7.         prepareUrl : function(url){
  8.             url = /\?/.test(url) ? url + "&" + seed : url + "?" + seed;
  9.             return url;
  10.         },
  11.         /*
  12.          *
  13.          */
  14.         xget : function(url, onDone){
  15.             url = this.prepareUrl(url);
  16.             if (window.opera && window.opera.defineMagicVariable){
  17.                 this.scriptTransport(url, onDone);
  18.             } else if(chrome && chrome.extension) {
  19.                 this.xhrTransport(url, onDone);
  20.             } else if (GM_xmlhttpRequest){
  21.                 this.GMTransport(url,onDone);
  22.             } else {
  23.                 var currentReqId = this.reqId++;
  24.                 this.req[currentReqId].handleJSONP = onDone;
  25.                
  26.                 this.JSONPTransport(url, "xdr.req["+currentReqId+"].handleJSONP");
  27.                         }
  28.         },
  29.        
  30.         /**
  31.          * Make GET request via <script> transport.
  32.          *
  33.          */
  34.         scriptTransport : function(url, onDone){
  35.             var t = document.createElement("script");
  36.             t.src = url;
  37.             t._callback = onDone;
  38.             document.body.appendChild(t);
  39.         },
  40.        
  41.         // transport should be proxyed via background.html of the chrome
  42.         // extension
  43.         xhrTransport : function(url, onDone){
  44.             chrome.extension.sendRequest({'action' : 'xget', 'url':url}, onDone);
  45.         },
  46.         /**
  47.          * Make GET request via GM_xmlhttpRequest.
  48.          *
  49.          */
  50.         GMTransport : function(url, onDone){
  51.             setTimeout(function(){GM_xmlhttpRequest({
  52.                 method : "GET",
  53.                 url : url,
  54.                 onload : function(x) {
  55.                     var o = x.responseText;
  56.                     if (onDone) {
  57.                         onDone(o);
  58.                     }
  59.                 }
  60.             });},0);
  61.        
  62.         },
  63.         JSONPTransport : function(url, callbackName ){
  64.             if (callbackName && typeof callbackName == "string"){
  65.                 url += "&callback="+callbackName;
  66.             }
  67.             var t = document.createElement("script");
  68.             t.src = url;
  69.             document.body.appendChild(t);
  70.         }
  71.     }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement