Automatically trigger an HTML hyperlink through JavaScript injected using a Safari App Extension

Currently, I am developing a Safari App Extension with the aim of automating the login process for a Citrix web portal. In my injected JavaScript code, there are two main tasks that need to be accomplished. Firstly, I need to submit an HTML form containing username and password fields, which is working correctly and redirects me to another page with an HTML anchor that requires clicking. However, I am encountering difficulties in programmatically triggering the click event on this HTML anchor link.

// The following code snippet is from the 'script.js' file injected by my Safari App Extension

document.addEventListener("DOMContentLoaded", function(event) {


    if (document.URL.includes("IdentityFederationPortal")) {
        document.getElementById("UserId").value = "..." // my login
        document.getElementById("Password").value = "..." // my password
        document.getElementById("submit").click() // <- works correctly
    }

    // The `.click()` method mentioned above functions properly as it is applied to a form button.


    if (document.URL.includes("TPDCWeb")) {

        var loginLink = document.querySelector("#plugin-assistance-download > div > div > div > div.footer > a.pluginassistant-skiplink.web-screen-link._ctxstxt_SkipToLogon._ctxsattr_title_SkipToLogonTip");

        console.log(loginLink) // console confirms that loginLink contains the correct HTML anchor element

        loginLink.click() // unfortunately, this does not work as expected!
    }
});

Additional Notes:

  1. The specific HTML for the anchor link that needs to be programmatically clicked is:

    <a class="pluginassistant-skiplink web-screen-link _ctxstxt_SkipToLogon _ctxsattr_title_SkipToLogonTip" href="#" title="Click here to skip to log on">Log on</a>
    . I have limited knowledge about web programming and am unsure about the functionality of this link when clicked by a regular user, especially considering that it appears to lead nowhere and has a NULL onClick property upon inspection.

  2. I have come across suggestions indicating that jQuery could be helpful in resolving this issue, but I am uncertain how to inject jQuery into a Safari App Extension script. My attempts thus far have resulted in the error message 'Can't find variable $' when attempting to utilize jQuery.

Answer №1

Thanks to the guidance of user1538301, I have managed to resolve the issue by introducing a 4-second delay before invoking the .click() method. It seems that my previous attempts to 'click' the link were premature, considering that the page's JavaScript code had not completed its operations or set an onclick handler yet. Although there may be a more sophisticated solution available, the simple time delay approach has successfully addressed the problem for now.

if (document.URL.includes("TPDCWeb")) {

    function getAndClickLink() {
        var loginLink = document.querySelector("#plugin-assistance-download > div > div > div > div.footer > a.pluginassistant-skiplink.web-screen-link._ctxstxt_SkipToLogon._ctxsattr_title_SkipToLogonTip");

        loginLink.click()
    }

    setTimeout(getAndClickLink, 4000);


}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to retrieve the DOM element using jQuery

I'm currently facing an issue with fetching a DOM element from a jQuery selector array. var index = $(this).index(); $titleElement = $(".title:not(.small)")[index]; Unfortunately, the code above only gives me the text and not the actual DOM element ...

What is the best way to create custom shapes using an array of points in the xyz coordinates system with THREE.JS?

For instance: I have the following points [1,0,2],[2,0,2],[3,2,5] I am looking to create a geometric shape using these points by connecting them. I attempted using THREE.Shape, however it only allows me to construct the shape on the x and y axis. Is there ...

Dealing with universal Ajax error handling in AngularJS

Previously on my website when it was 100% jQuery, I employed this method: $.ajaxSetup({ global: true, error: function(xhr, status, err) { if (xhr.status == 401) { window.location = "./index.html"; } } }); to establi ...

Looking for answers about JavaScript and SharePoint?

