How to access a known Ember component from console - javascript

Using Ember debug Chrome extension, I have identified this component in a website I am trying to automate (but do not have direct access to change the code):
<MYAPP#component:zipcode-field::ember592>
Which is shown in hierarchy as:
application
engine
myui
zipcodeField
If I edit the value property of that element in the debugger, it updates the UI and model as desired. Can I do this via a one-liner from the console?
Update: So far, I am able to enter this in the console:
Ember.lookup.$E.container.lookup("MYAPP#component:zipcode-field")
But unable to access/alter its value property as in the debugger.
Update:
In feedback to one of the answers, my aim is to have a console one-liner, which could be given to someone without any debuggers installed in order to run the code with the same behaviour. Using a variable such as $E within the console requires that the element has been manually selected prior to running the code, which would not be sufficient.

Correct me if I am wrong but it seems that you aren't using the ember inspector (available on firefox and as a bookmarklet).
Once you have that installed it is really easy to inspect debug and modify anything ember related, for the purpose of this answer I will be using the chrome version.
Open up your chrome dev tools in the tab that has your ember app running,
once there head to the ember tab in the developer tools.
In order to see the components you will have to tick a checkbox
Once enabled you will be able to see all of the components currently used.
Clicking on a component will open up a panel that contains all of the component's properties.
In order to access those properties from the console all you need to do is click on the $E next to the components.
Once clicked you should see something similar in the console.
Ember Inspector ($E): Class {helperName: (...), logout: (...), isOpenBinding: StreamBinding, currentUserBinding: StreamBinding, _morph: Morph…}
Now all you need to do in order to get the property values:
$E.get('myProperty');
And To set them:
$E.set('myProperty', newValue);

A component is just a view, so the following should work:
Ember.View.views[<GUID>]
So in your example:
Ember.View.views['ember592']
You need to use get/set if you want to modify/read the value property, for example:
Ember.View.views['ember592'].get('value')
Ember.View.views['ember592'].set('value', 'newValue')

Found a gist that works with Ember 2.13
App.__container__.lookup('-view-registry:main')[componentId]; // componentId i.e. "ember4322"
Credit goes to ngyv: https://gist.github.com/ngyv/819e2cc78eca2a3b19599707e1461205

Related

How do I access the redux store in Safari?

Context
I'm trying to debug a React application but cannot modify the source code to log redux variables. In chrome I'm able to access the redux store via the associated extension but it seems no such equivalent exists for safari.
Question
How can I access the redux store in Safari? Can I do so using the console?
The simplest solution, unfortunately, is to modify the source code to set a global variable to the Redux store. (It may be worth preemptively modifying any applications you can control to do this, just to make Safari easier to debug.)
Without modifying the source code, it should be possible, although it's awkward. The following instructions work for React 16.12.0.
In Safari's Web Inspector (dev tools), go to the Elements tab and find your React root element (the <div id="root"> or similar that you pass to ReactDOM.render).
Click on it. Web Inspector should show a = $0 next to it, indicating that you can now reference that DOM node in the Web Inspector console as $0.
In the Web Inspector's Console tab, type Object.keys($0) and press Enter to see the internal properties that React adds to the DOM node. For my app, I see ["__reactContainere$8yexuoe6iiv", "_reactRootContainer"].
Dump the internal React object to the console by typing $0["__reactContainere$8yexuoe6iiv"] (substituting your internal property name) and pressing Enter.
Inspect the object properties to find the Redux store: on my app, it's under child, under memoizedProps, under store, but this may depend on the specifics of your React component hierarchy and where and how you mount Redux's <Provider>.
Use the store reference you just found to call Redux's getState. For my app, that means typing $0["__reactContainere$8yexuoe6iiv"].child.memoizedProps.store.getState() and pressing Enter.
A simpler one-line alternative to the above:
document.getElementById('root')['_reactRootContainer']._internalRoot.current.child.memoizedProps.store.getState()
In case you are using Nextjs framework, you can achieve this by opening the console in safari. Type window in it. Expand it. Now just check in the window object property. You will find a key something like '__REDUX' or something like that. In my case it was __NEXT_REDUX_STORE__.
Now after you find it just enter the following in your console.:
__NEXT_REDUX_STORE__.getState();
you can now check your current redux state of your application.
I'm not aware of a safari extension for redux debugging (corrections welcome). This thread suggests that it's due to a lack of a dev-tools API: https://github.com/zalmoxisus/redux-devtools-extension/issues/435
Redux state isn't in the global scope, so you won't be able to access it through the console without modifying the source code.
You could just extract state using the connect function. Then just stringify it?
<pre>JSON.stringify({this.props.store, null, 2})</pre>
Then you could visually see it.
Adding to #josh-kelley's answer...
In 2021, tested with Safari Version 12.1 (14607.1.40.1.4), following line did the trick for me:
document.getElementById('root')['_reactRootContainer']
._internalRoot.current.child.memoizedProps
.children.props.store.getState()
Notice the children.props difference from what #josh had mentioned.
According to its GitHub repository for other browsers use remote-redux-devtools.

Debugging ReactJs in Chrome on page load - this and everything else is undefined when using console

I have a problem when debugging React on initial page load. As you can see from the screenshot, if I hover over this I can see the content but if I use console it says that this is undefined. This only happens when I reload the page, if I debug when clicking around this problem does not occur. I have React Developer Tools for Chrome installed and noted that when this is happening it only says Connecting to React…. Could this be the problem?
https://chrome.google.com/webstore/detail/react-developer-tools/fmkadmapgofadopljbjfkapdkoienihi?hl=en
I'm using .tsx files (.jsx but with TypeScript) if that could matter.
Sources tab:
React tab:
Based on your updated comments, if you launch your function from the constructor and then stop it with a breakpoint, the constructor won't finish executing, which means it won't be able to correctly set the prototype chain and most important, set the correct reference to this.

List all functions of jquery select

I'm trying to explore a jquery plugin.
var fb = new FormBuilder();
$(#fb).someMethod();
console.log($(fb)) How can I list all the functions/methods that I can use here?
Off the top of my head, here are the options you have:
1- Check the plugins' documentation. This is my prefer option because exploring the list of functions and methods DON"T necessarily describe the object's behaviour. However, documentation usually does
2- If the plugin is open source, then explore the internals by yourself
3- dump the object to the console using console.log. IMO, Google Chrome has one of the best (if not the best) developer tools integrated to the browser
4- Similar to the above you can add a breakpoint or a debugger statement to pause execution of javascript wherever you want and then explore the object in question
Below is a screenshot example of Chrome's Dev tools where I placed a breakpoint somewhere on this Stackoverflow page. You can see the StackExchange's object definition

How to load Component and its Classes in my website

I am trying to over ride one the notification popups of browser using the following code:
var branch = Components.classes["#mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
But in Mozilla Firefox, I get the error Component.classes is undefined.
And in Chrome Browser, I get the error Component is undefined.
Well I have realised I need to include something in my website. But I am unable to find exactly what is required.
Please anybody help. I googled about it a lot, but I have never used this thing before(the Classes) and I am unable to search what will help me out. i dont even have any idea that what will be the tags for this thing. I have never used Component or its classes
My website is in ZF2.
Components Object is non-standard feature. See https://developer.mozilla.org/en/docs/Components_object.
It also says
Warning: This object is only intended for code running with chrome
privileges. Exposing the object to regular web code was a mistake.

console.log() in windows 8 javascript/visual studio 2012

When I use console.log("Some text output") in javascript in Visual Studio 2012 for Windows 8 metro programming where is the text being sent to? Where can I view it? I've got the "Output" panel up but I do not see the log text.
Am I not using the right function? Is there another function I must use to print to Output?
To actually see the JavaScript console select DEBUG > Windows > JavaScript Console in the Visual Studio menus. Then you can use the good old console.log() to write to it, but you can also use the related WinJS functions:
There is WinJS.log() to log something after creating the function with WinJS.Utilities.startLog():
WinJS.Utilities.startLog({type: "info", tags: "custom" });
WinJS.log("my message", "info", "custom");
Here's some additional information on the WinJS logging functions:
startLog(options): Configures a logger that writes messages containing the specified tags to the JavaScript console.
The options object must contain a type which is supposed to be one of error, warn, info or perf. Unless you want to capture all log entries, also add tags to the object containing a space-separated list of tags you want to be logged.
WinJS.log(message, tags, type): This function does not exist by default. However, it is created by your startLog() call. You could also create it manually but since you want logging to the console using startLog() is the way to go.
If you need to debug the application in a live environment, you can use a simple on-screen console.log replacement, which you can activate using the ~ key.
Source code here:
https://gist.github.com/4012355
You can also view the log through the EventViewer. Here are the steps to enable this:
Open Event Viewer
Navigate to Application and Services Log
Expand Microsoft/Windows/AppHost
Left click to select Admin
Right click Admin and then select View => Show Analytic and Debug Logs
I might be replying to an old post, or maybe you have discovered your way to serve your purpose, but I tried console.log() and WinJS.log() and the javascript console seem to return 'undefined' for both. Also, I made sure with 'debugger;' statement that that code was is executing. Now I am assuming here that you need logging for debugging purposes then what I would prefer to "print" to my screen now is:
<body>
<label id="lblMessage">test</label>
</body>
and then in JS :
function f(message) {
document.getElementById('lblMessage').innerText = message;
}
( and then, place a call to this function where needed - in my case it was, app.onactivated function)

Categories