I'm trying to use Typescript together with Electron and RobotJS.
I'm a beginner with all of these technologies so I lack the deep understanding of what is happening behind the scenes so I cannot really connect the dots on this issue.
Electron and my app are working fine, everything compiles, but RobotJS won't work. The error I get is the following:
ERROR in ./node_modules/robotjs/build/Release/robotjs.node 1:0
Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type.
(Source code omitted for this binary file)
I tried rebuilding RobotJS for Electron but it still doesn't work.
Is this a native node module? I'm not sure, gotta read up on it first to figure out what I have to do with it if it indeed is.
Is this even achievable?
Thanks and have a lovely day!
LE: It would appear that I was trying to use robotjs that relies on node (main process) from angular (rendered process). When I moved what I was trying to do with robotjs from the angular component to main.ts that is ran by electron, it worked. I will try and find a way to use it from angular, I guess Inter-process Communication or something, since I couldn't find any other way for now.
Still waiting for ideas since I'm kind of in the dark right now.
Thanks!
Ok, figured this out.
Basically you cannot access Electron's Node.js API straight from Angular.
For that you need an awesome tool called ngx-electron. Read about it here or just Google it.
After you install this tool you can just DI its service as most guides instruct you and then use Electron remote to get access to robotjs.
Basically this:
const robot = this._electronService.remote.require('robotjs');
// The example supplied by robotjs
robot.setMouseDelay(2);
const twoPI = Math.PI * 2.0;
const screenSize = robot.getScreenSize();
const height = (screenSize.height / 2) - 10;
const width = screenSize.width;
for (let x = 0; x < width; x++) {
const y = height * Math.sin((twoPI * x) / width) + height;
robot.moveMouse(x, y);
}
Might not be the best solution but with my current limited knowledge about these technologies this will have to do.
I'm open to opinions tho.
Thanks for your time, have a nice one!
Related
I've been attempting to develop a Chrome extension that auto draws in skribbl.io for fun, and I'm really stuck on implementing dithering. It's just too slow. I've been trying to work out how to execute it in parallel, but to be honest, Javascript is just not my thing. I'm really uncomfortable with promises etc. It works by getting the canvas skribbl.io uses and sending input directly to it.
e.g
draw: function (coords) {
let startMouseCoords = getMouseCoords(coords[0]);
canvasElement.dispatchEvent(createMouseEvent("mousedown", startMouseCoords));
for (let i = 1; i < coords.length; i++) {
let mouseCoords = getMouseCoords(coords[i]);
canvasElement.dispatchEvent(createMouseEvent("mousemove", mouseCoords));
}
let endMouseCoords = getMouseCoords(coords[coords.length - 1]);
canvasElement.dispatchEvent(createMouseEvent("mouseup", endMouseCoords));
},
I was wondering if there would be any way to do this in a desktop application, as I feel much more comfortable in C++ or C# and I would likely have much better luck getting an algorithm of this sort to run at a decent speed. Is there any good way to do something like that through a desktop app? Perhaps even a Linux based browser that would let me interact with the web pages much more directly, instead of going through the Chrome extension system.
I have a Node client-side application with the latest ag-grid version.
I was using ag-grid-community without any issues with this require line
const {Grid} = require('ag-grid-community');
and this new
new Grid(agGridDiv, agGridOptions);
but if I change the require to
const {Grid} = require('ag-grid-enterprise');
the new fails with exception 'Grid is not a constructor'
How can I fix this? I have tried various changes such as new Grid.Grid etc but nothing seems to work.
For latest 23.1.1 version this page:
// ECMA 5 - using nodes require() method
const AgGrid = require('ag-grid-enterprise');
Another way to follow this guide, it all depends on which repository you download the dependencies from.
import {Grid, GridOptions} from '#ag-grid-community/core';
import {LicenseManager} from '#ag-grid-enterprise/core';
// or
const {Grid, GridOptions} = require('#ag-grid-community/core');
I used core and it worked for import.
For old version:
Grid, like everything else, needs to be imported from ag-grid-community.
1) ag-grid-enterprise is pure additive functionality for ag-grid-community.
2) You will use ag-grid-enterprise via the ag-grid-community api not explicit. Use ag-grid-enterprise for LicenseManager only.
Off-topic:
I would recommend starting with the old version, since the source code of the new version is minified and it will be more difficult for you to understand many nontrivial nuances.
I am relatively new to node (moving from PHP) and am encountering a problem that took me a while to figure out (meaning it took me a while to figure out what the problem was and now I have no idea how to fix it).
At the top of my node app (server.js), I have some requires. One of this requires my homemade api like this:
const express = require('express');
const app = express();
const v1 = require('./app/v1.js');
The v1 is just a function that returns the output to the api. However, there are many code problems with the v1 api as I have just wrote it and am testing it for the first time.
However, whenever there is a problem with the v1 function, node doesn't print it out and stop the program like it would if the problem were in the main server.js file. In fact (I am using nodemon so I don't have to restart each time), node just continues running like everything is moving perfectly smoothly. I understand why node would want to do this (they want people's app's to not be dependent on the quality of code of the module). However, this is not helpful for me as I have no idea where my problem is in the v1 module.
Does anyone know how to turn on errors/exceptions for modules? Any help appreciated.
Sounds like its doing its job just fine. Are you sure your v1 is returning the right value? or printing out the value that v1 is returning in server.js? because its not going to automatically print out the returned value, you still have to put it in a console.log(v1()); or something similar. without knowing what v1 actually returns, its hard to say what is suppose to be printed.
I need to "read" an array of some paperjs paths in nodejs and get their dimension. I wanted to use paper npm module but saw that it has a dependency to Cairo.
As I'm deploying to heroku it is a little difficult to use Cairo. I know its possible but I want to know if its really necessary just for "reading" the dimensions of a path group.
A present day answer: Yes it's possible.
———
To quote from the docs:
Paper.js comes in three different versions on NPM: paper, paper-jsdom and paper-jsdom-canvas. Depending on your use case, you need to require a different one:
paper is the main library, and can be used directly in a browser context, e.g. a web browser or worker.
paper-jsdom is a shim module for Node.js, offering headless use with SVG importing and exporting through jsdom.
paper-jsdom-canvas is a shim module for Node.js, offering canvas rendering through Node-Canvas as well as SVG importing and exporting through jsdom.
→ If you don't require rendering to canvas, paper-jsdom should do the trick (which doesn't need Cairo)
———
Important note:
If I understood correctly this means you can't use PaperScript, but have to use JavaScript directly. This will affect how you have to write code:
new paper.Path() instead of new Path()
Create project manually using paper.setup()
No mathematical operators (+ - * / %)
... (More info in the docs)
———
A simple example to create a line, log its bounding box size (and also export the SVG):
Note: I used this with the current Node LTS (v14.16.1)
package.json
{
"name": "paper-jsdom-example",
"main": "index.js",
"dependencies": {
"paper-jsdom": "^0.12.15"
}
}
index.js
const paper = require('paper');
const fs = require('fs');
var size = new paper.Size(300, 300)
paper.setup(size);
var path = new paper.Path();
path.strokeColor = '#348BF0';
var start = new paper.Point(100, 100);
var end = new paper.Point(200, 200);
path.moveTo(start);
path.lineTo(end);
console.log('width', path.bounds.width, 'height', path.bounds.height);
var svg = paper.project.exportSVG({asString:true});
fs.writeFileSync('punchline.svg', svg);
Did anyone set up something like this for himself using the existing
node.js REPL? I didn't think of a quick way to do it.
The way I do it today is using emacs and this:
https://github.com/ivan4th/swank-js
This module is composed of:
A SLIME-js addon to emacs which, in combination with js2-mode, lets
you simply issue a C-M-x somewhere in the body of a function def - and
off goes the function's string to the ..
Swank-js server (yes, you could eval from your local-machine
directly to a remote process) written in Node.js - It receives the
string of the function you eval'ed and actually evals it
A whole part that lets you connect to another port on that server
with your BROWSER and then lets you manipulate the DOM on that browser
(which is pretty amazing but not relevant)
My solution uses SLIME-js on the emacs side AND I require('swank-
js') on my app.js file
Now.. I have several issues and questions regarding my solution or
other possible ones:
Q1: Is this overdoing it? Does someone have a secret way to eval stuff
from nano into his live process?
Q2: I had to change the way swank-js is EVALing.. it used some
kind of black magic like this:
var Script = process.binding('evals').Script;
var evalcx = Script.runInContext;
....
this.context = Script.createContext();
for (var i in global) this.context[i] = global[i];
this.context.module = module;
this.context.require = require;
...
r = evalcx("CODECODE", this.context, "repl");
which, as far I understand, just copies the global variables to the
new context, and upon eval, doesn't change the original function
definitions - SOOO.. I am just using plain "eval" and IT
WORKS.
Do you have any comments regarding this?
Q3: In order to re-eval a function, it needs to be a GLOBAL function -
Is it bad practice to have all function definitions as global (clojure-like) ? Do you think there is another way to do this?
Actually, swank.js is getting much better, and it is now much easier to set up swank js with your project using NPM. I'm in the process of writing the documentation right now, but the functionality is there!
Check this out http://nodejs.org/api/vm.html
var util = require('util'),
vm = require('vm'),
sandbox = {
animal: 'cat',
count: 2
};
vm.runInNewContext('count += 1; name = "kitty"', sandbox, 'myfile.vm');
console.log(util.inspect(sandbox));
// { animal: 'cat', count: 3, name: 'kitty' }
Should help you a lot, all of the sandbox things for node uses it :) but you can use it directly :)
You might take a look at jsapp.us, which runs JS in a sandbox, and then exposes that to the world as a quick little test server. Here's the jsapp.us github repo.
Also, stop into #node.js and ask questions for a quicker response :)