I am using Tampermonkey and the script segment below to fill in some values into a web form, a timesheet actually. When the form loads the script runs and enters the values - this is working fine. The problem is when I click the form's Submit button nothing is submitted and the form is cleared. Entering the same text by hand works fine.
What am I doing wrong? Or what other approach can I try?
// ==UserScript==
// #name Fill the timesheet
// #namespace
// #version 0.1
// #description
// #author
// #match https://timesheet.employer.com/employees/timesheet/*
// #icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// #grant none
// #run-at document-end
// ==/UserScript==
var days = document.getElementsByClassName('timesheet_days');
for (var i = 0; i < days.length; i++) {
var day = days[i];
console.log(day);
var box = day.getElementsByClassName("Timesheet__input")[0];
box.value = "8h 00m";
}
So I found the clue here. The issue is if Tampermonkey changes the .value field for a text box then the isTrusted field for the DOM element is set false and the timesheet server rejects the text. I say that like I know what I am talking about, which for sure I don't, but that is what I understand from the link above. One way around this is to paste the value as though it came from the clipboard.
This code is working fine -
var box = day.getElementsByClassName("Timesheet__input")[0];
box.focus();
box.value = ""; // clear any prefilled value
const el = document.querySelector('input');
el.focus();
document.execCommand('insertText', false, '8h 00m');
el.dispatchEvent(new Event('change', {bubbles: true})); // usually not needed
Related
I want to make a script that fills the registration form for Doulos webinars. e.g. https://register.gotowebinar.com/register/2965111702634965004
When I go to this link via Firefox it loads https://register.gotowebinar.com/pageNotFound
Only when I click back then the proper form comes in. It looks like tampermonkey is trying to run the script before it gets to the right form, it doesn't find the right fields there and in this situation once it loads the fields indicated in the script are not filled in.
Here an example if I wanted to fill the first name and email:
// ==UserScript==
// #name Duolos autofill registration form
// #namespace http://tampermonkey.net/
// #version 0.1
// #description try to take over the world!
// #author blaz
// #match https://register.gotowebinar.com/register/*
// #icon https://www.google.com/s2/favicons?sz=64&domain=gotowebinar.com
// #grant none
// #run-at document-end
// ==/UserScript==
var formFirstName = document.getElementById('registrant.firstName')
var formEmail = document.getElementById('registrant.email')
formFirstName.value = 'My_first_name'
formEmail.value = 'my#email.com'
Important note: this is my first contact with JavaScript and Tampermonkey.
How to make such a script properly?
Filling works as I applied the suggested setTimeout() by #JeremyThille
I keep up with clicking on 'Undo' within 10 seconds and the form loads during that time.
setTimeout(function(){
console.log("10s later...");
var formFirstName = document.getElementById('registrant.firstName')
var formEmail = document.getElementById('registrant.email')
formFirstName.value = 'My_first_name'
formEmail.value = 'my#email.com'
}, 10000);
In our company, we are using the firefox greasemonkey addon to fill in login credentials on a webpage to open it and login automatically.
This is what the script looks like:
// ==UserScript==
// #name logon
// #namespace https://www.example.com
// #include https://www.example.com
// #version 1
// #grant none
// #require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// ==/UserScript==
$(document).ready(function() {
$('input[name=username]').val('*****');
$('input[name=passcode]').val('*****');
$('input[name=send]').click();
});
Since upgrading to Firefox version 70, the script does not enter the login credentials anymore.
I am under the impression that this is due to the page loading to slowly and the script executing to fast.
I would therefore like to insert a timeout in this script. So that it will only execute after 5 seconds after opening the page.
I have tried several examples found online, but none seems to work.
Help would be greatly appreciated!
Thanks
If your theory about why it does not work is correct, the code below should work. Observe some other changes such as:
JQuery is not needed for trivial things like this. Or any things. Just don't. It adds a dependency on your code for little benefit.
You should not store password in plaintext script, but if you do at least put it in variable so that it's obvious to non-programmers where to change it.
Using someinput.form.submit() also submits the form, and removes one input on which existence you have to rely on (the script still works if the submit button is renamed).
// ==UserScript==
// #name logon
// #namespace https://www.example.com
// #include https://www.example.com
// #version 1
// #grant none
// #rut-at document-start
// ==/UserScript==
const TIMEOUT = 2000;
const NAME = "***";
const PASSWORD = "***";
document.addEventListener("load", function() {
setTimeout(function() {
document.querySelector("input[name=username]").value = NAME;
document.querySelector("input[name=passcode]").value = PASSWORD;
document.querySelector("input[name=passcode]").form.submit();
}, TIMEOUT);
});
But your original script may also have broken to other things than what you think, such as:
Input fields renamed. Use dev tools to inspect them and see if there's any name, class name or ID.
Framework is now used that does not read the values from fields, but only updates it's internal values when you type in them
Look in dev console to errors. Next time when asking, also demonstrate what have you tried and how it failed, otherwise you will not learn as much as you could have.
document.querySelector('div.su-input-group:nth-child(2) > input:nth-child(2)').value = ('abc');
document.querySelector("#password").value = "abc";
document.querySelector("#dob").value = "abc";
document.querySelector(".button-orange").click();
So far this is the code that I was able to write and it does not work on https://kite3.zerodha.com/. The script fails after line number 3. BTW same code works for me on other sites.
The reason this code is not working on kite3 website is that controls that need to be filled are animated, and they are not available for selection until animation finishes.
Solution for this is usage of waitForKeyElements js library. When these elements become available values can be set, buttons clicked.
Only one criteria is used for wait just to prove the concept.
This code is working for login on that website, just replace username and password with correct values.
// ==UserScript==
// #name Login to Kite
// #namespace http://tampermonkey.net/
// #version 0.1
// #match *kite3.zerodha.com
// #require https://gist.github.com/raw/2625891/waitForKeyElements.js
// #require https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// ==/UserScript==
waitForKeyElements(":password", login);
function login() {
document.querySelector("[type=text]").value = 'yourUserId';
document.querySelector("[type=password]").value = 'yourPassword';
document.querySelector("button").click();
}
I'm trying to get Tampermonkey to complete an online form.
It works every 1 out of 4 times, all I want it to do is a simple check out process on a bigcartel store. Can anyone help?
It should work on any shop using their platform as they are all quite generic, i.e http://groundup.bigcartel.com
my code;
// ==UserScript==
// #name New Userscript
// #namespace http://tampermonkey.net/
// #version 0.1
// #description try to take over the world!
// #author You
// #include https://checkout.bigcartel.com/*
// #include https://*.bigcartel.com/product
// #include https://*.bigcartel.com/cart
// #grant none
// ==/UserScript==
// on "/cart" page click checkout button
document.getElementByName("checkout").click();
// fill first three form fields
document.getElementById("buyer_first_name").value = "John";
document.getElementById("buyer_last_name").value = "Smith";
document.getElementById("buyer_email").value = "john#doe.com";
// click "next" button
document.getElementByType("submit").click();
There are four major issues with your TM script.
1.) Your include tags use https instead of http
2.) document.getElementByName doesn't exist.
Fix: Use document.getElementsByName("checkout")[0]
3.) Once you click the checkout button, the script immediately try to set the values of the input fields, you must wait for the page to load.
4.) document.getElementByType doesn't exist either.
Here is the working script:
// ==UserScript==
// #name Script
// #version 0.1
// #description try to take over the world!
// #author You
// #include https://checkout.bigcartel.com/*
// #include http://*.bigcartel.com/product
// #include http://*.bigcartel.com/cart
// #grant none
// ==/UserScript==
// on "/cart" page click checkout button
if (window.location.origin !== "https://checkout.bigcartel.com") document.getElementsByName("checkout")[0].click();
else {
// fill first three form fields
document.getElementById("buyer_first_name").value = "John";
document.getElementById("buyer_last_name").value = "Smith";
document.getElementById("buyer_email").value = "john#doe.com";
// click "next" button
document.getElementsByTagName("button")[0].click();
}
I want to submit the form using javascript..
I have tried the codes below but they didn't work, the page just continue refreshing or did nothing.
document.forms[0].submit();
document.forms["create-discussion"].submit();
document.getElementByID("submitButton").click();
document.getElementByID("submitButton").submit();
Please here is the process to access to the webform
1st - www.yookos.com(login page) -- username(tolodo) password(loveworld)
2nd - https://www.yookos.com/discussion/create.jspa?containerID=5460&containerType=700
My Userscript Below fills in text but I still can't get it to submit
// ==UserScript==
// #name discussioncrap code
// #namespace http://hayageek.com
// #include http://www.yookos.*
// #require http://code.jquery.com/jquery-latest.js
// #include https://www.yookos.com/discussion/create.jspa?containerID=5460&containerType=700
// #version 1
// ==/UserScript==
var discussSubj = document.getElementById("subject");
discussSubj.value = "okay";
var discussBody = document.getElementById("wysiwygtext");
discussBody.value = "Just Now";
document.getElementByID("submitButton").click();
Be careful about the fact that javascript is case-sensitive.
In the last line of your code, you use an undefined function: it should be document.getElementById(), not document.getElementByID().