Advertisement
Guest User

background.html

a guest
Jan 13th, 2012
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.99 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.     </head>
  5.     <body>
  6.         <script>
  7.             /**
  8.             * Performs an XMLHttpRequest to send Ajax request
  9.             * @param backendURL The Url where to send xhr request
  10.             * @param dataStr Serialized (for GET request) Data array
  11.             * @param callback Function If the response from fetching url has a
  12.             *     HTTP status of 200, this function is called with a JSON decoded
  13.             *     response.  Otherwise, this function is called with null.
  14.             */
  15.             function sendAjaxRequest(backendURL, dataStr, callback) {
  16.                 var xhr = new XMLHttpRequest();
  17.                 xhr.onreadystatechange = function(data) {
  18.                     if (xhr.readyState == 4) {
  19.                         if (xhr.status == 200) {
  20.                             var data = JSON.parse(xhr.responseText);
  21.                             callback(data);
  22.                         } else {
  23.                             callback(null);
  24.                         }
  25.                     }
  26.                 }
  27.                 // Note that any URL fetched here must be matched by a permission in
  28.                 // the manifest.json file!
  29.                 var url = backendURL+'?'+dataStr;
  30.                 console.log(url);
  31.                 xhr.open('GET', url, true);
  32.                 xhr.send();
  33.             };
  34.  
  35.             /**
  36.             * Handles data sent via chrome.extension.sendRequest().
  37.             * @param request Object Data sent in the request.
  38.             * @param sender Object Origin of the request.
  39.             * @param callback Function The method to call when the request completes.
  40.             */
  41.             function onRequest(request, sender, callback) {
  42.  
  43.                 if (request.action == 'sendAjaxRequest') {
  44.                     sendAjaxRequest(request.url, request.dataStr, callback);
  45.                 }
  46.             };
  47.  
  48.             // Wire up the listener.
  49.             chrome.extension.onRequest.addListener(onRequest);
  50.         </script>
  51.     </body>
  52. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement