Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update inspectNode implementation #1105

Merged
merged 1 commit into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions app/adapters/web-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,13 @@ export default BasicAdapter.extend({
* Open the devtools "Elements" tab and select a specific DOM node.
*
* @method inspectDOMNode
* @param {String} selector XPath selector
* @param {String} name
*/
inspectDOMNode(selector) {
chrome.devtools.inspectedWindow.eval(`inspect($x('${selector}')[0])`);
inspectDOMNode(name) {
chrome.devtools.inspectedWindow.eval(`
inspect(window[${JSON.stringify(name)}]);
delete window[${JSON.stringify(name)}];
`);
},

/**
Expand Down
4 changes: 2 additions & 2 deletions app/routes/component-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ export default TabRoute.extend({
this.set('controller.inspectingViews', false);
},

inspectDOMNode({ selector }) {
this.get('port.adapter').inspectDOMNode(selector);
inspectDOMNode({ name }) {
this.port.adapter.inspectDOMNode(name);
},

actions: {
Expand Down
12 changes: 6 additions & 6 deletions ember_debug/adapters/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ export default EmberObject.extend({
},

/**
Inspect a specific element. This usually
Inspect a specific DOM node. This usually
means using the current environment's tools
to inspect the element in the DOM.
to inspect the node in the DOM.

For example, in chrome, `inspect(elem)`
For example, in chrome, `inspect(node)`
will open the Elements tab in dev tools
and highlight the element.
and highlight the DOM node.

@param {DOM Element} elem
@param {Node} node
*/
inspectElement(/* elem */) {},
inspectNode(/* node */) {},

_messageReceived(message) {
this._messageCallbacks.forEach(callback => {
Expand Down
43 changes: 27 additions & 16 deletions ember_debug/adapters/web-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,35 @@ export default BasicAdapter.extend({
},

/**
* Open the devtools "Elements" and select an element.
* Open the devtools "Elements" and select an DOM node.
*
* NOTE:
* This method was supposed to call `inspect` which is a Chrome specific function
* that can either be called from the console or from code evaled using `inspectedWindow.eval`
* (which is how this code is executed). See https://developer.chrome.com/extensions/devtools#evaluating-js.
* However for some reason Chrome 52+ has started throwing an Error that `inspect`
* is not a function when called from this code. The current workaround is to
* message the Ember Ibspector asking it to execute `inspected.Window.eval('inspect(element)')`
* for us.
*
* @param {HTMLElement} elem The element to select
* @param {Node} node The DOM node to select
*/
inspectElement(elem) {
/* inspect(elem); */
this.get('namespace.port').send('view:inspectDOMNode', {
selector: `//*[@id="${elem.getAttribute('id')}"]`
});
inspectNode(node) {
// NOTE:
//
// Basically, we are just trying to call `inspect(node)` here.
// However, `inspect` is a special function that is in the global
// scope but not on the global object (i.e. `window.inspect`) does
// not work. This sometimes causes problems, because, e.g. if the
// page has a div with the ID `inspect`, `window.inspect` will point
// to that div and shadown the "global" inspect function with no way
// to get it back. That causes "`inspect` is not a function" errors.
//
// As it turns out, however, when the extension page evals, the
// `inspect` function does not get shadowed. So, we can ask the
// inspector extension page to call that function for us, using
// `inspected.Window.eval('inspect(node)')`.
//
// However, since we cannot just send the DOM node directly to the
// extension, we will have to store it in a temporary global variable
// so that the extension can find it.

let name = `__EMBER_INSPECTOR_${(Math.random() * 100000000).toFixed(0)}`;

window[name] = node;

this.get('namespace.port').send('view:inspectDOMNode', { name });
},

_listen() {
Expand Down
2 changes: 1 addition & 1 deletion ember_debug/view-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export default EmberObject.extend(PortMixin, {
* @param {Element} element The element to inspect
*/
inspectElement(element) {
this.get('adapter').inspectElement(element);
this.get('adapter').inspectNode(element);
},

sendTree() {
Expand Down