File: checkCoralCache.js

package info (click to toggle)
greasemonkey 2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 1,820 kB
  • sloc: xml: 171; makefile: 10
file content (37 lines) | stat: -rw-r--r-- 1,023 bytes parent folder | download
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
Components.utils.import('resource://greasemonkey/prefmanager.js');
Components.utils.import('resource://greasemonkey/util.js');

const EXPORTED_SYMBOLS = ['checkCoralCache'];

const XMLHttpRequest = Components.Constructor(
    '@mozilla.org/xmlextras/xmlhttprequest;1');

var gCheckIsRunning = false;

function checkCoralCache() {
  if (GM_prefRoot.getValue('coralCacheWorks')) return true;

  if (gCheckIsRunning) return false;

  gCheckIsRunning = true;
  var req = new XMLHttpRequest();
  req.onerror = GM_util.hitch(null, onError, req);
  req.onload = GM_util.hitch(null, onLoad, req);
  req.open('get', 'http://www.greasespot.net.nyud.net/');
  req.send();

  return false;
}

function onError(aReq, aEvent) {
  GM_prefRoot.setValue('coralCacheWorks', false);
  gCheckIsRunning = false;
}

function onLoad(aReq, aEvent) {
  if (200 !== aReq.status) return onError();
  if (-1 == aReq.responseText.indexOf('Greasemonkey')) return onError();

  GM_prefRoot.setValue('coralCacheWorks', true);
  gCheckIsRunning = false;
}