blob: a187599a590fdcbd154faeabc0a05f7d1c3bec9c [file] [log] [blame]
Jeff Thompson02b9f4d2012-11-29 14:40:57 -08001Components.utils.import("chrome://modules/content/ndn-js.jsm");
2
3function CcnxToolbarGetLatest(event){
4 if (window._content.document.location.protocol != "ccnx:") {
5 alert("The addres must start with ccnx");
6 return;
7 }
8
9 // Parse the same as in ccnxprotocolService newChannel.
10 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) {
23 alert("The ccnx address does not have a version");
24 return;
25 }
26
27 var nameWithoutVersion = new Name(name.components.slice(0, indexOfVersion));
28 var searchWithChildSelector = (search == "" ? "?" : search + "&") + "ccnx.ChildSelector=1";
29
30 var uri = "ccnx:" + nameWithoutVersion.to_uri() + searchWithChildSelector + hash;
31 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}