ngrok.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. var ngrok = angular.module("ngrok", []);
  2. ngrok.directive({
  3. "keyval": function() {
  4. return {
  5. scope: {
  6. title: "@",
  7. tuples: "=",
  8. },
  9. replace: true,
  10. restrict: "E",
  11. template: "" +
  12. '<div ng-show="hasKeys">' +
  13. '<h6>{{title}}</h6>' +
  14. '<table class="table params">' +
  15. '<tr ng-repeat="(key, value) in tuples">' +
  16. '<th>{{ key }}</th>' +
  17. '<td>{{ value }}</td>' +
  18. '</tr>' +
  19. '</table>' +
  20. '</div>',
  21. link: function($scope) {
  22. $scope.hasKeys = false;
  23. for (key in $scope.tuples) { $scope.hasKeys = true; break; }
  24. }
  25. };
  26. },
  27. "tabs": function() {
  28. return {
  29. scope: {
  30. "tabs": "@",
  31. "btn": "@",
  32. "onbtnclick": "&"
  33. },
  34. replace: true,
  35. template: '' +
  36. '<ul class="nav nav-pills">' +
  37. '<li ng-repeat="tab in tabNames" ng-class="{\'active\': isTab(tab)}">' +
  38. '<a ng-click="setTab(tab)" href="#">{{tab}}</a>' +
  39. '</li>' +
  40. '<li ng-show="!!btn" class="pull-right"> <button class="btn btn-primary" ng-click="onbtnclick()">{{btn}}</button></li>' +
  41. '</ul>',
  42. link: function postLink(scope, element, attrs) {
  43. scope.tabNames = attrs.tabs.split(",");
  44. scope.activeTab = scope.tabNames[0];
  45. scope.setTab = function(t) {
  46. scope.activeTab = t;
  47. };
  48. scope.$parent.isTab = scope.isTab = function(t) {
  49. return t == scope.activeTab;
  50. };
  51. },
  52. };
  53. },
  54. "body": function() {
  55. return {
  56. scope: {
  57. "body": "="
  58. },
  59. template: '' +
  60. '<h6 ng-show="hasBody">' +
  61. '{{ Body.Length }} bytes ' +
  62. '{{ Body.RawContentType }}' +
  63. '</h6>' +
  64. '' +
  65. '<div ng-show="!isForm">' +
  66. '<pre ng-show="hasBody"><code ng-class="syntaxClass">{{ Body.Text }}</code></pre>' +
  67. '</div>' +
  68. '' +
  69. '<div ng-show="isForm">' +
  70. '<keyval title="Form Params" tuples="Body.Form">' +
  71. '</div>' +
  72. '<div ng-show="hasError" class="alert">' +
  73. '{{ Body.Error }}' +
  74. '</div>',
  75. controller: function($scope) {
  76. var body = $scope.body;
  77. $scope.isForm = (body.ContentType == "application/x-www-form-urlencoded");
  78. $scope.hasBody = (body.Length > 0);
  79. $scope.hasError = !!body.Error;
  80. $scope.syntaxClass = {
  81. "text/xml": "xml",
  82. "application/xml": "xml",
  83. "text/html": "xml",
  84. "text/css": "css",
  85. "application/json": "json",
  86. "text/javascript": "javascript",
  87. "application/javascript": "javascript",
  88. }[body.ContentType];
  89. var transform = {
  90. "xml": "xml",
  91. "json": "json"
  92. }[$scope.syntaxClass];
  93. if (!$scope.hasError && !!transform) {
  94. try {
  95. body.Text = vkbeautify[transform](body.Text);
  96. } catch (e) {
  97. }
  98. }
  99. $scope.Body = body;
  100. }
  101. };
  102. }
  103. });
  104. ngrok.controller({
  105. "HttpTxns": function($scope) {
  106. $scope.txns = window.txns;
  107. if (!!window.WebSocket) {
  108. var ws = new WebSocket("ws://localhost:4040/_ws");
  109. ws.onopen = function() {
  110. console.log("connected websocket for real-time updates");
  111. };
  112. ws.onmessage = function(message) {
  113. $scope.$apply(function() {
  114. $scope.txns.unshift(JSON.parse(message.data));
  115. });
  116. /*
  117. $("pre code").each(function(i, e) {
  118. hljs.highlightBlock(e)
  119. });
  120. */
  121. };
  122. ws.onerror = function(err) {
  123. console.log("Web socket error:" + err);
  124. };
  125. ws.onclose = function(cls) {
  126. console.log("Web socket closed:" + cls);
  127. };
  128. }
  129. },
  130. "HttpRequest": function($scope) {
  131. $scope.Req = $scope.txn.Req;
  132. $scope.replay = function() {
  133. $.ajax({
  134. type: "POST",
  135. url: "/http/in/replay",
  136. data: { txnid: $scope.txn.Id }
  137. });
  138. }
  139. },
  140. "HttpResponse": function($scope) {
  141. $scope.Resp = $scope.txn.Resp;
  142. $scope.statusClass = {
  143. '2': "text-info",
  144. '3': "muted",
  145. '4': "text-warning",
  146. '5': "text-error"
  147. }[$scope.Resp.Status[0]];
  148. }
  149. });