I have a question regarding the interaction between JavaScript and SharePoint. In an ASP page, I created a JavaScript function to retrieve list items from SharePoint. Here is part of my code: $(document).ready(function () { ExecuteOrDelayUntilScriptLoade ...

Having trouble with jQuery's 'OR' not functioning properly when iterating through mandatory input elements

In the process of creating a fallback for required HTML5 attributes for different input types, I encountered an issue with checking the ':checked' status of radio buttons and using the 'OR' || operator within the loop: if (self.val() = ...

Discover the rotation direction while dragging - GreenSock Animation Platform

I am currently implementing the Spin feature of the Greensock library for a dial element. While using the getDirection() method, I noticed that it accurately determines if the dial is rotating clockwise or counterclockwise when passing the starting point. ...

Formatting Tool Tips for Highcharts Bar Graph Points

I successfully generated a bar graph using HighCharts.js. The code snippet below shows my setup: var myChart = Highcharts.chart(id, { chart: { type: 'column' }, title: { text: '' }, xAxis: { ...

In the realm of JavaScript, consider logging a message to the console if the array value happens to be

I'm facing an issue with the code below. My goal is to log 'empty array value' whenever there is an empty value, but it's not functioning as expected. Can anyone advise me on what the value of an empty array item is and how I can modify ...

Looking for a specific search term within the middle of strings in an array

Is there a way to improve the autocomplete feature of my input field so that it shows suggestions based on any part of the word typed in? I currently have it set up to only show suggestions if the input matches the start of a word in the array. How can I m ...

Improving a lengthy TypeScript function through refactoring

Currently, I have this function that I am refactoring with the goal of making it more concise. For instance, by using a generic function. setSelectedSearchOptions(optionLabel: string) { //this.filterSection.reset(); this.selectedOption = optionLa ...

Manipulate the visibility of elements using the combination of MySQL, PHP

I have some experience in PHP programming, but I am new to JQuery. I successfully created a dropdown menu using data from a MySql database, however, I now want to add a button or icon next to the dropdown menu which will allow me to add an extra dropdown o ...

Examining an Array of Objects Based on Various Attributes

Looking to find a way to check if a similar object already exists in an array of objects. Specifically, I want to know if there is an object with the same make and model properties (Ford Focus): { make: 'Ford', model: 'Focus', co ...

What is the process for advancing to the next or previous step using Angular.js?

I am currently utilizing ui-route for routing purposes, specifically for navigating through a series of sequential forms. After submitting one form, I would like to automatically move on to the next step without hard coding the step name in the $state.go( ...

Strategies for maintaining a sticky element while transitioning between containers

My challenge involves a sticky button located inside a container, but the sticky behavior only seems to work within another element that is full width (container-fluid). Is there any way to make the sticky behavior global? I have attempted using 'fix ...

How can I prevent a child div from activating the hover effect on its parent div?

I am currently exploring how to create a child element with position: absolute that is placed outside of its parent element without triggering the parent's :hover effect. Despite the parent having a hover effect, the child elements will still activat ...

Two scrollbars within Bootstrap-4 modal

$("div[id^='entry']").each(function(){ var currentModal = $(this); //click next currentModal.find('.btn-next').click(function(){ currentModal.modal('hide'); currentModal.closest("div[id^='entry'] ...

Error message: Unable to access $controller with AngularJS and Karma

As someone who is just starting with testing, I figured it was a good idea to begin testing this project. However, when I execute grunt karma:watch, I encounter an error related to the configuration files. My config file includes: module.exports = functi ...

Sort through the array using a separate array in Vuejs

I am currently working with two arrays: { "products": [ { "name": "Jivi", "Hint": "45-60 IE/kg alle 5 Tage\n60 IE 1x/Woche\n30-40 IE 2 x/Woche", "frequency": ["1", "2", "8"] }, { "name": "Adynovi", ...

Events related to key press timing in HTML 5 canvas

Currently, I am developing a game similar to Stick Hero for Android using HTML5. I am working on the code that will capture the time of key press (specifically the right arrow key with ASCII 39) in JavaScript and expand a stick accordingly. <!doctype h ...

Server response not being awaited by Ajax call

Currently running WAMP v.2.5 on a Windows10 system for my PHP project connected to a MySQL DB that involves multiple AJAX calls, all functioning correctly. However, there is one specific call that keeps throwing an 'Unexpected end of input' error ...