尝试创建一个 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.href
和window.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
不适合您的原因是:
window.location
调用在事件处理程序load
窗口中进行。- 该目标页面是
DOMContentLoaded
事件后几乎立即触发的load
事件之一。 - 默认情况下,用户脚本在
DOMContentLoaded
触发,但在这种情况下,加载事件在脚本运行时已经触发。
真正的答案是:这个问题代码有很多问题:
- 它不是跟踪和说明过程的状态。 脚本在许多(各种)页面上触发,并且必须根据状态的不同而有所不同。
- 这些页面是广泛由 AJAX 驱动的。 代码没有正确说明这一点。
- 在这种情况下,Load 事件不是特别有用。
- 前面提到的脚本触发与负载、竞争条件。
- 在这样的脚本中硬编码登录凭据意味着您将被欺骗,这只是何时以及损坏多少的问题。
您需要跟踪和区分至少 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);
}
可能的快速简便的解决方案:
- 如果和仅当 (IIF) 帖子登录页面始终
https://tramites.saime.gob.ve/index.php?r=tramite/tramite/
。 - 和 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");