Jeff Thompson | 02b9f4d | 2012-11-29 14:40:57 -0800 | [diff] [blame^] | 1 | Components.utils.import("chrome://modules/content/ndn-js.jsm"); |
| 2 | |
| 3 | function 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. |
| 35 | function 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 | } |