Last active
December 7, 2017 19:34
-
-
Save LukeMurphey/6769265 to your computer and use it in GitHub Desktop.
This browser user script identifies parts of a page that references a resource that is not preceded with a custom root endpoint. This script will only find problems if a custom rest endpoint is used.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Bad root endpoint links | |
// @namespace splunk | |
// @version 0.5 | |
// @description finds bad links caused when the root endpoint is modified | |
// @match https://127.0.0.1:8000/* | |
// @match http://127.0.0.1:8000/* | |
// @copyright 2013+, lmurphey@splunk.com | |
// ==/UserScript== | |
function is_url_rooted( link, n ){ | |
var root_endpoint = $C.MRSPARKLE_ROOT_PATH; | |
if( root_endpoint === undefined){ | |
return false; | |
} | |
if(link.indexOf("http") === 0){ | |
// An external link | |
return false; | |
} | |
else if(link.indexOf("#") === 0){ | |
// A local anchor | |
return false; | |
} | |
else if(link.indexOf("//") === 0){ | |
// An external link with no protocol specified | |
return false; | |
} | |
else if(link.indexOf("javascript:") === 0){ | |
// A javascript call | |
return false; | |
} | |
else if(link.indexOf("/") !== 0){ | |
// A relative link | |
return false; | |
} | |
else if( link !== null && link.indexOf(root_endpoint) !== 0 && n !== null){ | |
console.error("Found link without root endpoint: " + link); | |
n.css("color", "red").css("border", "5px solid red"); | |
return true; | |
} | |
return false; | |
} | |
function is_href_bad( n ){ | |
var root_endpoint = $C.MRSPARKLE_ROOT_PATH; | |
var link = null; | |
if( root_endpoint === undefined){ | |
return false; | |
} | |
if( n.attr("href") !== undefined){ | |
link = n.attr("href"); | |
} | |
else if( n.attr("src") !== undefined){ | |
link = n.attr("src"); | |
} | |
return is_url_rooted(link, n); | |
} | |
function find_bad_links(){ | |
var problems = 0; | |
$( "[href]" ).each( function(index){ | |
if( is_href_bad($(this)) ){ | |
problems++; | |
} | |
}); | |
$( "[src]" ).each( function(index){ | |
if( is_href_bad($(this)) ){ | |
problems++; | |
} | |
}); | |
// Parse style blocks | |
$( "style" ).each( function(index){ | |
text = $(this).text(); | |
if( text ){ | |
var reg = /url[(](.*)[)]/g; | |
var matches = [], found; | |
found = reg.exec(text); | |
while (found) { | |
if( is_url_rooted(found[1]) ){ | |
problems++; | |
} | |
reg.lastIndex = found.index+1; | |
found = reg.exec(text); | |
} | |
} | |
}); | |
console.info("Check for bad links done"); | |
msg = "" + problems + " links were found that are incorrect (are unaware of the custom root endpoint)"; | |
if( problems > 0 && $('body').modal !== undefined){ | |
d = '<div id="root_endpoints_modal" class="modal hide fade">' + | |
'<div class="modal-header">' + | |
'<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>' + | |
'<h3>Bad Links Found</h3>' + | |
'</div>' + | |
'<div class="modal-body">' + | |
'<p>' + msg + '</p><p />See the console logs for a list' + | |
'</div>' + | |
'<div class="modal-footer">' + | |
'<a href="#" class="btn" data-dismiss="modal" aria-hidden="true">Close</a>' + | |
'</div>' + | |
'</div>'; | |
$('body').append(d); | |
$('#root_endpoints_modal').modal(); | |
} | |
else if( problems > 0 ){ | |
alert(msg); | |
} | |
} | |
setTimeout(find_bad_links, 10000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment