gotify-firefox

Send push notifications to your Gotify server from Firefox
git clone git://fossdaily.xyz/gotify-firefox
Log | Files | Refs | README | LICENSE

http.js (1659B)


      1 'use strict';
      2 
      3 gotify.onResponse = function(status, body, done) {
      4   if (status == 200) {
      5     try {
      6       done(JSON.parse(body));
      7     } catch (e) {
      8       done();
      9     }
     10   } else if (status === 401) {
     11     // sign out
     12   } else if (status === 400) {
     13     try {
     14       done(null, JSON.parse(body).error);
     15     } catch (e) {
     16       done();
     17     }
     18   } else {
     19     done();
     20   }
     21 };
     22 
     23 gotify.buildXhr = function(method, url, done) {
     24   var xhr = new XMLHttpRequest();
     25   xhr.open(method, url, true);
     26   xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
     27   xhr.setRequestHeader('X-User-Agent', 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0');
     28 
     29   xhr.onreadystatechange = function() {
     30     if (xhr.readyState === 4) {
     31       gotify.onResponse(xhr.status, xhr.responseText, done);
     32     }
     33   };
     34 
     35   return xhr;
     36 }
     37 
     38 gotify.postToApi = function(url, object, done) {
     39   browser.storage.local.get({
     40     app_token: null
     41   }).then(function(items) {
     42     if (items.app_token === null || items.app_token === '') {
     43       Materialize.toast('No app token configured. Please set one in Options.', 4000);
     44     } else {
     45       var xhr = gotify.buildXhr('POST', url + '?token=' + items.app_token, done);
     46       xhr.send(object);
     47     }
     48   });
     49 };
     50 
     51 gotify.postMessage = function(object, done) {
     52   browser.storage.local.get({
     53     gotify_url: null
     54   }).then(function(items) {
     55     if (items.gotify_url === null || items.gotify_url === '') {
     56       Materialize.toast('No Gotify URL configured. Please set one in Options.', 4000);
     57     } else {
     58       gotify.postToApi(items.gotify_url, object, done);
     59     }
     60   });
     61 };