Click a link with Javascript when there is no element ID

前端 未结 2 512
死守一世寂寞
死守一世寂寞 2021-01-14 03:38

Please forgive me if this has already been answered somewhere but I just can\'t find what I\'m looking for. I\'m using Greasemonkey for Firefox and Tampermonkey in Chrome to

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-14 03:49

    var link = document.querySelector('[href*="/cgi/admin/user/ssh_login/"]');
    link.click();
    

    Edit:

    Open link in a background tab in chrome (based on this answer)

    var link = document.querySelector('[href*="/cgi/admin/user/ssh_login/"]');
    var url = link.getAttribute('href');
    openNewBackgroundTab(url);
    
    function openNewBackgroundTab(url){
        var a = document.createElement("a");
        a.href = url;
        var evt = document.createEvent("MouseEvents");
        //the tenth parameter of initMouseEvent sets ctrl key
        evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0,
                                    true, false, false, false, 0, null);
        a.dispatchEvent(evt);
    }
    

提交回复
热议问题