Overrides.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*jshint browser:true */
  2. /*global WebInspector:true, InspectorFrontendHost:true, InspectorBackend:true, importScript:true */
  3. /*global Preferences:true */
  4. // Wire up websocket to talk to backend
  5. WebInspector.loaded = function() {
  6. var webSocketUrl = function() {
  7. var a = document.createElement('a');
  8. // browser will resolve this relative path to an absolute one
  9. a.href = 'ws';
  10. a.search = window.location.search;
  11. a.protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
  12. return a.href;
  13. }();
  14. WebInspector.socket = new WebSocket(webSocketUrl);
  15. WebInspector.socket.onmessage = onWebSocketMessage;
  16. WebInspector.socket.onerror = onWebSocketError;
  17. WebInspector.socket.onopen = onWebSocketConnected;
  18. };
  19. var _inspectorInitialized = false;
  20. function onWebSocketError(error) {
  21. console.error(error);
  22. }
  23. function onWebSocketConnected() {
  24. if (_inspectorInitialized) return;
  25. InspectorFrontendHost.sendMessageToBackend = WebInspector.socket.send.bind(WebInspector.socket);
  26. WebInspector.dockController = new WebInspector.DockController();
  27. WebInspector.doLoadedDone();
  28. _inspectorInitialized = true;
  29. }
  30. function onWebSocketMessage(response) {
  31. var message = response.data;
  32. if (!message) return;
  33. if (message === 'showConsole') {
  34. WebInspector.showConsole();
  35. } else {
  36. InspectorBackend.dispatch(message);
  37. }
  38. }
  39. // Disable HTML & CSS inspections
  40. WebInspector.queryParamsObject['isSharedWorker'] = true;
  41. // disable everything besides scripts and console
  42. // that means 'profiles' and 'timeline' at the moment
  43. WebInspector._orig_panelDescriptors = WebInspector._panelDescriptors;
  44. WebInspector._panelDescriptors = function() {
  45. var panelDescriptors = this._orig_panelDescriptors();
  46. return panelDescriptors.filter(function(pd) {
  47. return ['scripts', 'console'].indexOf(pd.name()) != -1;
  48. });
  49. };
  50. // Patch the expression used as an initial value for a new watch.
  51. // DevTools' value "\n" breaks the debugger protocol.
  52. importScript('WatchExpressionsSidebarPane.js');
  53. WebInspector.WatchExpressionsSection.NewWatchExpression = '\'\'';
  54. Preferences.localizeUI = false;
  55. Preferences.applicationTitle = 'Node Inspector';
  56. WebInspector._platformFlavor = WebInspector.PlatformFlavor.MacLeopard;
  57. // Front-end uses `eval location.href` to get url of inspected page
  58. // This does not work in node.js from obvious reasons, and cause
  59. // a 'null' message to be printed in front-end console.
  60. // Since Preferences.applicationTitle does not include inspected url,
  61. // we can return arbitrary string as inspected URL.
  62. WebInspector.WorkerManager._calculateWorkerInspectorTitle = function() {
  63. InspectorFrontendHost.inspectedURLChanged('');
  64. };
  65. // Do not offer download of the edited file when saving changes to V8.
  66. // DevTools' implementation changes window.location which closes
  67. // web-socket connection to the server and thus breaks the inspector.
  68. InspectorFrontendHost.close = function(url, content, forceSaveAs) {
  69. delete this._fileBuffers[url];
  70. };
  71. // Let DevTools know we can save the content of modified files,
  72. // so that a warning icon is not displayed in the file tab header.
  73. // See UISourceCode.hasUnsavedCommittedChanges to understand why.
  74. WebInspector.extensionServer._onSubscribe(
  75. {
  76. type:WebInspector.extensionAPI.Events.ResourceContentCommitted
  77. },
  78. {
  79. postMessage: function(msg) {
  80. // no-op
  81. }
  82. }
  83. );
  84. // Front-end intercepts Cmd+R, Ctrl+R and F5 keys and reloads the debugged
  85. // page instead of the front-end page. We want to disable this behaviour.
  86. WebInspector._orig_documentKeyDown = WebInspector.documentKeyDown;
  87. WebInspector.documentKeyDown = function(event) {
  88. switch (event.keyIdentifier) {
  89. case 'U+0052': // R key
  90. case 'F5':
  91. return;
  92. }
  93. WebInspector._orig_documentKeyDown(event);
  94. };
  95. var orig_createResourceFromFramePayload =
  96. WebInspector.ResourceTreeModel.prototype._createResourceFromFramePayload;
  97. WebInspector.ResourceTreeModel.prototype._createResourceFromFramePayload =
  98. function(frame, url, type, mimeType) {
  99. // Force Script type for all node frames.
  100. // Front-end assigns Document type (i.e. HTML) to our main script file.
  101. if (frame._isNodeInspectorScript) {
  102. type = WebInspector.resourceTypes.Script;
  103. }
  104. return orig_createResourceFromFramePayload(frame, url, type, mimeType);
  105. };
  106. //
  107. // Open the main application file on startup
  108. //
  109. WebInspector.notifications.addEventListener(
  110. WebInspector.Events.InspectorLoaded,
  111. function() {
  112. WebInspector.resourceTreeModel.addEventListener(
  113. WebInspector.ResourceTreeModel.EventTypes.CachedResourcesLoaded,
  114. showMainAppFile,
  115. null
  116. );
  117. },
  118. null
  119. );
  120. function showMainAppFile() {
  121. var fileTabs = WebInspector.showPanel('scripts')._editorContainer._files;
  122. if (Object.keys(fileTabs).length > 0){
  123. // Some files are already opened - do not change user's workspace
  124. return;
  125. }
  126. var uiSourceCodes = getAllUiSourceCodes();
  127. var uriToShow = WebInspector.inspectedPageURL;
  128. for (var i in uiSourceCodes) {
  129. if (uiSourceCodes[i].uri() !== uriToShow) continue;
  130. WebInspector.showPanel('scripts').showUISourceCode(uiSourceCodes[i]);
  131. return true;
  132. }
  133. console.error('Cannot show the main application file ', uriToShow);
  134. }
  135. function getAllUiSourceCodes() {
  136. // Based on FilteredItemSectionDialog.js > SelectUISourceCodeDialog()
  137. var projects = WebInspector.workspace.projects();
  138. var uiSourceCodes = [];
  139. var projectFiles;
  140. for (var i = 0; i < projects.length; ++i) {
  141. projectFiles = projects[i]
  142. .uiSourceCodes()
  143. .filter(nameIsNotEmpty);
  144. uiSourceCodes = uiSourceCodes.concat(projectFiles);
  145. }
  146. return uiSourceCodes;
  147. function nameIsNotEmpty(p) {
  148. return p.name();
  149. }
  150. }
  151. var oldDetached = WebInspector.detached;
  152. WebInspector.detached = function () {
  153. oldDetached.apply(this, arguments);
  154. setTimeout(function () {
  155. location.reload();
  156. }, 400);
  157. };