0

I've been writing a content script to do some form filling. The website I am trying to do this on uses the jquery spinner widget for one of the values. Using the Chrome console and manually performing every command, all of this works fine. However, when I try to let the script run it I get this error:

Uncaught Error: cannot call methods on spinner prior to initialization; attempted to call method 'value'
    at Function.error (jquery.js:253)
    at HTMLInputElement.<anonymous> (jquery-ui.min.js:6)
    at Function.each (jquery.js:370)
    at jQuery.fn.init.each (jquery.js:137)
    at jQuery.fn.init.t.fn.<computed> [as spinner] (jquery-ui.min.js:6)
    at myScript.js:96

My first thought was that I was calling the method too soon, but having it time out for a while does not work.

HTML:

<div class="spinbox">
    <div class="title">...<div>
    <div class="inputs" style="height: 49px;">
        <input type="tel" id="input-ID" data-win-bind="disabled: AmountSpinnerEnabled Converters.not" aria-valuemin="0.5" aria-valuemax="4750" class="ui-spinner-input" autocomplete="off" role="spinbutton" aria-valuenow="100">
        <button class="icon-plus ui-spinner-button ui-spinner-up ui-button ui-widget ui-state-default ui-button-text-only" tabindex="-1" role="button">...</button>
        <button class="icon-minus ui-spinner-button ui-spinner-down ui-button ui-widget ui-state-default ui-button-text-only" tabindex="-1" role="button">...</button>
    </div>
</div>

Code:

if(window.location.href == "website.com") {
    $( window ).on('load', function(){
        var x = 100
        var y = 200

        document.getElementsByClassName("dropdown")[1].click();
        setTimeout(function () {
            console.log("set amount")
            $("#input-ID").spinner("value", x);
            setTimeout(function () {
                document.getElementById("checkbox").click()
            }, 2000);
        }, 2000);
    }
}

I've been working through this by selecting elements and clicking buttons/entering values in the Chrome console and then adding the command to my script. That has worked fine until I came to this point. I was under the impression that with $( window ).on("load") the spinner would already have been initialized. When does the spinner get initialized for me to be able to set it to a certain value?

Edit: I am able to run the $("#input-ID").spinner("value", x); command manually before the script can do it and it works fine. Is there some way my script can't reach the initiated spinner, but the normal chrome console can?

Jeroen
  • 279
  • 1
  • 6
  • Don't use `$( window ).on("load")`, try `$(document).ready(function()(...` or it's shorthand `$(function() {...` instead. – Moob Oct 28 '19 at 22:52
  • @Moob That sadly does not work, I still get the same error. – Jeroen Oct 29 '19 at 11:42
  • I think its an issue with jQuery.spinner. There's this (with no actual solution): https://stackoverflow.com/questions/29968126/error-cannot-call-methods-on-spinner-prior-to-initialization-attempted-to-call – Moob Oct 29 '19 at 18:05
  • I don't think you can call `$("#input-ID").spinner("value", x);` until you've initialised the spinner (ie: `$("#input-ID").spinner();`). See also:https://api.jqueryui.com/spinner/ – Moob Oct 29 '19 at 18:08

0 Answers0