Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- </head>
- <body>
- <script>
- /**
- * Performs an XMLHttpRequest to send Ajax request
- * @param backendURL The Url where to send xhr request
- * @param dataStr Serialized (for GET request) Data array
- * @param callback Function If the response from fetching url has a
- * HTTP status of 200, this function is called with a JSON decoded
- * response. Otherwise, this function is called with null.
- */
- function sendAjaxRequest(backendURL, dataStr, callback) {
- var xhr = new XMLHttpRequest();
- xhr.onreadystatechange = function(data) {
- if (xhr.readyState == 4) {
- if (xhr.status == 200) {
- var data = JSON.parse(xhr.responseText);
- callback(data);
- } else {
- callback(null);
- }
- }
- }
- // Note that any URL fetched here must be matched by a permission in
- // the manifest.json file!
- var url = backendURL+'?'+dataStr;
- console.log(url);
- xhr.open('GET', url, true);
- xhr.send();
- };
- /**
- * Handles data sent via chrome.extension.sendRequest().
- * @param request Object Data sent in the request.
- * @param sender Object Origin of the request.
- * @param callback Function The method to call when the request completes.
- */
- function onRequest(request, sender, callback) {
- if (request.action == 'sendAjaxRequest') {
- sendAjaxRequest(request.url, request.dataStr, callback);
- }
- };
- // Wire up the listener.
- chrome.extension.onRequest.addListener(onRequest);
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement