Get the hostname of the document.referrer and redirect

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Get the hostname of the document.referrer and redirect



Note: Using GreaseMonkey to redirect on the browser



I am aware that I cannot simply place document.refferer.hostname to gather this information. My goal is to redirect the current page I am on to the document.referrer page + desiredPathName



Code Example:


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

var url = window.location.href
window.onload = function Redirect()
if(url.indexOf("adfs") > -1)
var refURL=document.createElement('a');
refURL.href = document.referrer;
var refer = refURL.hostname
var newurl = refer + '/admin/?saml=off'

window.location = newurl;


//redirect URL if the currently existing url contains "adfs" in its pathname



Unfortunately, this is the active result of that script:



URL: www.piercecountywa.gov/admin



Auto Redirects to: www.piercecountywa.gov/Admin
/Saml/LogonRequest?logonType=admin to verify the request on the server



Realizing it is ADFS, it redirects AGAIN to their ADFS Login: https://sas.co.pierce.wa.us/adfs/ls



My Script then runs: "the URL has "adfs" in it so - lets go back a page, grab the hostname, and add "/admin?saml=off" to it and reload".



However, the actual result is: https://sas.co.pierce.wa.us/adfs/ls/www.piercecountywa.gov/admin/sas.co.pierce.wa.us/?saml=off



Instead of doing what it is supposed to do and replacing https://sas.co.pierce.wa.us/adfs/ls with www.piercecountywa.gov/admin?saml=off and reloading the page, it is just adding www.piercecountywa.gov/admin to the URL and then my script re-runs itself in a continuous loop.



I need my script to replace https://sas.co.pierce.wa.us/adfs/ls with www.piercecountywa.gov/admin?saml=off and reload the page.



Note: This needs to work for more than one domain, so I cannot make this code specific to www.piercecountywa.gov. It has to be specific to the "adfs" being present in the URL.



Updated working 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





Resolved. :) It was amending the URL due to me not adding "//" to the string. Updated code above.
– The Eskimonian
Aug 21 at 14:53









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard