How to get the current foreground application in electron(Javascript) - javascript

How can I detect if, for example, a browser is currently open?
I need this in my electron-application.
ty :)
I've found nothing like this online.
I've only found how I can check which window is open from the windows I have in my own application, but I need to know, what else is opened.
It should be something like this:
if(Application.isOpen('Google Chrome'){}

Unless someone has built a specific electron api to do this (which I can't find), then from electron...no. However, the beauty of electron being built with node.js, means that any node module should be able to do the job for you.
For example, ps-list should be able to get you all currently running processes.
psList().then(processes => {
console.log(processes)
})
Which gives a list for me, including:
Just be aware that you need node access from the electron thread attempting to use this lib.
This can easily be abstracted to do a name search in the list for you to get your desired functionality.

You can use find-process in case you need to search by given name, name pattern or pid.

Related

Using JavaScript in a NodeJS environment, can you find a window handle value of a running application?

I'm writing some tests for my React-Native application (using JS) in a NodeJS environment. In one scenario, I need to attach to an already-running Windows application. In order to attach to this Application, I need to know the NativeWindowHandle value.
For example, if you open Inspect.exe on a window, you'll find the "NativeWindowHandle" hex value.
Is there anyway I can find this value programmatically?
What I've Tried:
I'm able to find the PID of the app using:
const exec = require('child_process').exec;
exec('tasklist', function (err, stdout) {
....
}}
However, I haven't been able to turn that into the window handle. Does anyone have any ideas here? Is this possible?
This can be reliably accomplished by writing a native (C++) node addon which calls the appropriate Windows API functions and passes the results back to JS land.
eg you might want to call FindWindowEx and Windows will find and return the HWND (native window handle) of the matching open window. Or use one of the enumeration functions if you need to do the search yourself.
I did a quick search of npm and it looks like there might be a few packages that have done this work already, but you'll need to evaluate them.
If none of the npm packages will work, you'll need to write it yourself. This isn't too hard if you have a little C++ knowledge, but alternatively you might be able to get away with using node-ffi, which lets you write everything in JS and marshals the native calls for you.
(Using ffi will be a little slower than writing the native module yourself, but for your purposes that doesn't really matter. Either native or ffi will be much faster than spawning child processes.)

Distinguish between different linux distros in nodejs

Basically I want to be able to run different code depending on what os you have.
I've found out that the os.platform() function will return "win32", "win64", "darwin", or "linux" (possibly others?), but I can't seem to get any more specific information.
Ideally I want to be able to tell if Gnome, Unity, KDE, or some other desktop environment is being used.
Getting the active desktop environment/window manager is not a node-specific problem. There are different approaches (some better than others) that include using pgrep to check running process names against known DE/WM binary names and using other tools such as HardInfo or wmctrl.
What I ended up using was a bash scripts from mscottnielsen. It seems to use the best of many different commands to find out what desktop environment is being used. Unfortunetally, it's kind of hard to figure out the exact string that gets outputed by it (It doesn't say anywhere what strings get outputed), but other then that it does the job.
See the script here.

How can I write a script to automatically walk through a process like my user would?

I have some valuable processes on my site that I'd like to track regularly to make sure they are working. I wrote some javascript that will run the actions if the starting page contains a particular parameter, but I can't figure out how to properly execute the script without opening the page in a browser.
My best guess is I need some sort of chron driven bot for this, but I don't even know where I should begin with that and haven't found anything in my searching. I tried a cURL request, but it doesn't seem to fire the js. Really, if I could just find a way to properly initialize the js with a chron job that would be sufficient.
The key here is that I need it to execute the javascript so I can imitate user actions.
I'm working on a WordPress install, so it would need to be a php or javascript based solution. How can I build something like this?
Use an interaction testing framework like Ember.js. that should allow you to test your UI Interactions.
See the link above to get some detailed information on how to use the library.
Here is a code snippet from the Ember.js library to see if a user is
redirected properly if not authenticated (100% javascript!):
module('Integration: Transitions', {
teardown: function() {
App.reset();
}
});
test('redirect to login if not authenticated', function() {
visit('/');
click('.profile');
andThen(function() {
equal(currentRouteName(), 'login');
equal(currentPath(), 'login');
equal(currentURL(), '/login');
});
});
Ember.js is an excellent way to test your user interactions and your UI components.
Learn more here: http://emberjs.com/guides/testing/testing-user-interaction/
UPDATE:
See this answer for another solution that combines CasperJS and PhantomJS to test user interfaces.
Good luck!
If you don't want to have a browser open to do it you could use a headless browser like PhantomJS

is there a difference between alert() vs notification.alert() using phonegap in xcode?

I'm trying to figure out how to fix the titles on my messages that pop up in my iOS app I am attempting to work on seeing as the messages tend to pop up with a long path of where the file is, then the message which is to a point counter productive for the needs of the popup. That said. I started searching for how to fix it and I came up with the notification.alert(). I am assuming that the standard alert() I am using is binded to that with the way cordova/phonegap works. But does this mean I should instead of alert('message') use notification.alert() if so. Then how can I fix the one that is auto generated by the app when I am looking for geolocation information?
As requested "What am I using for geolocation"
geocoder = new google.maps.Geocoder();
if(navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
}
Which is what I do in my web based apps. I know this may not be the ideal solution for phonegap/cordova specifically. So I am searching for the right answer to this as well. But mostly the alerts. I am currently porting over an existing web based app to a phonegap version for iOS so the original question is should I remain using alert() where I do in my web based version or should I convert those as well to notification.alert() or does it really make that big a difference.
Like Noogen already mentioned, you should use notification.alert if you want it to look native and you want to customize the title etc.
For iOS 6 and above, to change the alert asking for permission to use current location, you can set the value for key NSLocationUsageDescription (or Privacy - Location Usage Description) in your app's Info.plist. The title of the alert will still be "YourAppBundleName" Would Like to Use Your Current Location. The value of the NSLocationUsageDescription will be shown as an explanation below the title.
There are similar properties for other permission dialogs as described in Apple's Information Property List Key Reference.
What Google geolocation code are you using that is doing the alert? API should not be doing any alert. The alert and notification.alert are two different functions.
Default alert webview/browser display the page URL in the title. Phonegap people did a great thing by providing the additional notification (alert, prompt, and confirm) API. This is a feature/benefit of PhoneGap. It is also required if you don't want to be rejected by Apple with app must be native look and feel clause. Just change all your alert to notification.alert.
You can also hack/override default alert with window.alert = notification.alert, but I do not recommend this.
Alternatively, you can do something like my AngularJS phonegap $notification friendly factory shown in my response here: Angularjs + phonegap logout on history back

Ideas needed. Javascript+XPCOM+C++ add-on

So, there is a WebRTC inside Firefox and there is a convenient class for making RTC communication possible called RTCPeerConnection which can be instantiated and used from the JavaScript app. You can find some decent example of it on [1].
And here am I with my custom transport (if you're interested - [2]) would like to use it for RTC communication. Briefly, I need to "substitute" the transport layer of WebRTC engine by my custom transport while providing the same RTCPeerConnection-like JavaScript interface for the user. And preferably, it should not look like custom build of Firefox (no patches).
So I've come up with the idea of extension, which will be written in C++ (since it need to be linked with WebRTC library and my custom transport library) and somehow will expose its interface to Javascript. And I've found XPCOM which, as I thought, can provide me this.
So I've started to fight with out-dated and sparsed info on this topic and after 3 days of struggling finally ended up with builded add-on. Unfortunately, I can't access it from Javascript, because of Javascript's "Components.classes is undefined" error. And it seems that there is no way to access it at all. Or I'm wrong on that?
Here is Javascript:
function check()
{
console.debug("checking...");
const {Cc,Ci,Cu} = require("chrome");
var rtc = Components.classes["#named-data.net/ndnrtc;1"].createInstance();
rtc = rtc.QueryInterface(Ci.ndINrtc);
console.debug("rtc: "+rtc);
}
My component is visible with XPCOM Viewer addon and the code above I can execute in the console while empty page is open in Firefox.
Given all that, I would like to ask Firefox experts regarding possible approaches which I can take in order to implement my idea.
Thank you in advance
1 https://apprtc.appspot.com/
2 http://named-data.net
Finally, I've figured out one possible solution for that and describe it in my post

Categories