尝试创建一个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
函数到window.location。什么都行不通。
控制台上没有错误。
尝试:
登录页面/表单为https://tramites.saime.gob.ve/index.php?r=site/login
成功登录后,它会转到https://tramites.saime.gob.ve/index.php?r=tramite/tramite/
- 我想重定向。
答案 0 :(得分:0)
简单,即时的答案:
window.location
不适用于您的原因是:
window.location
调用是在窗口load
事件处理程序内完成的。load
事件在DOMContentLoaded
事件发生后几乎立即触发。DOMContentLoaded
处触发,但在这种情况下,脚本运行时已经触发了load事件。 真正的答案:
该问题代码存在许多问题:
您需要使用页面组合来跟踪和区分至少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);
}
https://tramites.saime.gob.ve/index.php?r=tramite/tramite/
。然后,以下用户说明可满足您的需求:
// ==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");