登录和重定向用户脚本失败,没有错误,也没有重定向?

时间:2018-05-06 22:40:31

标签: javascript greasemonkey userscripts tampermonkey

尝试创建一个Greasemonkey脚本来自动填充某些表单,我希望脚本在提交表单后转到另一个URL。

// ==UserScript==
// @name     usersaime
// @description fills data form for user and pass 
// @include https://tramites.saime.gob.ve/*
// @version  1.1
// @grant    none
// ==/UserScript==
document.getElementById('LoginForm_username').value = "user";
document.getElementById('LoginForm_password').value = "1234";

setTimeout(function() {
    document.getElementById('login_button').click();
}, 2000);

window.addEventListener('load', function(event) {
    // all resources finished loading
    window.location = 'https://tramites.saime.gob.ve/index/example/example';
});

window.location根本不起作用。我还尝试了window.location.hrefwindow.location.replace以及一个setTimeout函数到window.location。什么都行不通。

控制台上没有错误。

尝试:

  • Firefox 59 + Greasemonkey 4.3
  • Chrome 66 + Tampermonkey 4.5

登录页面/表单为https://tramites.saime.gob.ve/index.php?r=site/login 成功登录后,它会转到https://tramites.saime.gob.ve/index.php?r=tramite/tramite/ - 我想重定向。

1 个答案:

答案 0 :(得分:0)

简单,即时的答案:
window.location不适用于您的原因是:

  1. window.location调用是在窗口load事件处理程序内完成的。
  2. 该目标网页就是其中之一,load事件在DOMContentLoaded事件发生后几乎立即触发。
  3. 默认情况下,用户脚本在DOMContentLoaded处触发,但在这种情况下,脚本运行时已经触发了load事件。
  4. 真正的答案:
    该问题代码存在许多问题:

    1. 它没有跟踪和核算流程的 。该脚本会触发许多(各种)页面,并且必须根据状态而有所不同。
    2. 页面广泛受AJAX驱动。代码没有恰当地说明这一点。
    3. 在这种情况下,加载事件并不是特别有用。
    4. 前面提到的,脚本射击与负载,竞争条件。
    5. 在这样的脚本中硬编码登录凭据就意味着你会被淹没,这只是一个关于损害的时间和程度的问题。
    6. 您需要使用页面组合来跟踪和区分至少3种不同的状态。页面上的URL和/或关键节点和/或脚本存储/传递的状态变量。

      以下用户说明了该过程,但我无法在网站上对其进行测试,为简单起见省略了the recommended authentication framework。 :

      // ==UserScript==
      // @name     _Login and then redirect an AJAX-driven page
      // @include  https://tramites.saime.gob.ve/*
      // @require  https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
      // @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
      // @grant    GM_addStyle
      // @grant    GM.getValue
      // ==/UserScript==
      //- The @grant directives are needed to restore the proper sandbox.
      
      //--- Different pages require different actions
      if (location.search.includes ("site/login") ) {  // initial login page
      
          //-- Wait for page to initialize
          waitForKeyElements ("#login_button:visible", loginWhenReady);
      }
      else if (location.search.includes ("tramite/tramite") ) {  // successful login page
          //-- Just redirect
          location.assign ("https://tramites.saime.gob.ve/index/example/example");
      }
      else {
          //-- All other pages, no action needed.
      }
      
      function loginWhenReady (jNode) {
          //-- Demo purposes only!  Use framework or password manager instead!
          $("#LoginForm_username").val ("user");
          $("#LoginForm_password").val ("1234");
      
          clickNode (jNode);  //  Login button passed in by WFKE
      }
      function clickNode (jNode) {
          var clickEvent  = document.createEvent ('MouseEvents');
          clickEvent.initEvent ('click', true, true);
          jNode[0].dispatchEvent (clickEvent);
      }
      

      可能的快速简便的解决方案:

      1. if-and-only-if(IIF)后登录页面始终为
        https://tramites.saime.gob.ve/index.php?r=tramite/tramite/
      2. 和IIF该页面不再用于您关心的其他内容......
      3. 然后,以下用户说明可满足您的需求:

        // ==UserScript==
        // @name     _Quick and dirty post login redirect
        // @include  https://tramites.saime.gob.ve/index.php?r=tramite/tramite*
        // @grant    none
        // @run-at   document-start
        // ==/UserScript==
        
        location.replace ("https://tramites.saime.gob.ve/index/example/example");
        
相关问题