US Certificate Digital Auto Authentication

Automatically clicks on the "Certificado digital" authentication button on Universidad de Sevilla login page

  1. // ==UserScript==
  2. // @name US Certificate Digital Auto Authentication
  3. // @name:es Autenticacion con Certificado Digital Universidad de Sevilla
  4. // @namespace Violentmonkey Scripts
  5. // @match https://sso.us.es/*
  6. // @grant none
  7. // @version 1.5
  8. // @author Alf
  9. // @description Automatically clicks on the "Certificado digital" authentication button on Universidad de Sevilla login page
  10. // @description:es Automaticamente clica en "Certificado digital" en la pagina de login de la Universidad de Sevilla
  11. // @run-at document-end
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15. /* MIT License
  16. *
  17. * Copyright (c) 2025 Alf
  18. *
  19. * Permission is hereby granted, free of charge, to any person obtaining a copy
  20. * of this software and associated documentation files (the "Software"), to deal
  21. * in the Software without restriction, including without limitation the rights
  22. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  23. * copies of the Software, and to permit persons to whom the Software is
  24. * furnished to do so, subject to the following conditions:
  25. *
  26. * The above copyright notice and this permission notice shall be included in all
  27. * copies or substantial portions of the Software.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  32. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  33. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  34. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  35. * SOFTWARE.
  36. */
  37.  
  38. (function () {
  39. "use strict";
  40.  
  41. // Track the current page URL to detect navigation
  42. let currentUrl = window.location.href;
  43. // Remember if we've acted on this specific page already
  44. let actedOnThisPage = false;
  45.  
  46. // Function to check and handle the confirmation dialog
  47. function checkForConfirmationDialog() {
  48. // Skip if we've already acted on this page
  49. if (actedOnThisPage) {
  50. return false;
  51. }
  52.  
  53. // Look for the confirmation dialog button with "Aceptar" text
  54. const confirmButton = document.querySelector(
  55. ".ui-dialog-buttonset button span#submit_confirm"
  56. );
  57.  
  58. if (
  59. confirmButton &&
  60. (confirmButton.textContent === "Aceptar" ||
  61. confirmButton.textContent.includes("Aceptar"))
  62. ) {
  63. console.log('Confirmation dialog found, clicking "Aceptar"...');
  64.  
  65. // Click the confirmation button
  66. confirmButton.closest("button").click();
  67. actedOnThisPage = true;
  68.  
  69. // Clear any existing intervals
  70. if (window.checkIntervalId) {
  71. clearInterval(window.checkIntervalId);
  72. window.checkIntervalId = null;
  73. }
  74.  
  75. return true;
  76. }
  77.  
  78. return false;
  79. }
  80.  
  81. // Main function to handle authentication
  82. function handleAuthentication() {
  83. // Reset our "acted" flag when URL changes (navigation or reload)
  84. if (currentUrl !== window.location.href) {
  85. currentUrl = window.location.href;
  86. actedOnThisPage = false;
  87. console.log("New page detected, ready to authenticate if needed");
  88. }
  89.  
  90. // Skip if we've already acted on this page
  91. if (actedOnThisPage) {
  92. return;
  93. }
  94.  
  95. // First check if we're already on the confirmation dialog
  96. if (checkForConfirmationDialog()) {
  97. return;
  98. }
  99.  
  100. // If not, look for the certificate button
  101. const certButton = document.getElementById("x509_module");
  102.  
  103. if (certButton) {
  104. console.log("Certificate digital button found, clicking...");
  105. certButton.click();
  106. actedOnThisPage = true;
  107.  
  108. // Set up observer to detect when confirmation dialog appears
  109. const observer = new MutationObserver(function (mutations) {
  110. // Reset flag for confirmation dialog since it's a new element
  111. actedOnThisPage = false;
  112.  
  113. for (const mutation of mutations) {
  114. if (mutation.addedNodes.length) {
  115. if (checkForConfirmationDialog()) {
  116. observer.disconnect();
  117. break;
  118. }
  119. }
  120. }
  121. });
  122.  
  123. // Start observing the document for changes
  124. observer.observe(document.body, { childList: true, subtree: true });
  125.  
  126. // Also set a fallback timer to check for the dialog (just once)
  127. setTimeout(function () {
  128. // Reset flag for confirmation dialog that might appear late
  129. actedOnThisPage = false;
  130. checkForConfirmationDialog();
  131. }, 1000);
  132. }
  133. }
  134.  
  135. // Wait for page to fully load
  136. window.addEventListener("load", function () {
  137. // Short delay to ensure all elements are rendered
  138. setTimeout(handleAuthentication, 1000);
  139. });
  140.  
  141. // Watch for URL changes (via History API)
  142. const originalPushState = history.pushState;
  143. history.pushState = function () {
  144. originalPushState.apply(this, arguments);
  145. actedOnThisPage = false;
  146. currentUrl = window.location.href;
  147. };
  148.  
  149. const originalReplaceState = history.replaceState;
  150. history.replaceState = function () {
  151. originalReplaceState.apply(this, arguments);
  152. actedOnThisPage = false;
  153. currentUrl = window.location.href;
  154. };
  155.  
  156. // Watch for URL changes (via navigation)
  157. window.addEventListener("popstate", function () {
  158. actedOnThisPage = false;
  159. currentUrl = window.location.href;
  160. });
  161.  
  162. // Check once initially for dialog
  163. setTimeout(checkForConfirmationDialog, 500);
  164.  
  165. // Short interval check for confirmation dialog
  166. // This will only run for a few seconds and will be cleared once a dialog is found
  167. window.checkIntervalId = setInterval(function () {
  168. if (!actedOnThisPage) {
  169. checkForConfirmationDialog();
  170. } else {
  171. clearInterval(window.checkIntervalId);
  172. window.checkIntervalId = null;
  173. }
  174. }, 1000);
  175.  
  176. // Auto-clear interval after 5 seconds to prevent continuous checking
  177. setTimeout(function () {
  178. if (window.checkIntervalId) {
  179. clearInterval(window.checkIntervalId);
  180. window.checkIntervalId = null;
  181. }
  182. }, 5000);
  183. })();