Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pebble-js-app.src.js « js « src - github.com/ClusterM/pebble-my-data.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 833d11b3a7203fc4c9a1bc6dc334e680e0cb7214 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// -*-coding: utf-8 -*-
// vim: sw=2 ts=2 expandtab ai

//TODO geolocation

var config = {};

function fetch_data() {
  var req = new XMLHttpRequest();

  if (config["url"]) {
    req.open('GET', config["url"], true);
    req.onload = function(e) {

      if (req.readyState == 4) {
        if(req.status == 200) {
          try {
            var response = JSON.parse(req.responseText);
            //console.log("success: " + JSON.stringify(response));
            Pebble.sendAppMessage(response);

          } catch(e) {
            console.log("json parse error");
          }

        } else {
          console.log("fetch error");
        }
      }
    }
    req.send(null);

  } else {
    Pebble.sendAppMessage({ "content": "URL not defined, chech settings in Pebble App" });
  }
}

Pebble.addEventListener("ready",
  function(e) {
    console.log("JavaScript app ready and running!");

    var json = window.localStorage.getItem('pebble-my-data');

    if (typeof json === 'string') {
      try {
        config = JSON.parse(json);
        //console.log("loaded config = " + JSON.stringify(config));

      } catch(e) {
        console.log("json parse error");
      }
    }
  }
);

Pebble.addEventListener("appmessage",
  function(e) {
    //console.log("received message: " + JSON.stringify(e.payload));
    fetch_data();
  }
);

Pebble.addEventListener('showConfiguration', function () {
  if (config["url"]) {
    url = config["url"];
  } else {
    url = "";
  }

  Pebble.openURL('data:text/html,'+encodeURI('_HTMLMARKER_<!--.html'.replace('"_URL_"', url, 'g')));
});

Pebble.addEventListener("webviewclosed", function(e) {
  if ((typeof e.response === 'string') && (e.response.length > 0)) {
    config = JSON.parse(decodeURIComponent(e.response));
    //console.log("got options = " + JSON.stringify(config));

    window.localStorage.setItem('pebble-my-data', e.response);

    fetch_data();
  }
});