Nativescript - Running code when app is close - javascript

I would know if, using Nativescript, is it possible to create an application that on specific event (onclick) "register" a javascript function that will run always and indipendently from the app (this means either if the app is open or close) until another event "unregister" the function.
If it is possible can you direct me to the right construct to manage this situation? I heard about workers but i cant understand is can use it to do this.

you can use background-tasks which will run when the app is sent to the background.. for a long term execution you would need the user permission.
nativescript docs: https://docs.nativescript.org/core-concepts/ios-runtime/how-to/BackgroundExecution
edit: you can wake a closed app into the background in respond to events.. (notification..etc) so a running app is either an active foreground app(visible on the screen) or a background app(running but not active, can be seen from the app switcher)..

Related

expo-linking: Linking back to app doesn't trigger eventListener on iOS, works fine on android

I’ve been struggling for days trying to figure out why the Linking eventListener doesn’t seem to work. I am setting up the event listener correctly:
Linking.addEventListener(“url”, handleStravaRedirect);
where, “handleStravaRedirect” is the callback function to be called after the user grants my app permission on the 3rd party website. The problem is, this function never gets called after the redirect back to my app. I thought I was doing something wrong, but when I tried the auth flow on android, the event handler triggered as expected.
Are there any extra steps I need to do to make Linking work on iOS? It seems like iOS handles the app being pushed to the background & the redirect differently than android handles it, almost like the Linking eventListener doesn't even know the user left the app in the first place.
Other information:
To make the Auth Request and open the web browser, I’m using: AuthSession.useAuthRequest
When I log “Linking.addEventListener” right above where it gets linked, the function is there and I receive no errors that adding the event listener failed, So I’m assuming that part is set up properly and the problem is purely on reopening of my app from the browser.
I’m having the same issue in Dev when I’m using the "exp:// " scheme and in Testflight when I use my custom “herofit://” scheme.
EDIT: This seems to be very similar to the issue I'm having
https://forums.expo.dev/t/expo-deeplinking-issue-with-addeventlistener/2254/14
I resolved my issue. I was unable to read the response because, I was using the “AuthSession.useAuthRequest” hook, and trying to listen to the response with “Linking.addEventListener”. Instead, I needed to to listen for the response with “response”, which was one of the methods returned on the useAuthRequest hook.

Html button that can redirect to your app or app store

I currently have two buttons but when viewing 9gag, they were able to group the two buttons into one. I was wondering how can I possibly do that.
First Button (which opens my app directly)
Use The App
Second button (which gives the option to download directly to playstore.
Download Here
May I ask how can I make it so that I have one button. If clicked on and the app is installed, it'll go to the app, otherwise go to the app store.
Use the window.navigator object to attempt to detect what OS the client is running, then change the href to point to the relevant URL.
If you're curious, you might learn more by typing navigator into your browser console and exploring its properties. navigator.platform seems like it could be useful.

Is there a way to prevent someone from closing a web app?

I work in a call center and we use an in-house app for dialing. The app is built using HTML/JavaScript for the front end and MSSQL/Node.js for the back end.
We have a problem where some people close the browser without logging out properly and this is causing data to be lost because the phone call is not being terminated correctly. Is there any way to prevent the user from closing the app using the 'X' button in Chrome?
I have already setup the generic Chrome message that warns of data loss if the browser is closed, but this isn't really doing the job.
I am looking for either some kind of JavaScript code to run in the app or perhaps something externally I can run on the local computer. Also, a Chrome startup command line switch would be perfect for the job too, but from my research, I am not sure one exists.
$(window).on("beforeunload", function() {
return confirm("Do you really want to close?");
})
There is no specific event for capturing browser close event.
You can only capture on unload of the current page.
By this method, it will be effected while refreshing / navigating the current page.
Ref

How to have a javascript function running in page context call puppeteer functions?

I am trying to make a bot that automatically plays a game on a web page. As of right now, I can navigate to the website, sign in, and load the game page, but I am stuck here.
I would like to inject a script at the webpage level that uses jQuery to constantly scrape the webpage and determine what state the game is in. When a certain event happens, I want the script to fire a custom event(?) that would notify a function at the Node.js./Puppeteer level to execute. My problem is that I do not understand how to make Puppeteer react when a custom event from the page is fired.
For example, let's say we have a webpage level function that is on a 1 second timeout that scrapes the webpage for X. When X is found, I want to scrape Y data off the webpage and send it up to the node level. When Y is received at the node level, it moves the mouse to Y location, or do something else with Y that cannot be done at the browser level.
I'm not sure if this is the most appropriate way to handle this kind of task, but trying to understand how async and promises work on the Node.js level without being able to use jQuery to select elements is giving me an advanced form of terminal ebola AIDS.
This answer has couple of different ways how to send data back from an injected script to your Node.js code:
Communicate "out" from Chromium via DevTools protocol

Continuous headless page automation with IPC

I need to make a headless (for a docker container) app that waits for an external signal and then acts on that signal by clicking on several html elements (selectors, buttons, links) and filling in some input fields. All this can be done using jQuery, I know how to do that.
The app needs to keep the page loaded so it can act immediately, reloading the page every time is taking too long. The whole action of receiving a signal and filling in a form and submit it, should be done under one second.
I made an electron app that does all this but I need to make the app headless so it can be run inside a docker container.
It looks like Phantomjs could do this but I see two problems:
The Phantom script needs to keep the web page loaded as the web page I need to automate is very heavy, it can take more than a minute to load.
The Phantom script needs to be able to receive a signal and report back on the progress. HTTP or file based is too slow, I'd like to use websockets for this communication.
I hope someone can point me to the right tools for this and/or point me to some examples how to achieve this.
I would like to use Javascript, but if there is a perfect solution in an another modern language, I have no problem to use that.
I managed to get it working inside a Docker container using Electron.

Categories