0

I am trying to create a bookmarklet that performs simple functions across different webpages. The idea is that it would be in an easy to find location (on the bookmark bar) and perform the "standard function" on a given page. Instead of looking for a hyperlink or a submit button, I can rely on the bookmarklet to do its duty.

As someone that does not code in javascript, I am having trouble getting the bookmarklet to work on more than one item/page. I started by using the getElementById function successfully

javascript:(function () {
var i = document.getElementById("contactSeller").click()
})()

On another page, there is a button with the ID "sndBtn". Adding another click() function to the code with the second ID, however, doesn't work.

javascript:(function () {
var i = document.getElementById("contactSeller").click();
var m = document.getElementById("sndBtn").click()
})()

As far as I can tell it is because getElementById can only work once on a page. (Removing the first function makes the second one work). Other users have suggested recursive functions with CSS or jQuery but I had little luck getting them to work as bookmarklets.

Can someone help construct a simple function that will run through a list of clicks? It would be nice to have the ability to follow hyperlinks, submit forms, and click buttons i.e. so it is not only a recursive script of the same function. Each of the elements on each page seems to have unique ID's.

Links GetElementByID - Multiple IDs

https://gist.github.com/aseemk/5000668

https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll

http://www.dynamicdrive.com/forums/showthread.php?30130-multiple-ID(elements)-inside-document-getElementById(-)

2 Answers2

0

There's no limit to the number of times you can call getElementById. When the element doesn't exist you were probably encountering an exception that caused your script to terminate. You can't call .click() on a null object.

This will check for the existence of the element before clicking:

javascript:(function () {
var i = document.getElementById("contactSeller"); 
if(i){i.click();}
var m = document.getElementById("sndBtn")
if(m){m.click();}
})()
Rich Moss
  • 2,195
  • 1
  • 13
  • 18
  • 1
    This explanation is correct. Here's a shorthand way if you like it in one line (can be repeated with more ||, the first one will get clicked, if none exist, it will still terminate the script) : `(document.getElementById("contactSeller") || document.getElementById("sndBtn")).click();` – efreed Apr 23 '19 at 21:04
0

Did an example and its working good, have a look.

I think what @Rich Moss wrote is right, that document.getElementById("contactSeller") is undefined or that the click function get an exaption, that prevent the code from continue.

function c(v){
console.log(v)
}

document.getElementById("btn1").click()
document.getElementById("btn2").click()
<input type="button" onclick="c(1)" value="btn1" id="btn1" />

<input type="button" onclick="c(2)" value="btn2" id="btn2" />
Alen.Toma
  • 4,684
  • 2
  • 14
  • 31