0

I am trying to write a code to auto select a radio button:

<input type="radio" class="pricezone-radio-input" 
name="abcdef" value="123" checked="" title="A">

Since the name of the radio button changes every time when the website refresh, so I used the title instead

var $ = window.jQuery;
(function() {
'use strict';

// Your code here...
$("[A']").click();
})();

When I look at the console in chrome, after the code run, an error occur:

common.js?version=1552060637712:1175 Uncaught TypeError: Cannot read 
property 'replace' of undefined
at handleAjaxError (common.js?version=1552060637712:1175)
at Object.success (performanceDetail.js?version=1552060637712:345)
at u (userscript.html?id=8cad0fd6-8836-407e-b409-3f083400471d:5)
at Object.fireWith [as resolveWith] (userscript.html?id=8cad0fd6-8836-407e- 
b409-3f083400471d:5)
at k (userscript.html?id=8cad0fd6-8836-407e-b409-3f083400471d:5)
at XMLHttpRequest.eval (userscript.html?id=8cad0fd6-8836-407e-b409- 
3f083400471d:5)

Can anyone suggest how can I fix this?


Just find that it is the script of the website itself causing the problem

handleAjaxError = function(ajaxData){
    var metaTag = $(ajaxData).filter("meta[http-equiv=refresh]");
    if (typeof(metaTag) !== undefined) {
        var content = metaTag.attr("content");
        var url = content.replace("0; url=", "");
        window.location.href = url;
    } else {
        $('#loading-blk').hide('hidden');
        $('.ajax-call-error').removeClass('hidden');
    }
};

is there any way I can solve this?


Chrome: Version 72.0.3626.121; Tampermonkey: v4.8

The exact script I'm using:

// ==UserScript==
// @name         New test
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://ticket.urbtix.hk/internet/zh_TW/secure/event/37954/performanceDetail/367999
// @require http://code.jquery.com/jquery-3.3.1.min.js
// @grant        none
// ==/UserScript==

var $ = window.jQuery;


(function() {
    'use strict';

    // Your code here...
    $("[title='Price Zone B']").click();

})();
george
  • 21
  • 5
  • Instead of IIFE, use `$(function () {...});`, which waits the DOM to be loaded before trying to access it. Also a typo in the attribute selector. – Teemu Mar 12 '19 at 11:27
  • If the website is throwing the error even when your userscript is disabled/uninstalled, then don't worry about it; such errors only effect your Tampermonkey script in rare-ish conditions. If using your script somehow causes the error, then something you are not showing us is the problem. **Give a link to the target page and/or a [mcve].** – Brock Adams Mar 12 '19 at 18:49
  • @BrockAdams the error only appears with my script enabled. The link for the website is: https://ticket.urbtix.hk/internet/en_US/secure/event/37954/performanceDetail/367999 (choose non-member login) (you may have to go to the main page first before accessing to the above link) Link to home page: https://ticket.urbtix.hk/internet/ – george Mar 13 '19 at 01:28
  • George, that script has a jQuery conflict with the site. This question is a duplicate of https://stackoverflow.com/questions/12146445/jquery-in-greasemonkey-1-0-conflicts-with-websites-using-jquery. Either use a different `@grant` or delete the `@require` line. – Brock Adams Mar 13 '19 at 08:09
  • Also delete the `var $ = window.jQuery;` line. It is not needed in any case but can cause problems depending on the sandbox mode. – Brock Adams Mar 13 '19 at 08:12
  • @BrockAdams Thanks a lot, it's working. But on the tampermonkey scrip, it says '$' is not defined. – george Mar 13 '19 at 08:31
  • Does it say that in the console or in Tampermonkey's editor? – Brock Adams Mar 13 '19 at 09:02
  • @BrockAdams Tampermonkey's editor – george Mar 13 '19 at 09:13
  • Add a `global` directive, similar to [this script's line 38](https://github.com/BrockA/SE-misc/blob/master/Add_kbd_sup_sub_shortcuts.user.js#L38). – Brock Adams Mar 13 '19 at 09:19

0 Answers0