summaryrefslogtreecommitdiff
path: root/js/background.js
blob: dec42907249f0514bce08b6fb421720a7dcf3c5c (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* Handle showing of new item count and polling the TT-RSS server. */

var last_updated = 0;
var feeds_last_updated = 0;
var prefs_last_updated = 0;

function param_escape(arg) {
  if (typeof encodeURIComponent != 'undefined')
    return encodeURIComponent(arg);
  else
    return escape(arg);
}

function update_feeds() {
  console.log('feeds update');

  var requestUrl = localStorage['site_url'] +
    '/public.php?op=globalUpdateFeeds';

  var xhr = new XMLHttpRequest();

  xhr.open('POST', requestUrl, true);
  xhr.send();

  var d = new Date();
  localStorage['last_feeds_updated'] = d.getTime();
}

function update() {
  console.log('update');

  var d = new Date();
  var login = localStorage['login'];
  var single_user = localStorage['single_user'];

  if (single_user == '1') login = 'admin';

  var requestUrl = localStorage['site_url'] + '/public.php';
  var params = 'op=getUnread&fresh=1&login=' + param_escape(login);

  var xhr = new XMLHttpRequest();

  xhr.open('POST', requestUrl, true);
  xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  xhr.send(params);

  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
      var icon = new Object();
      var title = new Object();
      var badge = new Object();
      var badge_color = new Object();

      // init stuff
      icon.path = 'images/normal.png';
      title.title = '';
      badge.text = '';
      badge_color.color = [0, 0, 0, 0];

      var show_badge = localStorage['show_badge'] == '1';
      var show_fresh = localStorage['show_fresh'] == '1';

      if (xhr.status == 200) {
        var response = xhr.responseText.split(';');

        var unread = parseInt(response[0]);

        if (isNaN(unread)) unread = 0;

        var fresh;

        if (response.length == 2)
          fresh = parseInt(response[1]);
        else
          fresh = 0;

        if (isNaN(fresh)) fresh = 0;

        if (unread > 0) {
          icon.path = 'images/alert.png';
          title.title = 'You have %s unread articles.'.replace('%s', unread);

          if (show_fresh && fresh > 0) {
            badge.text = fresh + '';
            badge_color.color = [0, 200, 0, 255];
          } else {
            badge.text = unread + '';
            badge_color.color = [255, 0, 0, 255];
          }
        } else if (unread == -1) {
          icon.path = 'images/error.png';

          var errorMsg = xhr.responseText.split(';')[1];

          title.title = 'Error: %s.'.replace('%s', errorMsg.trim());
        } else {
          title.title = 'You have no unread articles.';
        }

        localStorage['last_updated'] = d.getTime();
        localStorage['last_error'] = '';
      } else {
        localStorage['last_error'] = xhr.responseText;

        icon.path = 'images/error.png';
        title.title = 'Error (%s) while updating.'.replace('%s', xhr.status);
      }

      if (!show_badge) badge.text = '';

      chrome.browserAction.setBadgeBackgroundColor(badge_color);
      chrome.browserAction.setBadgeText(badge);
      chrome.browserAction.setTitle(title);
      chrome.browserAction.setIcon(icon);
    }
  };
}

function timeout() {
  var update_interval;
  var prefs_updated;
  var feeds_update_interval = 30 * 60 * 1000;

  if (localStorage['update_interval'])
    update_interval = localStorage['update_interval'] * 60 * 1000;
  else
    update_interval = 15 * 60 * 1000;

  if (localStorage['prefs_updated'])
    prefs_updated = localStorage['prefs_updated'];
  else
    prefs_updated = -1;

  var d = new Date();

  if (d.getTime() > last_updated + update_interval ||
      prefs_updated != prefs_last_updated) {
    last_updated = d.getTime();
    try {
      update();
    } catch (e) {
      console.warn(e);
    }
  }

  if (localStorage['update_feeds'] == 1 &&
      (d.getTime() > feeds_last_updated + feeds_update_interval ||
       prefs_updated != prefs_last_updated)) {
    feeds_last_updated = d.getTime();

    try {
      update_feeds();
    } catch (e) {
      console.warn(e);
    }
  }

  prefs_last_updated = prefs_updated;
}

function is_newtab(url) {
	return url.indexOf('chrome://newtab/') == 0;
}

function is_site_url(url) {
    var site_url = localStorage['site_url'];
	return url.indexOf(site_url) == 0;
}

function init() {
  chrome.browserAction.onClicked.addListener(function() {
    var site_url = localStorage['site_url'];

    if (site_url) {
      // try to find already opened tab
      chrome.tabs.query({currentWindow: true}, function(tabs) {

        var found_existing = false;

        for (var i = 0, tab; tab = tabs[i]; i++) {
          if (tab.url && is_site_url(tab.url)) {
            chrome.tabs.update(tab.id, {highlighted: true});
            update();
            found_existing = true;
            return;
          }
        }

        // check if current tab is newtab (only if not updated yet)
        if (!found_existing) {
          chrome.tabs.query({currentWindow: true, active: true}, function(tabs) {
            if (tabs[0].url && is_newtab(tabs[0].url)) {
              chrome.tabs.update(tabs[0].id, {url: site_url});
            } else {
              chrome.tabs.create({url: site_url});
            }
          });
        } // (if (!found_existing))

      });

    } // if (site_url)
  });

  // TODO: Create smarter algorithm that sets `periodInMinutes` to
  // `feeds_update_interval` and updates the `alarm` object when extension
  // preferences are saved.
  timeout();
  chrome.alarms.create({periodInMinutes: 1});
  chrome.alarms.onAlarm.addListener(function() {timeout();});
}

init();