-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
268 lines (238 loc) · 7.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
const panels = chrome && chrome.devtools && chrome.devtools.panels;
const elementsPanel = panels && panels.elements;
if (elementsPanel) {
elementsPanel.createSidebarPane('State', sidebar => {
elementsPanel.onSelectionChanged.addListener(() => sidebar.setExpression(`(${getPanelContents})()`));
});
}
/**
* @typedef {{
* panelState: object,
* previousPanelState: object,
* originalState: object
* }} State
*/
// The function below is executed in the context of the inspected page.
function getPanelContents() {
if (!$0) return;
const ng = window.ng;
let isAngular = false;
let isAngularJs = false;
let isAngularIvy = false;
const state = getPanelContent();
if (state) {
exportToWindow(state);
} else {
const message = 'Cannot retrieve angular state';
updateState({originalState: message});
return message;
}
return state.panelState;
/** @returns {State} */
function getPanelContent() {
let _panelContent;
try {
if (isAngularContext()) { // Angular 2+
_panelContent = getAngularContent(ng);
isAngular = true;
} else if (isAngularIvyContext()) { // Angular ivy
_panelContent = getAngularIvyContent();
isAngularIvy = true;
} else if (isAngularJsContext()) { // AngularJs
_panelContent = getAngularJsContent(window.angular);
isAngularJs = true;
}
} catch {
/* proceed */
}
return _panelContent;
}
/** @returns {boolean} */
function isAngularContext() {
try {
return !!ng.probe($0);
} catch {
return false;
}
}
/** @returns {boolean} */
function isAngularIvyContext() {
try {
return !!ng.getContext($0);
} catch {
return false;
}
}
/** @returns {boolean} */
function isAngularJsContext() {
try {
return !!window.angular.element($0).scope()
} catch {
return false;
}
}
function parseNgMajorVersion() {
if (window.getAllAngularRootElements) {
const rootElements = getAllAngularRootElements();
if (rootElements && rootElements[0]) {
const versionString = rootElements[0].getAttribute('ng-version');
return versionString.split('.')[0];
}
} else if (window.angular) {
return 1;
}
}
/** @returns {State} */
function getAngularJsContent(angular) {
const originalState = angular.element($0).scope();
const panelState = clone(originalState);
return {panelState, previousPanelState: clone(panelState), originalState};
}
/** @returns {State} */
function getAngularContent(ng) {
const probe = ng.probe($0);
const originalState = probe.componentInstance;
const panelState = clone(originalState);
addStateProp(panelState, '$context', probe.context);
addStateProp(panelState, '$debugInfo', probe);
return {panelState, previousPanelState: clone(panelState), originalState};
}
/** @returns {State} */
function getAngularIvyContent() {
let el = $0;
const owningComponent = ng.getOwningComponent(el);
const originalState = ng.getComponent(el) || owningComponent;
const panelState = clone(originalState);
if (owningComponent !== originalState) {
addStateProp(panelState, '$owningComponent', owningComponent);
}
addStateProp(panelState, '$context', ng.getContext(el));
addStateProp(panelState, '$directives', ng.getDirectives(el));
addStateProp(panelState, '$listeners', ng.getListeners(el));
return {panelState, previousPanelState: clone(panelState), originalState};
}
/**
* @param {object} state
* @param {string} name
* @param {*} value
*/
function addStateProp(state, name, value) {
if (value && Object.keys(value).length) {
// Properties added with defineProperty are shown in a light red color
Object.defineProperty(state, name, {value, enumerable: false});
}
}
/**
* Returns function that runs digest based on angular version.
* @returns {function(...[*]=)}
*/
function getDetectChangesFunc() {
return () => {
let result = 0x1F44D;
const state = stateRef();
try {
if (isAngularIvy && ng.applyChanges) {
// Angular 9+
ng.applyChanges(updateComponentState(state));
} else if (isAngular) {
if (state.panelState.$debugInfo._debugInfo) {
// Angular 2
updateComponentState(state);
state.panelState.$debugInfo._debugInfo._view.changeDetectorRef.detectChanges();
} else if (ng.coreTokens) {
// Angular 4+
const ngZone = state.panelState.$debugInfo.injector.get(ng.coreTokens.NgZone);
ngZone.run(() => {
updateComponentState(state);
});
}
} else if (isAngularJs && window.angular) {
updateComponentState(state);
angular.element($0).scope().$applyAsync();
} else {
console.error("Couldn't find change detection api.");
result = 0x1F44E;
}
} catch (e) {
console.error("Something went wrong. Couldn't run change detection.", e);
result = 0x1F44E;
}
return String.fromCodePoint(result);
}
}
/**
* Compares previous and current panel state, if something is changed applies it to original state.
* @param {State} scope
* @returns {object}
*/
function updateComponentState(scope) {
Object.keys(scope.originalState).forEach((prop) => {
if (scope.previousPanelState[prop] !== scope.panelState[prop]) {
scope.previousPanelState[prop] = scope.panelState[prop];
scope.originalState[prop] = scope.panelState[prop];
}
})
return scope.originalState;
}
/**
* Recursively searches the closest $ctrl property in scope.
* @param {object} scope
* @returns {string|object}
*/
function findCtrl(scope) {
if (scope && scope.$ctrl) {
return scope.$ctrl;
} else if (scope && scope.$parent) {
return findCtrl(scope.$parent);
} else {
return '$ctrl is not found. Component or directive with controllerAs might not used in selected scope. ' +
'See https://docs.angularjs.org/guide/component';
}
}
/** @returns {State} */
function stateRef() {
return window.__ngState__;
}
/** Updates state reference. */
function updateState(state) {
window.__ngState__ = state;
}
/** Adds shortcuts to window object and prints help message to console. */
function exportToWindow() {
updateState(state);
if (isAngularJs && !window.$ctrl) {
Object.defineProperty(window, '$ctrl', {
get() {
return findCtrl(stateRef().originalState);
}
})
}
if (!window.$applyChanges) {
window.$apply = window.$detectChanges = window.$applyChanges = getDetectChangesFunc();
}
if (!window.$state) {
['$state', '$scope', '$context'].forEach(method =>
Object.defineProperty(window, method, {
get() {
return stateRef().originalState;
}
}));
}
if (window.__shortcutsShown__) return;
console.log('\n\n');
console.log('%cAngular state inspector shortcuts:', 'color: #ff5252; font-weight: bold;');
if (isAngularJs) {
console.log(`%c $ctrl: %cComponent $ctrl property`, 'color: #ff5252;', 'color: #1976d2');
}
console.log(`%c $state/$scope/$context: %cElement debug info`, 'color: #ff5252;', 'color: #1976d2');
console.log(
`%c $apply/$applyChanges(): %cTrigger change detection cycle`,
'color: #ff5252', 'color: #1976d2'
);
console.log('\n\n');
window.__shortcutsShown__ = true;
}
function clone(object) {
return Object.assign(Object.create(null), object);
}
}