Jeff Thompson | 02b9f4d | 2012-11-29 14:40:57 -0800 | [diff] [blame] | 1 | Components.utils.import("chrome://modules/content/ndn-js.jsm"); |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 2 | Components.utils.import("chrome://modules/content/NdnProtocolInfo.jsm"); |
Jeff Thompson | 02b9f4d | 2012-11-29 14:40:57 -0800 | [diff] [blame] | 3 | |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 4 | function ndnToolbarGetLatest(event) { |
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 5 | if (window._content.document.location.protocol != "ndn:") { |
Jeff Thompson | 35cfc26 | 2012-12-16 12:06:40 -0800 | [diff] [blame] | 6 | alert("The address must start with ndn:"); |
Jeff Thompson | 02b9f4d | 2012-11-29 14:40:57 -0800 | [diff] [blame] | 7 | return; |
| 8 | } |
| 9 | |
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 10 | // Parse the same as in ndnProtocolService newChannel. |
Jeff Thompson | 02b9f4d | 2012-11-29 14:40:57 -0800 | [diff] [blame] | 11 | var spec = window._content.document.location.href.trim(); |
| 12 | var preHash = spec.split('#', 1)[0]; |
| 13 | var hash = spec.substr(preHash.length).trim(); |
| 14 | var preSearch = preHash.split('?', 1)[0]; |
| 15 | var search = preHash.substr(preSearch.length).trim(); |
| 16 | // Set nameString to the preSearch without the protocol. |
| 17 | var nameString = preSearch.trim(); |
| 18 | if (nameString.indexOf(':') >= 0) |
| 19 | nameString = nameString.substr(nameString.indexOf(':') + 1).trim(); |
| 20 | |
| 21 | var name = new Name(nameString); |
| 22 | var indexOfVersion = getIndexOfVersion(name); |
| 23 | if (indexOfVersion < 0) { |
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 24 | alert("The ndn address does not have a version"); |
Jeff Thompson | 02b9f4d | 2012-11-29 14:40:57 -0800 | [diff] [blame] | 25 | return; |
| 26 | } |
| 27 | |
| 28 | var nameWithoutVersion = new Name(name.components.slice(0, indexOfVersion)); |
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 29 | var searchWithChildSelector = (search == "" ? "?" : search + "&") + "ndn.ChildSelector=1"; |
Jeff Thompson | 02b9f4d | 2012-11-29 14:40:57 -0800 | [diff] [blame] | 30 | |
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 31 | var uri = "ndn:" + nameWithoutVersion.to_uri() + searchWithChildSelector + hash; |
Jeff Thompson | 02b9f4d | 2012-11-29 14:40:57 -0800 | [diff] [blame] | 32 | window._content.document.location = uri; |
| 33 | } |
| 34 | |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 35 | /* |
| 36 | * Return the index of the last component that starts with 0xfd, or -1 if not found. |
| 37 | */ |
Jeff Thompson | 02b9f4d | 2012-11-29 14:40:57 -0800 | [diff] [blame] | 38 | function getIndexOfVersion(name) { |
| 39 | for (var i = name.components.length - 1; i >= 0; --i) { |
| 40 | if (name.components[i].length >= 1 && name.components[i][0] == 0xfd) |
| 41 | return i; |
| 42 | } |
| 43 | |
| 44 | return -1; |
| 45 | } |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * This is called when the connected NDN hub changes. |
| 49 | */ |
| 50 | function onNdnHubChanged(host, port) { |
| 51 | document.getElementById("ndnHubLabel").setAttribute("value", "Hub: " + host + ":" + port); |
| 52 | } |
| 53 | |
Jeff Thompson | e843502 | 2012-12-30 22:06:50 -0800 | [diff] [blame] | 54 | window.addEventListener("load", function() { addNdnHubChangedListener(onNdnHubChanged); }, false); |