Revisions

gist: 206199 Download_button fork
public
Public Clone URL: git://gist.github.com/206199.git
Embed All Files: show embed
JavaScript #
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
var Herd = {};
 
Herd.INDEX_URL = "http://herd.fernandotakai.net/";
Herd.SUBMIT_URL = "http://herd.fernandotakai.net/api/submit";
 
Herd.getCodeInfo = function Herd_getCodeInfo(str, charset) {
  // Most of this code was taken directly from:
  // http://developer.mozilla.org/en/docs/nsICryptoHash
 
  var Cc = Components.classes;
  var Ci = Components.interfaces;
  var converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
                  createInstance(Ci.nsIScriptableUnicodeConverter);
 
  converter.charset = charset;
  // result is an out parameter,
  // result.value will contain the array length
  var result = {};
  // data is an array of bytes
  var data = converter.convertToByteArray(str, result);
  var ch = Cc["@mozilla.org/security/hash;1"]
           .createInstance(Ci.nsICryptoHash);
  ch.init(ch.SHA1);
  ch.update(data, data.length);
  var hash = ch.finish(false);
 
  // return the two-digit hexadecimal code for a byte
  function toHexString(charCode) {
    return ("0" + charCode.toString(16)).slice(-2);
  }
 
  // convert the binary hash data to a hex string.
  var hexHash = [toHexString(hash.charCodeAt(i)) for (i in hash)].join("");
 
  return {hash: hexHash,
          size: data.length};
};
 
// Resync with the herd every hour.
Herd.SYNC_INTERVAL_MS = 60 * 60 * 1000;
 
Herd.getSubscribedFeeds = function Herd_getSubscribedFeeds() {
  var jsm = {};
  try {
    Components.utils.import("resource://ubiquity/modules/setup.js", jsm);
  } catch (e) {
    Components.utils.import("resource://ubiquity-modules/setup.js", jsm);
  }
 
  var services = jsm.UbiquitySetup.createServices();
 
  if (services.feedManager) {
    // This is Ubiquity 0.1.5 or later.
    return services.feedManager.getSubscribedFeeds();
  } else {
    // This is Ubiquity 0.1.4 or earlier.
    var markedPages = services.linkRelCodeService.getMarkedPages();
    var feeds = [];
 
    function makeFeedFromPage(page) {
      var feed = new Object();
      feed.srcUri = page.jsUri;
      feed.uri = page.htmlUri;
      feed.__proto__ = page;
    }
 
    markedPages.forEach(
      function(page) {
        feeds.push(makeFeedFromPage(page));
      });
 
    return feeds;
  }
};
 
// Conditionally submit new information to the herd, if enough time
// has passed since we last sent information.
Herd.submitInfo = function Herd_submitInfo() {
  var req = new XMLHttpRequest();
 
  var data = {feeds: []};
 
  function isPublicUri(uri) {
    // TODO: If possible, we should also filter out intranet
    // URIs here too.
    return ((uri.scheme == "http" || uri.scheme == "https") &&
            uri.host != "localhost" &&
            uri.host != "127.0.0.1");
  }
 
  var feeds = Herd.getSubscribedFeeds();
 
  feeds.forEach(
    function(feed) {
      if (isPublicUri(feed.srcUri)) {
        var code = feed.getCode();
        if (code) {
          var feedInfo = Herd.getCodeInfo(code, "UTF-8");
          feedInfo.url = feed.uri.spec;
          data.feeds.push(feedInfo);
        }
      }
    });
 
  jQuery.ajax({
     url: Herd.SUBMIT_URL,
     type: "POST",
     dataType: 'text',
     data: { json: Utils.encodeJson(data) },
     error: function(req, error){
         Components.utils.reportError("Herd resync got status code " +
                                     req.status + " with text: " +
                                     req.responseText);
     },
     success: function(){
        displayMessage("Submited!");
     }
  })
};
 
function cmd_herd() {
  Herd.submitInfo();
}