Discord.js loadstring - javascript

I want to make a command for a bot to evaluate a string (run it as code). Is there a way to run a string as code in discord.js? Not rly having a code sample here, I just want to know of a function to do so.

discord.js is using Node which uses Javascript, so your solution to evalute a code from a string should be with the eval() method.
Eval on developer.mozilla.org
However, as soon as you implement eval(), you'll soon find out that they have access to basically your whole computer. I would not recommend you to use this.

Related

NEW Selenium IDE - how to run and store JavaScript?

I have a bunch of old test scripts that were written for the old Selenium IDE. I'm trying to update them to run with the new Selenium, but I'm having a really hard time figuring out what to do with bits of javascript and the new syntax.
For example, I have something like:
(command)STORE (Target) javascript{Math.floor(Math.random()*100000)} (Value) ReportNumber
But all Selenium does is store the javascript expression as a variable if I use that old Syntax. I saw that the new IDE wants us to instead use Run Script but I need to run the javascript and save it as a variable and none of the attempts I've made thus far have worked. Has anyone figure out HOW to use and run javascript successfully, and to save it as a variable. Examples if you have them, please!!
You now need to use the execute script command instead, like this:
execute script | return Math.floor(Math.random()*100000) | ReportNumber
The "target" of the execute script command is the JavaScript to execute, and the "value" is the name of variable you want Selenium to put the result into. The return in the JavaScript is not optional!

execute javascript from php

Sorry for this beginner question, I never used js server-side before.
here is my problem:
I have some javascript downloaded from a remote page (it's encrypted, I can't convert it to php), I need to execute it and read its output.
How can I do it? I'm thinking about something like this:
shell_exec('nodejs code...')
but how to pass the code? It's quite long, about 10 lines of javascript.
Another way would be to store the js to a file and run nodejs script.js, but that would be a useless and slow disk IO...
Important caveat about using exec/shell_exec
I feel the need to prepend a caveat about security to this answer. Always be careful when using exec or shell_exec. You almost always should not be taking data over the network to inject into a shell command for security reasons. Writing the script to a file would be much safer because there is no risk of command injection. If you are confident that this approach is required then I strongly advise you to.
Use the PHP function escapeshellcmd which will try to escape shell control characters.
Really ask yourself how much you trust the source? And how much you trust their security?
Having said that. Here's my original answer to the question as asked:
It sounds like the missing piece for your puzzle is the -e parameter for node. This will allow you to pass a script as part of the command invocation.
E.g.
C:\Users\Cmonahan>node -e "console.log('hello world');"
hello world
You can then use PHP exec or shell exec to get the output.
More information:
PHP shell_exec() vs exec()
Node CLI documentation
Edit: Regarding passing multiline arguments to the command line. This can be a bit of a minefield. For example: It depends on whether it is a Unix-like or Windows environment and then, if Unix-like, what shell is parsing the command.
See for example:
Windows: How to specify multiline command on command prompt?
End of line (new line) escapes in bash
I would recommend just making sure the argument is a single line. In the case of JS you can try minification first, which typically strips out all newlines, and see if that works for you.
Here's a popular PHP based minifier https://github.com/mrclay/minify I believe you should be able to install via composer.
dont know am not that at home with php, with isnt it true that php serves html files to the user at home, so why would it be any different from using a javascript file in a normal html page?
cant you just use:<script src="myscripts.js"></script>"
or <script>"with here your script"</script>
in the file that need to load this javascript?

How to develop a javascript library from an already existing npm module (codius)

never done this before.
I'm using https://github.com/codius/codius-host. Codiu§ development has been abandoned, but I want to salvage part of it to use for my own project. I really need to be able to run codius commands from browser, so I need to develop a library or what you call it.
var codius = require('codius')
codius.upload({host: http://contract.host}
codius-host comes packed with command-line integration,
$ CODIUS_HOST=https://codius.host codius upload
How do I make a .js script do what the command-line command does ?
also posted on https://stackoverflow.com/questions/31126511/if-i-have-a-npm-tool-that-uses-comman-line-commands-how-can-i-create-a-javascri
hard time asking this questions since don't know where to start. help.
Assuming that you have access to the codius-host source code you should find the piece of code which manages the command line arguments. I am sure that they do handle the command and the command line arguments from an entry module/function and than later delegate the real job to a different module/function. What you need to do is to provide correct parameters to the functions which the function/module that handles command line argument calls with command line parameters.
In addition to that there are some nodejs libraries that might imitate a command line call from the program itself. One of those I know is shelljs:
https://www.npmjs.com/package/shelljs
You might want to check this out as well. With this one without bothering with the source code you might be able to imitate command line behaviour.

Gnome shell extensions: stdout from GLib.IOChannel

So I'm making a Gnome Shell extension. And I want to be able to run some command. (The command is actually "synclient -m 100", but that is off topic)
So, what I have done so far is
s=GLib.spawn_async_with_pipes(null, ["synclient","-m","100"], null, GLib.SpawnFlags.SEARCH_PATH,null)
c=GLib.IOChannel.unix_new(s[3])
The first line spawns my process. It is definitely working.
s[3] is the file descriptor for the stout of the process. (It has something to do with pipes. Not really sure about the whole pipe thing.)
Anyway, my problem is that I can't seem to read anything from the output of synclient.
This is what I'm using for reference, but it seems that not all of the functions work. For example, I want to use add_watch, but that apperently doesn't work with gnome extensions.
I've tried using a bunch or read functions and specifically read_line_string, but they all have problems. For read_line_string it seems like it should all work except I can't figure out how to create a StringBuilder object to pass as an argument.
So, does anyone know how to get the output of a command?
Edit: also I'm kind of confused about which language the extensions use. I think it's javascript, but the docs I'm using seem to make me think Vala, whatever that is (I'm guessing a variation of java?).
Edit 2:
So, what I've got now is
let [res, pid, in_fd, out_fd, err_fd] =
GLib.spawn_async_with_pipes(
null, ["synclient","-m","100"], null, GLib.SpawnFlags.SEARCH_PATH, null);
out_reader = new Gio.DataInputStream({ base_stream: new Gio.UnixInputStream({fd: out_fd}) });
And to read a line:
let [out, size] = out_reader.read_line(null);
This gives me the output of the command, but it still doesn't give me any way to get some callback whenever the DataInputStream is changed. I need to be able to do something whenever there is a new line in the stream.
Gnome Shell extensions are usually written in JavaScript. They use JavaScript bindings to libraries like GLib that are written in C. There are also Vala bindings to those libraries, and that is the documentation you are looking at. Here is the documentation for the JS bindings, unofficial as yet.
StringBuilder is a Vala language feature that corresponds to GLib.String in JS.
How do you know add_watch() doesn't work? What do you expect and what does it do instead?

Using C libraries from a Gnome-Shell extension

I want to write a Gnome-Shell extension that can tell how long a session has not received any user input. I know that calling XScreenSaverQueryInfo will give me that information, but I can't find a way to call it from my gjs extension. What do I need to do to get this to work?
Probably the easiest way to do this is to use D-Bus to call the org.gnome.Mutter.IdleMonitor.GetIdletime method on the /org/gnome/Mutter/IdleMonitor/Core path of org.gnome.Shell. That will give you the time in milliseconds that the shell has not seen any user input for.
You can test this on the command line using:
while true; do
gdbus call --session --dest org.gnome.Shell \
--object-path /org/gnome/Mutter/IdleMonitor/Core \
--method org.gnome.Mutter.IdleMonitor.GetIdletime
done
You can use GIO’s D-Bus support from GJS to call the method from your extension. There’s an example here.

Categories

Resources