Advertisement
Mr_Mig

Google Chrome backgroung.html XDR proxy for userscripts

Oct 14th, 2011
2,390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.   </head>
  5.   <body>
  6.     <script>
  7.    
  8.     /**
  9.     * XDR for Chrome.
  10.     * This is a background script engine, which should be requested via content script
  11.     *
  12.     */
  13.       function get(url, callback) {
  14.         var xhr = new XMLHttpRequest();
  15.         xhr.onreadystatechange = function(data) {
  16.           if (xhr.readyState == 4) {
  17.             if (xhr.status == 200) {
  18.               callback(data.srcElement.responseText);
  19.             } else {
  20.               callback(null);
  21.             }
  22.           }
  23.         }
  24.         // Note that any URL fetched here must be matched by a permission in
  25.         // the manifest.json file!
  26.         xhr.open('GET', url, true);
  27.         xhr.send();
  28.       };
  29.  
  30.       /**
  31.        * Handles data sent via chrome.extension.sendRequest().
  32.        * @param request Object Data sent in the request.
  33.        * @param sender Object Origin of the request.
  34.        * @param callback Function The method to call when the request completes.
  35.        */
  36.       function onRequest(request, sender, callback) {
  37.         // Only supports the 'xget' action, although this could be
  38.         // generalized into a more robust RPC system.
  39.         if (request.action == 'xget') {
  40.           get(request.url, callback);
  41.         }
  42.       };
  43.  
  44.       // Wire up the listener.
  45.       chrome.extension.onRequest.addListener(onRequest);
  46.      
  47.       // get method should be called from the content script as follows:
  48.       // chrome.extension.sendRequest({'action' : 'xget', 'url':myUrl}, myCallback);
  49.     </script>
  50.   </body>
  51. </html>
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement