Make my userscript wait for other scripts to load

岁酱吖の 提交于 2019-12-05 05:02:33
Wayne Burkett

This is not a matter of timing.

You're bumping into Greasemonkey's security restrictions, which prevent you from executing functions in the page. Please see my answer to this previous question for an explanation and some safe workarounds:

UserScripts & Greasemonkey: calling a website's JavaScript functions

You can always write a little function that checks to see if the function is loaded

function waitForFnc(){
  if(typeof absearch == "undefined"){
    window.setTimeout(waitForFnc,50);
  }
  else{
    runMyFunction();
  }
}

function runMyFunction(){
    var urlParams = window.location.search.substring(1).split('&'),
        username = "",
        hscEmailInput = document.getElementById('userfield6'),
        i = 0;
    if (urlParams !== "") {
        for (i = 0; i < urlParams.length; i++) {
            if (urlParams[i].substr(0,4) === "USER") {
                username = urlParams[i].replace('USER=', '');
                hscEmailInput.value = username + '@example.com';
                absearch('&PAGESIZE=1');
            }
        }
    }
}

waitForFnc();
no.good.at.coding

If the problem is indeed that your script runs too early, perhaps checking and waiting until the DOM is ready is the solution: Relying on DOM readiness to invoke a function (instead of window.onload)

Edit

This answer on SO might also prove to be useful.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!