blob: 078c7adbfe60ef2f1f620eb4145bc4ce360e5173 [file] [log] [blame]
Jeff Thompson02b9f4d2012-11-29 14:40:57 -08001Components.utils.import("chrome://modules/content/ndn-js.jsm");
2
Jeff Thompsonbd829262012-11-30 22:28:37 -08003function ndnToolbarGetLatest(event){
4 if (window._content.document.location.protocol != "ndn:") {
Jeff Thompson35cfc262012-12-16 12:06:40 -08005 alert("The address must start with ndn:");
Jeff Thompson02b9f4d2012-11-29 14:40:57 -08006 return;
7 }
8
Jeff Thompsonbd829262012-11-30 22:28:37 -08009 // Parse the same as in ndnProtocolService newChannel.
Jeff Thompson02b9f4d2012-11-29 14:40:57 -080010 var spec = window._content.document.location.href.trim();
11 var preHash = spec.split('#', 1)[0];
12 var hash = spec.substr(preHash.length).trim();
13 var preSearch = preHash.split('?', 1)[0];
14 var search = preHash.substr(preSearch.length).trim();
15 // Set nameString to the preSearch without the protocol.
16 var nameString = preSearch.trim();
17 if (nameString.indexOf(':') >= 0)
18 nameString = nameString.substr(nameString.indexOf(':') + 1).trim();
19
20 var name = new Name(nameString);
21 var indexOfVersion = getIndexOfVersion(name);
22 if (indexOfVersion < 0) {
Jeff Thompsonbd829262012-11-30 22:28:37 -080023 alert("The ndn address does not have a version");
Jeff Thompson02b9f4d2012-11-29 14:40:57 -080024 return;
25 }
26
27 var nameWithoutVersion = new Name(name.components.slice(0, indexOfVersion));
Jeff Thompsonbd829262012-11-30 22:28:37 -080028 var searchWithChildSelector = (search == "" ? "?" : search + "&") + "ndn.ChildSelector=1";
Jeff Thompson02b9f4d2012-11-29 14:40:57 -080029
Jeff Thompsonbd829262012-11-30 22:28:37 -080030 var uri = "ndn:" + nameWithoutVersion.to_uri() + searchWithChildSelector + hash;
Jeff Thompson02b9f4d2012-11-29 14:40:57 -080031 window._content.document.location = uri;
32}
33
34// Return the index of the last component that starts with 0xfd, or -1 if not found.
35function getIndexOfVersion(name) {
36 for (var i = name.components.length - 1; i >= 0; --i) {
37 if (name.components[i].length >= 1 && name.components[i][0] == 0xfd)
38 return i;
39 }
40
41 return -1;
42}