I'm hoping this is the right place for this question since it centers around JavaScript. In the new iOS 12 Shortcuts app you can create workflows. I want to create one that simply launches a web page, fills in my username and password, then clicks the submit button, something like this...
document.myForm.username.value = 'myUsername';
document.myForm.password.value = 'myPassword';
document.getElementById('loginSubmit').submit();
But before even getting that far I just want to run an alert(1);. I can't get that to happen. I keep getting this error message below...
Run JavaScript on Web Page failed because Shortcuts couldn't convert
from URL to Safari web page.
I'm not sure what that means. I haven't been able to find info or tutorials on how to use this. Does anyone know how to get JavaScript to run? Thanks!
the first thing you need to know is that docs are here: https://support.apple.com/guide/shortcuts/welcome/ios
Now, the problem is that "Run Javascript on web page" need an input called "Safari web pages" but Open urls only gives as output the result of opening the url that is showing the page in safari, in order to make it work you need Open url to give you a "Safari web page" item.
The only way that I found to do that is to use the shortcut as a "Share extension"
In your workflow delete the URL item
Go to the shortcut settings page by tapping the icon
Settings icon
Now tap on "Show in Share Sheet"
In the "Accepted types" section, select only URLs that is at the very bottom of the list.
Finally go to safari, open the url you want and tap the share button, if this is the first time you do this, you need to active the "Shortcuts" section, in the bottom list go to the end and tap in "More", the scroll down and select "Shortcuts"
After doing that you will have the Shortcuts option, tap on it and select your Shortcut and it will run the java script in the page.
Is important to note that you will need to do something with the output of "Run javascript on web pages" like showing the result in one alert, because the way you have your workflow now, it may look like nothing is happening.
There's a note in the Run JavaScript on Web Page action that says, "Safari Web Page item in shortcuts stating that they are only available when running your shortcut as an Action Extension in Safari."
So, sadly, it's not possible to chain up actions where it launches a URL and then runs JS. You must visit the url and run the action from the share sheet.
The Safari actions require Safari to be open to the page you want when you launch the activity. That said, you can accomplish this easily using another element.
I don’t know your technical level, so I apologize if any of this is stuff you already know, but for you and for future reference to anyone looking, here is the howto.
First, go to the page you want on a Mac/PC browser and open the developer tools. I use Firefox Developer Edition, but Safari and Chrome have them also. If you’re using MacOS Safari, you will first need to open Safari’s Preferences menu and check “developer options” and relaunch.
Once the developer tools are open, switch to the Network tab / pane; find and turn off the option which clears the log on each page load. Keep this pane open, fill out the form, hit the trash can to clear the network log, and then submit the form. Now the HTTP request of the form submission should be either the first or the only thing in the list.
Select the line for the form submission and look at the request header. There should be a Request Method, Request Url, and all the key-value pairs that were submitted with the form.
Back in the Shortcuts app, add a URL element and give it the same URL as the Request Url. Chain this URL input element to a Get Contents of URL action element and toggle open the Advanced drawer. If there are any additional headers in the request log, you can tap Add new header to add more. Match the Request-Type to the Method field, choose “Form” as the Request Body, and then add in all the keys and values from the request.
Anyways, that’s it.
Chain this to Set Variable then use an IF block to test if the login succeeded or not. If it succeeded, make a Show Result that says “You are now logged in.” and if it failed, show the variable so that you can debug the response.
For API requests in shortcuts, I typically use the Get Contents of URL shortcut, which allows you to use different HTTP methods (POST, GET, etc) and adjust the URL parameters.
In your case, once you authenticate, you could use the authentication token to make other requests as needed.
View Apple's documentation here for an example
I am writing a PowerShell script that accesses a website in order to upload an image
When it presses a button, it throws up windows explorer to select a file. I want my script to enter a preselected url into the text field, however this windows explorer screen is not part of the actual document. This means I cannot access as I would a textbox or textarea.
I have the aim of translating this script into JavaScript (since PowerShell and JavaScript use almost identical code for accessing webpages), so a way to do this in (ideally native) JS would also be appreciated.
The webpage in question is: http://imageupper.com/cgi/uploads.pl
I am clicking on the first "Browse" button, which is document.getElementById('fileo1').click(), but I don't know how to enter data into the subsequent file explorer popup.
This is somewhat of a return to my 2012 question - Is it possible to fire a keypress event for an iframe that has been used to load a PDF file, I think I may have been mistaken accepting the answer there.
The current situation is this -
The document of the main page has an onkeypress event attached to it which will load a PDF into an object when the user scans a particular barcode.
The PDF is created using iTextSharp and has the open action set - writer.SetOpenAction(new PdfAction(PdfAction.PRINTDIALOG)) (writer is a PdfWriter).
So a user will scan the barcode which automatically loads a PDF file which opens it's print dialog. The user will then choose the printer and print the PDF. The problem comes when the user wants to scan the next unit they need to click somewhere within the html body (but outside of the PDF object) and then scan the barcode for the new PDF to be loaded.
I had a look at this and saw that it is possible to setup some kind of communication between the PDF plugin and the browser but is is possible to tell the plugin to surrender control to the browser/javascript
EDIT
Some extra background information -
I've tried using both objects and and iframes.
Using the debugger, if I type document.activeElement.id gives the ID of an element an appropriate element however (in the case of text inputs) any key presses do not appear.
Using the debugger I can use blur the active element bring focus to another element even change the value of an input but no user input is seen by the HTML/javascript when the user types/scans.
This is a legacy application runs in Internet Explorer compatibility mode.
Just to sum up - I want to scan a barcode which will open a PDF (already done) which opens it's print dialog automatically (already done) the user prints the PDF and then without any further human interaction with the page (i.e. the user does not need to use the mouse or keyboard after clicking the print button in the print dialog) the user should be able to scan another barcode which will open a new PDF.
Rather than render the PDF inside an iframe/object, why not take advantage of modern HTML5 and Mozilla's hard work and render it using https://github.com/mozilla/pdf.js/ - it is a fully HTML5/javascript PDF renderer and does not require any plugins.
There is a nice online demo at http://mozilla.github.io/pdf.js/web/viewer.html
Then you would be on the same page with JavaScript all the way down and could avoid having to pass focus/events etc between the two environments, so you could catch events anywhere on the page easily enough.
I have site which contains many links. User can open links in new window. User can log out from one window. If they do so and click on the other window of my site then I want to give an alert to the user saying that "Please login to continue". My questions are:-
Is this possible in Drupal 7?
Is there any extra module to support this.
If there is no contributed module available for this then my idea is to register a click event on the page(document) inside JQuery which will always check if user is authenticated or not. I want to know that which function in Drupal will help me in understanding authenticated user of site in JQuery.
Please share if any other option available to achieve this goal.
This is not an easy task, but I am going to explain how I would do so (without too many details):
Create a web service which receives a cookie and returns whether it represents a live session or not.
Use the visibility change javascript event to detect when a user leaves the browser or comes back in. When you notice the user has come back to the browser, perform an AJAX call to the server you set up in step one. You should provide the cookie stored in the browser, and expect a boolean answer.
Depending on the response given by the server, display a modal dialog with Javascript showing a login form orwhatever you consider necessary.
Note: This method will only work if the user is using a browser supporting visibility change events, and can be bypassed disabling Javascript code execution. In addition, the user will still be able to see the content if the browser is not the focused window (for example, there are two opened windows, each of them covering one half of the screen. The event will not fire until the focus comes back to the "forbidden" window).
Ok so this is a general question about browser interaction and HTTP mechanims
Here is the scenario
There is a very rich interface with a lot of buttons.
The user clicks the button "generate the report"
Then there is a loading time of approximately 15 seconds and then the "save file" dialogue box appears
The user saves the files wherever he wishes and do further stuff.
Now I want to:
Prevent him from interacting with the interface during step 3.
At the end of step 3, trigger some specific javascript like showing a popup.
All this actually sums up to "how to detect the appearance of the download dialog box in javascript" because once this one is solved, this is easy to set up some mask during the file generation, and to do whatever is required once the event has been detected.
Unfortunately it seems like there is no way to launch a file download via Ajax because it lacks adequate handling for turning an XMLHttpRequest into a save as dialog box. As a consequence you need to submit some form, but the mechanism is a little weird because although you submitted the form absolutely nothing happens in the DOM. Then the browser detects that response to the form submission is an incoming file and shows a download box instead of trying to display the file content, and once the user is done with the download box, the browser somehow manage to restore the interface in its previous state so that the user can keep on browsing (or at least this is how I understand the thing).
Display a loading sign for x seconds, disable click events (see here) or replace them with a dialog saying they should stay quiet while it loads. Do not forget to make the loading sign moving/showing progress or they will get bored. I'm sure there are plenty of libraries to achieve this, you could even try using the <'progress'> tag. As #j3r3m7 said, if they want to close the browser they will; the goal here is to make user understand he has to wait for x seconds and make him eager to wait.
As for your other issue, please look here
EDIT : A helpful plugin to address these kinds of issues can be found here.
Interesting question as in essence you can't stop the user from doing anything really... they could if so inclinded:
close their browser
enter a url directly to go to a new page
click back in the browser
open a new browser window and navigate to the report generation page for a second time
refresh the page
So, if you have a long running process quite a good way to deal with it is:
indicate that the process may take a while
at the point that the report is ready provide a unique link to the generated report
if you are using a javascript framework like AngularJS you could asynchronously handle an event that shows a report download/view link when the report is ready.