Here is a straightforward explanation:
The reason why window.location
may not be working for you is:
- The call to
window.location
is placed within a window's load
event handler.
- The target page triggers the
load
event almost immediately after the DOMContentLoaded
event.
- Normally, userscripts execute at
DOMContentLoaded
, but in this case, the load event has already been triggered when your script runs.
The actual issue:
There are several problems with the code in question:
- It does not take into account and address the different states of the process. The script needs to behave differently based on the state it encounters on various pages.
- The page(s) heavily rely on AJAX operations, which the code fails to handle appropriately.
- In this scenario, the load event does not provide significant help.
- There exists a timing conflict between script execution and the load event.
- Including login credentials directly in the script poses a severe security risk and makes it susceptible to exploitation.
To resolve these issues, you need to differentiate between at least 3 distinct states by utilizing information such as the URLs of pages, key elements on the pages, or stored/passed state variables within the script.
The following userscript demonstrates the process, although testing may be restricted due to authentication requirements, as discussed in this related thread:
// ==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 ensure proper handling.
//--- Different actions needed for different pages
if (location.search.includes ("site/login") ) {
//-- Wait for page setup completion
waitForKeyElements ("#login_button:visible", loginWhenReady);
}
else if (location.search.includes ("tramite/tramite") ) {
//-- Simply redirect
location.assign ("https://tramites.saime.gob.ve/index/example/example");
}
else {
//-- No action required on other pages.
}
function loginWhenReady (jNode) {
//-- For demonstration purposes only - Usage of framework or password manager recommended!
$("#LoginForm_username").val ("user");
$("#LoginForm_password").val ("1234");
clickNode (jNode);
}
function clickNode (jNode) {
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
jNode[0].dispatchEvent (clickEvent);
}
Possible efficient resolution:
- If-and-only-if (IIF) the post login page always ends in
https://tramites.saime.gob.ve/index.php?r=tramite/tramite/
.
- AND IIF that specific page serves no other relevant purpose...
Then the following userscript might meet your requirements effectively:
// ==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");