Skip to content

Instantly share code, notes, and snippets.

@fernandotakai
Created October 9, 2009 17:29
Show Gist options
  • Save fernandotakai/206199 to your computer and use it in GitHub Desktop.
Save fernandotakai/206199 to your computer and use it in GitHub Desktop.
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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment