Last active
February 3, 2020 18:51
-
-
Save JamoCA/fee255d6cbdf444e04dfda6a944e56b1 to your computer and use it in GitHub Desktop.
RemoveLogging() is a ColdFusion/CFML UDF to strip javascript debugging console.log from HTML output.
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
<cfscript> | |
/* 2/3/2020 removeLogging - removes console.log debugging from HTML fragment. (Tested w/ACF2016) | |
Safely replace debugging w/trivial variable assignment. (Removing or replacing with a commenting may not work in every context.) | |
James Moberg - https://www.sunstarmedia.com/ | |
TryCF: https://trycf.com/gist/fee255d6cbdf444e04dfda6a944e56b1 | |
Blog: https://dev.to/gamesover/sanitizing-console-log-from-html-using-coldfusion-regex-m3b | |
*/ | |
string function removeLogging(required string inputString, string replacementText="ignored"){ | |
if (len(trim(arguments.replacementText)) IS 0){ | |
arguments.replacementText = "ignored"; | |
} | |
return javacast("string", arguments.inputString).replaceAll("(?m)console\.(assert|clear|count|debug|dir|dirxml|error|exception|group|groupCollapsed|groupEnd|info|log|markTimeline|profile|profileEnd|table|time|timeEnd|timeStamp|trace|warn)\(([\s\S.]*?)\);", "var #arguments.replacementText#;"); | |
} | |
</cfscript> | |
<cfsavecontent variable="SampleHTML"> | |
<script> | |
var a = location; | |
function foo(){return new Date}; | |
function bar(){return location.href}; | |
console.log('TEST2', new Date); | |
console.log('hello: ' + foo(), bar()); | |
console.log('TEST VARS', a, new Date); | |
console.info("TEST INFO", "this is info"); | |
console.warn("TEST WARN", "this is a warning"); | |
console.error("TEST ERROR", "this is an error"); | |
console.table("TEST TABLE", a); | |
// console.clear(); | |
/* console.log($('#zipSearch').select2('data'), new Date); */ | |
console.log(Math.abs(-123), new Date); | |
console.log( "spaces" ) ; | |
console.log('clicked', b2, b2.is(':checked'), new Date); | |
console.log( | |
"Multi", | |
"line" | |
); | |
console.log('components.stringify()', JSON.stringify(a)); | |
console.log("abc;123"); | |
//console.log('hello', new Date); | |
</script> | |
</cfsavecontent> | |
<cfoutput> | |
<div style="display:table;"> | |
<div style="display:table-cell;"> | |
<h2>SampleHTML</h2> | |
<xmp>#SampleHTML#</xmp> | |
</div> | |
<div style="display:table-cell;"> | |
<h2>removeLogging(SampleHTML)</h2> | |
<xmp>#removeLogging(SampleHTML)#</xmp> | |
</div> | |
</div> | |
</cfoutput> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment