I have some Javascript that is interacting with the DOM from a website.
document.location.href="https://www.example.com";
It works well from Chrome console, but I would like to run that code from the command line. How can I do it? NodeJS gives "document is not defined" error.
In order to use command line to inject Javascript code in a browser's DOM you actually need...a browser that runs Javascript code ad has a DOM.
You can go with something like Selenium or rely on a bridged library like https://github.com/prasmussen/chrome-cli
What you are looking for is called jsdom! https://github.com/jsdom/jsdom
When you run the code from browser the DOM api is included, so your code excutes accordingly and finds the the location object with information about the current document your on. When you try to run it from command line using node.js the DOM api is not included which is why it gives you "document is not defined" error. Node.js is for excuting server side js code.
Related
I know there are lots of questions about this already, but i cant seem to find one that works for me.
I am trying to launch a local file from a local html using cmd to pass command to launch file but it does not seem to work.
This is what i used so far:
<script type="text/javascript" language="javascript">
function RunFile() {
window.open('C:/Windows/System32/cmd.exe /c START %temp%/file.cpl');
}
</script>
someone pls help with this.
Lets just asume i can do this on IE window.open('C:/Windows/System32/cmd.exe); and it will open cmd.
My question is how do i pass some extra argument to make the cmd open my file from another location e.g. window.open('C:/Windows/System32/cmd.exe /c START %temp%/file.cpl');
You cannot run a program using a browser. You might be confused with Windows Scripting JScript, check: https://en.wikipedia.org/wiki/JScript
You may run apps using that (in windows shell). Check this: https://stackoverflow.com/a/15351708/1082061
You could do this using server-side binary execution on Nodejs using child_process.
Pro: Easy to use, just need a simple AJAX call to trigger execution from client to Node server.
Cons: Need to use a server instead of a single HTML page.
Rails will not let me run JS code, I have these errors
1)
2)
whenever you add JS code, the errors appear.
Some idea why this happening?
Just because you're getting error highlights in your IDE, doesn't necessarily mean your code is wrong. Try running your server, navigate to your site from your browser, and check the developer console. Do you still see javascript errors?
This warning (it is not an error) is being displayed because your IDE thinks that the variable $ is not defined in your code. However, it is not able to find out that $ is a global variable defined in the jQuery library, imported a few lines before.
The IDE is just saying that the presence of that variable is not guaranteed unless you properly import the needed libraries to make it exist (jQuery in this case). Your code should work properly. In order to identify errors in your javascript code, I would recommend you to use the built in console in the web browser.
I'm experimenting with a simple Javascript debugger for a WebView. I'd like to debug/control/inspect how some Javascript code is being executed inside my WebView.
I haven't found any solution other than using the WebChromeClient to receive the console messages.
Since I have access to the Javascript code I can add instrumentation code: a console.log call before each line, with a special message (e.g. "debugging line 3") that tells which lines have been executed.
It's quite rudimentary so I wonder if there's any better solution. It would be great if I could use the debugger statement to really control execution flow.
This is what I have been doing if I want to console.log() anything directly on the mobile browser so that debugging can be done on the actual device and not in emulator or similar...
I made JS debugger plugin and here is what it have:
it creates an absolutely positioned HTML element that is placed on top of the content and is semi transparent.
I made the JS logic that actually simulates what console.log() does and print out all desired information in mentioned HTML element
once plugin was done I simply used MoibileDebugger.log('what ever'); instead of console.log('what ever');
My code is still not published publicly but will do that soon, so that anyone can benefit from using it...
In any case this plugin can be made very quickly by anyone who is good in JS.
I'm trying to create a node.js app and
alert('Sample Alert');
is causing my program to crash. Node says
ReferenceError: alert is not defined
and then quits. I can use the alert function when running javascript on a regular html page, so I'm at a loss to understand why this is... Is this a separate module that I have to use with node.js?
The alert() function is a property of browser window objects. It is not really part of JavaScript; it's just a facility available to JavaScript code in that environment.
Try console.log("Hello World");
alert() function is only available when you execute JavaScript in the special context of browser windows. It is available through the window object.
Node.js is not intended for writing desktop applications (directly). It is mainly intended for writing server-side JavaScript applications. You can use following frameworks/packages (and many more) if you want to develop true desktop applications.
Electron
NW.js (previously, node-webkit)
NW.js is an app runtime based on Chromium and node.js. You can write native apps in HTML and JavaScript with NW.js. It also lets you call Node.js modules directly from the DOM and enables a new way of writing native applications with all Web technologies.
AppJS
Available as an standalone distributable and an npm package
Meanwhile, you can use console.log() to output a message in Node.js.
console.log('hello');
While these answers are "correct", as there is no alert function available outside of the browser, there's no reason you can't create one and then use it:
node -e "function alert(x){
x === 'undefined' ? console.log('undefined') : console.log(x); return;
};
alert('x'); alert();"
results:
x
undefined
Then you might not need to change your existing code or example or whatever.
You'll also need code to wait for a key. Here's a start:
process.stdin.on('char', function() {
var chunk = process.stdin.read();
if (chunk !== null) {
process.stdout.write('data: ' + chunk + 'got?\n');
}
});
alert function is for browsers. means front end..in nodejs for printing in cmd or bash you should use this one..
console.log("Sample alert");
you can print any variable or constant here... for printing variables just remove quotes
The alert() property is only allowed by browsers, not JavaScript.
Since things like DOM and alerts are made for your browser to execute, you cannot use them in your nodejs environment.
Use
console.log('Sample Alert');
or if you want to use DOM or alerts, follow-->
What you can do is, first make a separate .js file for your HTML file and then link those two...
Note: Make sure the path is right (For .ejs users, make sure to give a path relative to your public folder) and now you can use your alerts.
Hope this helps.....
I don't see where it is documented but I have been using global.alert() in my react-native code. I am using it for debugging purposes because I am running an Appium test so I don't have access to the console.log() output.
I am trying to write a program in C++ and wxWidgets that accesses YouTube and start the video with JavaScript.
It uses the YouTube JavaScript API, documentation for which is found here.
I wrote the following piece of code to play ‘O, Canada’, specifically the one here.
wxWebView *webview = wxWebView::New(this, wxID_ANY, "http://www.youtube.com/watch?v=zwDvF0NtgdU");
webview->RunScript("function onYouTubePlayerReady(playerId) {document.getElementById('watch-player').playVideo();}");
Running the above code fails to fulfill its intended purpose, giving me the following error and a crash:
....\src\msw\wxwebview_ie.cpp(762): "assert "document" failed in wxWebViewIE::GetDocument().
I know that my code successfully LOADS the page, but running the JavaScript (the RunScript() function) seems to result in the error.
I am using wxWidgets 2.9.3 on Windows.
This should have been fixed in revision 71030 which is more recent than the 2.9.3 build that you are using. You can get the updated code either through SVN or a daily snapshot. If that still doesn't fix it please file a bug on the wxWidgets Trac.
The problem is actually because I call the JavaScript too early, before the page is loaded. If I call it a bit later, it works.