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 | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 11 | var uriParts = NdnProtocolInfo.splitUri(window._content.document.location.href); |
Jeff Thompson | 8107ec8 | 2013-01-12 21:53:27 -0800 | [diff] [blame] | 12 | var name = new Name(uriParts.name); |
Jeff Thompson | 02b9f4d | 2012-11-29 14:40:57 -0800 | [diff] [blame] | 13 | var indexOfVersion = getIndexOfVersion(name); |
| 14 | if (indexOfVersion < 0) { |
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 15 | alert("The ndn address does not have a version"); |
Jeff Thompson | 02b9f4d | 2012-11-29 14:40:57 -0800 | [diff] [blame] | 16 | return; |
| 17 | } |
| 18 | |
| 19 | var nameWithoutVersion = new Name(name.components.slice(0, indexOfVersion)); |
Jeff Thompson | 8107ec8 | 2013-01-12 21:53:27 -0800 | [diff] [blame] | 20 | var searchWithChildSelector = |
| 21 | (uriParts.search == "" ? "?" : uriParts.search + "&") + "ndn.ChildSelector=1"; |
Jeff Thompson | 02b9f4d | 2012-11-29 14:40:57 -0800 | [diff] [blame] | 22 | |
Jeff Thompson | 8107ec8 | 2013-01-12 21:53:27 -0800 | [diff] [blame] | 23 | var uri = "ndn:" + nameWithoutVersion.to_uri() + searchWithChildSelector + uriParts.hash; |
Jeff Thompson | 02b9f4d | 2012-11-29 14:40:57 -0800 | [diff] [blame] | 24 | window._content.document.location = uri; |
| 25 | } |
| 26 | |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 27 | /* |
| 28 | * Return the index of the last component that starts with 0xfd, or -1 if not found. |
| 29 | */ |
Jeff Thompson | 02b9f4d | 2012-11-29 14:40:57 -0800 | [diff] [blame] | 30 | function getIndexOfVersion(name) { |
| 31 | for (var i = name.components.length - 1; i >= 0; --i) { |
| 32 | if (name.components[i].length >= 1 && name.components[i][0] == 0xfd) |
| 33 | return i; |
| 34 | } |
| 35 | |
| 36 | return -1; |
| 37 | } |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * This is called when the connected NDN hub changes. |
| 41 | */ |
| 42 | function onNdnHubChanged(host, port) { |
| 43 | document.getElementById("ndnHubLabel").setAttribute("value", "Hub: " + host + ":" + port); |
| 44 | } |
| 45 | |
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 46 | window.addEventListener("load", function() { NdnProtocolInfo.addNdnHubChangedListener(onNdnHubChanged); }, false); |