3

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 try to create a Javascipt to change how I interact with a webpage. Upon page load, I'd like to automatically open a link in a new tab in the background. This link is slightly different each time I load the page. The element from the webpage is this:

<a href="/cgi/admin/user/ssh_login/*" target="_blank">SSH</a>

The part with the * is what's different each time.

So how can I automatically click that link upon page load if it doesn't have an elementID or at the very least an elementName?

2 Answers2

8
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);
}
Community
  • 1
  • 1
bcr
  • 3,791
  • 18
  • 28
  • 1
    @Mr_Green `.click()` fires the click event, not `onclick` which defines it. – Spencer Wieczorek Oct 05 '15 at 15:59
  • 3
    `onclick` is an inline event listener. `click()` simulates a mouse click. – bcr Oct 05 '15 at 15:59
  • another question, why `[href*=` wouldn't `[href^=` works here as we know it starts from there only? – Mr_Green Oct 05 '15 at 16:01
  • @Mr_Green it sure would, `*= ` was just the first one that came to mind. – bcr Oct 05 '15 at 16:03
  • How would I incorporate this into a javascript to automatically click this upon page load? – Mister Pyrrhuloxia Oct 05 '15 at 16:03
  • 1
    @MisterPyrrhuloxia `window.onload = function(){ /*code*/ }` – Spencer Wieczorek Oct 05 '15 at 16:04
  • @MisterPyrrhuloxia Seriously, why do you want to click something automatically onload? You could forward the user, but this sounds like you are trying to do something that could negatively impact the user. Like I mentioned before, this looks dodgy. – somethinghere Oct 05 '15 at 16:04
  • @MisterPyrrhuloxia add it above the end of the body tag.. (In script tag) – Mr_Green Oct 05 '15 at 16:06
  • "I'm using Greasemonkey for Firefox and Tampermonkey in Chrome to try to create a Javascipt to change how I interact with a webpage. Upon page load, I'd like to automatically open a link in a new tab in the background." This is from my OP. This is not a webpage that I can edit. This is for a userscript in Greasemonkey to click this link for me when I load this webpage. – Mister Pyrrhuloxia Oct 05 '15 at 16:08
  • Then use `window.addEventListener('DOMContentLoaded', function(){ /* Your code here */ }, false)` instead of using the `window.onload` method. – somethinghere Oct 05 '15 at 16:13
  • @Brian when I do `window.addEventListener('DOMContentLoaded', function(){ var link = document.querySelector('[href*="/cgi/admin/user/ssh_login/"]'); link.click(); }, false)` nothing happens upon page load. But when I do `window.onload = function(){ var link = document.querySelector('[href*="/cgi/admin/user/ssh_login/"]'); link.click(); }` it works. But it does take me to the link immediately. I'd like for the link click to open in the new tab in the background. Is that possible? – Mister Pyrrhuloxia Oct 05 '15 at 16:58
  • @MisterPyrrhuloxia the first line of my answer, where `link` is set - this gets the link element. From there you want to set the `target` attribute, above `link.click()` add this line - `link.setAttribute('target', '_blank');` – bcr Oct 05 '15 at 17:26
  • @Brian so opening the link in a new tab is not a problem. Now my problem is that it's not opening that new tab in the background. Any idea? – Mister Pyrrhuloxia Oct 05 '15 at 17:33
  • @MisterPyrrhuloxia that's a browser specific issue, here is a fix for chrome - http://stackoverflow.com/questions/10812628/open-a-new-tab-in-the-background. You'll want to take my code and get the url by using `var url = link.getAttribute('href')` then pass the url into the function given in the accepted answer for the question above. I've edited my answer to include this. – bcr Oct 05 '15 at 17:36
  • @Brian like this? `var url = link.getAttribute('href') function clicking(){ var link = document.querySelector('[href*="/cgi/admin/user/ssh_login/"]'); link.setAttribute('target', '_blank'); link.click(); } function openNewBackgroundTab(){ 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); }` – Mister Pyrrhuloxia Oct 05 '15 at 17:46
  • @Brian thanks so much for your help. That script above that you edited into your original answer works in every way other than actually opening the link in a background tab. I'm using that script and it's just opening the link as a new tab and focusing on it right away. I'm not sure what I'm doing wrong. :( – Mister Pyrrhuloxia Oct 05 '15 at 20:08
0

If the content of the anchor is always going to be 'SSH' you can use;

$("a:contains('SSH')")

atoms
  • 2,993
  • 2
  • 22
  • 43