Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tampermonkey clicking a button by class

So i have a school issue where i need to access a site but this site requires me to go through around 4 portals to get there and am hoping to just write a quick script to do this for me. Problem comes where the site is very sloppy and are written with names being the same on certian buttons so I would like to click the buttons based on class

the classes are readit2, readit23, readit239, and readit2394

 $(function(){
        document.getElementByClassName(readit2).click();
       });

the above code i thought would click it as soon as it loads the first page but does not. any help would be great

// ==UserScript==
// @name       dumb spider web
// @namespace  ===============
// @version    0.1
// @description  gets me through this dumb stuff
// @match      ===============
// @copyright  2012+, You
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==

edit ^ added the header stuff

more edits:

it works through the console now but through tamper monkey OR grease monkey I can not get it to actually preform the action.

$(function(){
    document.getElementsByClassName("readit2")[0].click();
});

Works through console but does not run when script is started.

like image 991
user3699068 Avatar asked Jun 02 '14 11:06

user3699068


1 Answers

I started looking at all the wrong things, your original code:

$(function(){
    document.getElementByClassName(readit2).click();
});

The issue with this is that the call should be getElementsByClassName (plural "Elements"), since class is usually a shared property. That also returns an array, so if you are positive that there is only 1 ever:

$(function(){
    document.getElementsByClassName("readit2")[0].click();
});

If not, I'd suggest getting the text and verifying that it contains something that you expect.

Edit: Added quotes.

like image 198
nerdwaller Avatar answered Oct 17 '22 20:10

nerdwaller