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



尝试创建一个 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到窗口位置的函数。什么都没用。

控制台上没有错误。

试:

  • 火狐 59 + 油猴 4.3
  • 铬 66 + 篡改猴 4.5

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

简单、直接的答案:
window.location不适合您的原因是:

  1. window.location调用在事件处理程序load窗口中进行。
  2. 该目标页面是DOMContentLoaded事件后几乎立即触发的load事件之一。
  3. 默认情况下,用户脚本在DOMContentLoaded触发,但在这种情况下,加载事件在脚本运行时已经触发。

真正的答案是:这个问题代码有很多问题:

  1. 它不是跟踪和说明过程的状态。 脚本在许多(各种)页面上触发,并且必须根据状态的不同而有所不同。
  2. 这些页面是广泛由 AJAX 驱动的。 代码没有正确说明这一点。
  3. 在这种情况下,Load 事件不是特别有用。
  4. 前面提到的脚本触发与负载、竞争条件。
  5. 在这样的脚本中硬编码登录凭据意味着您将被欺骗,这只是何时以及损坏多少的问题。

您需要跟踪和区分至少 3 种不同的状态,使用页面的 URL 和/或页面上的关键节点和/或脚本存储/传递的状态变量的组合。

以下用户脚本说明了该过程,尽管我无法在站点上对其进行测试,并且为简单起见,省略了推荐的身份验证框架。

// ==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. 如果和仅当 (IIF) 帖子登录页面始终
    https://tramites.saime.gob.ve/index.php?r=tramite/tramite/
  2. 和 IIF 该页面不用于您关心的任何其他内容......

那么以下用户脚本可能足以满足您的需求:

// ==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");

最新更新