summaryrefslogtreecommitdiffstats
path: root/chrome/test/data/extensions/samples/subscribe/toolstrip.html
blob: c2355a684be97a50893894ee84f9b0a0ccd2839f (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
<html>
<div id="button" class="toolstrip-button">
  <img id="icon" src="icon_disabled.png"><span>Subscribe</span>
</div>

<script>
  var icon = document.getElementById("icon");
  var button = document.getElementById("button");
  var enabled = false;
  var feedUrl, req;

  button.onclick = handleClick;

  chrome.self.onConnect.addListener(function(port) {
    port.onMessage.addListener(function(feedUrls) {
      // TODO(aa): When we have the ability to display drop downs, we should let
      // the user pick which feed to subscribe. Right now, we just pick the
      // first.
      feedUrl = feedUrls[0];
      if (!feedUrl) {
        icon.src = "icon_disabled.png";
        enabled = false;
      } else {
        icon.src = "icon.png";
        enabled = true;

        feedUrl = encodeURIComponent(feedUrl);
        req = new XMLHttpRequest();
        req.onload = handleResponse;
        req.open("GET",
                 "http://www.google.com/reader/api/0/subscribed?s=feed%2F" + feedUrl,
                 false);
        req.send(null);
      }
    });
  });

  function handleResponse() {
    if (req.responseText == "true") {
      icon.src =  "icon_subscribed.png";
      enabled = false;
    }
  }

  function handleClick() {
    if (enabled) {
      var url = "http://www.google.com/reader/view/feed/" + feedUrl;
      chrome.tabs.create({url: url});
    }
  }
</script>
</html>