1

I have written a script that is current working in GreaseMonkey on page load. However, when I transfer this script over to TamperMonkey (In Chrome), the Script refuses to run on Page Load.

See Script:

// ==UserScript==
// @name     Redirect_ADFS
// @author      Eskimonian
// @version     2018.08.20
// @description     Auto Redirect ADFS Sites to Internal Login Page
// @run-at      document-end
// ==/UserScript==

window.onload = function Redirect() {
  var currenturl = window.location.href //define current URL
  if(currenturl.indexOf("/adfs") > -1) {
    var newurl = "//" + new URL(document.referrer).hostname + '/admin?saml=off'; //set destination URL
    alert("[Eskimo.1] SAML Site Detected. Redirecting to Internal Login page.")
window.location = newurl
  }
} //redirect URL if the currently existing url contains "adfs" in its pathname

What the script does:

Example with script running in background:

Go to: https://www.piercecountywa.gov/

Type /admin: https://www.piercecountywa.gov/admin

Redirects to: https://sas.co.pierce.wa.us/adfs/ls/

My script will then force the page to render the referral URL and add /admin?saml=off to the referral string and reload the page

My Script goes to: https://www.piercecountywa.gov/admin?saml=off

Again, running this in Greasemonkey works, however - I cannot understand how to make it run on start with Tampermonkey.

Any help on understanding WHY this is not working would be greatly appreciated.

Thank you! I understand this might be hard to troubleshoot as you cannot test so here is a video of the script working in Greasemonkey.

Note the immediate redirect from the ADFS login to the internal login form.

1 Answers1

8

In your header, you're using @run-at document-start, which according to the documentation, will inject the script immediately.

Try using @run-at document-end, which injects the script when the DOMContentLoaded event is dispatched. If you don't have luck with that, try hooking into the load event by means of;

window.addEventListener("load", function(event) {
    console.log("All resources finished loading!");
});
lux
  • 8,315
  • 7
  • 36
  • 49
  • 1
    This resolved my issue and I very much appreciate the help! :) I would publically mark this up, but it will not visibly show as my reputation is low. Thank you, again! – The Eskimonian Aug 16 '18 at 20:27
  • 2
    @TheEskimonian If it resolved your issue, then please accept it as the accepted answer. – lux Sep 27 '18 at 21:13
  • not working for me (January 2023). i used it as bookmarlet that was moving to https://lichess.org/training first. – SL5net Jan 21 '23 at 20:19