How to fix continuous reloading of a webpage by tamper monkey script? - javascript

I'm trying to build a script that will click 3 buttons in succession but after clicking the first button, the script goes into infinite reloading.
The site I'm trying to click buttons on: JDoodle
You need to sign-in to see the buttons.
As you can see in the screenshots:
Button 1
Button 2
// ==UserScript==
// #name New Userscript
// #namespace http://tampermonkey.net/
// #version 0.1
// #description try to take over the world!
// #author You
// #match *.jdoodle.com/*
// #grant none
// ==/UserScript==
'use strict';
$ = jQuery;
$(document).ready(function foo(){
var node = document.querySelectorAll('a');
node[8].click(); //clicking button 1
node = document.querySelector('[title="User\'s Saved Files"]');
node.click(); //clicking button 2
}
);
Tried approaches:
1.
unsafeWindow.r=0
if(unsafeWindow.r==0){
foo = function(){};
unsafeWindow.r=1;
}
Result : unsafeWindow.r not defined
2.
f = 1;
while(f){
if(document.querySelector('[title="User\'s Saved Files"]'))
f=0;
}
Result: Infinite reloading.
Simply executing without approaches 1 & 2, no button is being clicked on by the script.

Related

Tampermonkey script is probably executed at the wrong time

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);

trying to auto-login at https://kite3.zerodha.com/ using javascript GreaseMonkey

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();
}

Tampermonkey script only sporadically working?

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 Click Post Button On This WebPage

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().

How would I get a user script to load after php is executed?

I am writing a script that changes up the message system for a website I use a lot. It adds a button to the top of each message thread. When clicked, it automatically moves to the most recent message in the thread.
The issue I am having is that the messages are loaded by php and the script fires before the messages are loaded. The site also doesn't load all of the message threads at the same time. It will load 10 or so, and then as you scroll down it loads more. Is there a way to get the button to load for each of the message threads, or am I shooting for an unreachable goal?
// ==UserScript==
// #name Imgur - Message Viewer
// #namespace Basion
// #description It skips to the bottom of the most recent message
// #author Basion
// #include http://imgur.com/*
// #include https://imgur.com/*
// #include http://*.imgur.com/*
// #include https://*.imgur.com/*
// #run-at document-end
// ==/UserScript==
var messages = document.getElementsByClassName("thread-wrapper");
var newA = document.createElement('a');
for (var i = 0; i < messages.length; i++) {
newA.addEventListener("click", scrollToMessage(i), true);
newA.innerText = "Go to Newest Message";
messages[i].appendChild(newA);
}
function scrollToMessage(id) {
var newMessages = document.getElementsByClassName('thread-wrapper');
newMessages[id].scrollIntoView(false);
}
To handle elements added via AJAX, use jQuery and waitForKeyElements as shown in "Run Greasemonkey script on the same page, multiple times?".
Here's a handy jQuery reference.
Also, you can't send id's to event listeners that way.
Here's a complete working, post-jump script, adapted to the question's code and the Imgur target page(s):
// ==UserScript==
// #name Imgur - Message Viewer
// #namespace Basion
// #description It skips to the bottom of the most recent message
// #author Basion
// #include http://imgur.com/*
// #include https://imgur.com/*
// #include http://*.imgur.com/*
// #include https://*.imgur.com/*
// #run-at document-end
// #require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// #require https://gist.github.com/raw/2625891/waitForKeyElements.js
// #grant GM_addStyle
// ==/UserScript==
/*- The #grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/
waitForKeyElements ("div.thread-wrapper", addJumpButton);
/*-- Prepend a button to the top of each thread. Don't add in the header, to
avoid conflicts with the open/close toggle.
*/
function addJumpButton (jNode) {
jNode.prepend (
'Go to Newest Message in thread'
);
}
//-- Activate *all* the jump buttons with one jQuery function.
$("div.notifications").on (
"click", "div.thread-wrapper a.gmThreadJmpBtn", jumpToEndOfThread
);
function jumpToEndOfThread (zEvent) {
//-- The last (newest) message will be just above the "reply bar".
var thisThreadHdr = $(zEvent.target);
var targetDiv = thisThreadHdr.nextAll ("div.reply.striped");
targetDiv[0].scrollIntoView (false);
//-- Block the page handlers from also firing for this link.
zEvent.stopPropagation ();
zEvent.preventDefault ();
}

Categories

Resources