0

I'm trying to click the "Visit" button here:

<a name="claim" href="#" onmousedown="$(this).attr('href', '/sdf/view.php?dsg=45368');"
class="btn-small larg-btn">Visit<i class="fas fa-eye" aria-hidden="true"></i></a>

There many other buttons (same but with different links) on the page, but I need to click only 1 (first link in a row) each time I visit/load the page.

Tried to combine those two solutions (1 and 2) but unfortunately I'm absolutely dumb in it:-)).

Also tried the simpliest thing which works in most cases

(function (){
document.querySelector('selector').click();

But obviously didn't help at all.

Thank you.

DummyOne
  • 33
  • 1
  • 1
  • 6

1 Answers1

1

The link in your example doesn't have the correct href therefore click() wont have the desired result.

Method 1:

  • Get the link e.g. querySelector()
  • Simulate mousedown on the link so page JS changes the URL
  • Click the link

Method 2:

  • Get the link e.g. querySelector()
  • Get the onmousedown attribute
  • Get the final URL from the above attribute using Regular Expression
  • Assign the URL to the link
  • Click the link or open using GM_openInTab/GM.openInTab

Method 3: (not recommanded)

  • Get the link e.g. querySelector()
  • Get the onmousedown attribute
  • Use eval() to run the onmousedown JS on the link
  • Click the link

Here is an example of the method 1 using CustomEvent()

// get the  link using the correct query
const a = document.querySelector('a.btn-small');
if (a) {
  // simulate mousedown
  const event = new CustomEvent('mousedown');
  a.dispatchEvent(event);
  console.log(a.href); // not needed, for debuggin only
  a.click();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a name="claim" href="#" onmousedown="$(this).attr('href', '/sdf/view.php?dsg=45368');"
class="btn-small larg-btn">Visit<i class="fas fa-eye" aria-hidden="true"></i></a>

Using GM.openInTab

const a = document.querySelector('a.btn-small');
if (a) {
  const event = new CustomEvent('mousedown');
  a.dispatchEvent(event);
  GM.openInTab(a.href)
}
erosman
  • 7,094
  • 7
  • 27
  • 46
  • Thank you, but it doesn't work. It definetely clicks on something, which leads me to the main page of the site (so basically the script clicks on the first avalable link I guess) but not on the link in my requests. – DummyOne Jan 19 '22 at 16:41
  • 1
    @DummyOne That was an example code. You need to adjust its 'selector' to get the link that you require. – erosman Jan 19 '22 at 17:41
  • Changed the selector to "a.btn-small". It looked like finally it does what I need, but immediately after 1st click I got another 2 clicks in a row which lead somewhere else. How to make the script click the button just once? – DummyOne Jan 19 '22 at 18:24
  • 1
    @DummyOne It is difficult to advise when the actual station is not known. You can try `GM.openInTab` instead of `click()`. (example added to the post) – erosman Jan 19 '22 at 18:36
  • NP, solved it actually, added all classes in to the selector and now it stays when it should be, then it goes crazy with timing (I guess it's because the other scripts I have installed), but it's not essential. Maybe some random delay before click would be helpful? – DummyOne Jan 19 '22 at 18:39