简体   繁体   English

在通过 Safari App Extension 注入的 JavaScript 中以编程方式单击 HTML 链接

[英]Click HTML link programmatically in JavaScript injected via Safari App Extension

I'm writing a Safari App Extension to automate logging into a Citrix web portal.我正在编写一个 Safari 应用程序扩展来自动登录到 Citrix Web 门户。 My injected JavaScript needs to do two things.我注入的 JavaScript 需要做两件事。 First, there is an HTML form to submit (with name/password).首先,需要提交一个 HTML 表单(带有名称/密码)。 That works just fine and takes me to another page with an HTML anchor that needs to be clicked.这工作得很好,并带我到另一个页面,其中包含需要单击的 HTML 锚点。 The problem is that I am unable to programmatically click on the HTML anchor link.问题是我无法以编程方式单击 HTML 锚链接。

// This is the `script.js` 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 above works just fine, I assume because it is used on 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 shows loginLink is assigned the correct HTML anchor

        loginLink.click() // does not work!
    }
});

Notes:笔记:

  1. The actual HTML for the anchor link I need to programmatically click on 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 am not familiar enough with web programming to understand what is happening when a regular user clicks on that link, which by simple inspection doesn't seem to lead anywhere (and has a NULL onClick property).我需要以编程方式单击的锚链接的实际 HTML 是: <a class="pluginassistant-skiplink web-screen-link _ctxstxt_SkipToLogon _ctxsattr_title_SkipToLogonTip" href="#" title="Click here to skip to log on">Log on</a>我对 Web 编程不够熟悉,无法理解当普通用户单击该链接时会发生什么,通过简单的检查,该链接似乎没有指向任何地方(并且具有NULL onClick属性)。

  2. I've seen scattered references suggesting that jQuery might be useful here, but I also can't figure out how to inject jQuery into a Safari App Extension script.我看到零散的参考资料表明 jQuery 可能在这里有用,但我也无法弄清楚如何将 jQuery 注入到 Safari App Extension 脚本中。 My attempts so far lead to the Can't find variable $ error when I try to use jQuery.到目前为止,当我尝试使用 jQuery 时,我的尝试导致Can't find variable $错误。

With the help of user1538301 I now have this working, just by delaying the .click() method invocation by 4 seconds.user1538301的帮助下,我现在可以完成这项工作,只需将.click()方法调用延迟 4 秒即可。 As was pointed out, presumably I was trying to 'click' the link before the page's own JavaScript had finished its business and assigned an onclick handler or some such.正如所指出的,大概我试图在页面自己的 JavaScript 完成其业务并分配一个onclick处理程序或一些类似的之前“单击”链接。 I'm sure there's a more elegant way to deal with this, but the simple time delay gets the job done for me 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);


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM