/    Sign up×
Community /Pin to ProfileBookmark

Remove SCRIPTS from HTML

I”m attemmpting to build a javascript which generates the HTML for the current page, but with all of the scripts removed. I beleive I’m on the right track, but I’m guessing there might be something wrong with my regular expression. Running the code below simply gives me the unmodified HTML for the current site. Suggestions??

<html>
<head>
<script type=”text/javascript”>
function getFirst500()
{
alert( (document.getElementsByTagName(“HTML”)[0].innerHTML).replace(/<script.*script>/ig, “”) );
}
</script>
</head>
<body>
<form>
<input type=”button” value=”Remove Scripts” onclick=”getFirst500()” >
</form>
</body>
</html>

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@rnd_meJul 17.2008 — static, support code (dont chnage)
[CODE]if (!Array.prototype.map) {// from http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:map
Array.prototype.map = function (fun) {var len = this.length;if (typeof fun != "function") {throw new TypeError;}var res = new Array(len);var thisp = arguments[1];for (var i = 0; i < len; i++) {if (i in this) {res[i] = fun.call(thisp, this[i], i, this);}}return res;};}
if (!Array.prototype.filter) { //from http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:filter
Array.prototype.filter = function (fun) {var len = this.length;if (typeof fun != "function") {throw new TypeError;}var res = new Array;var thisp = arguments[1];for (var i = 0; i < len; i++) {if (i in this) {var val = this[i];if (fun.call(thisp, val, i, this)) {res.push(val);}}}return res;};}
function obValsl(ob) {try {if (ob && ob.length) {var r = [], i = 0, mx = ob.length;for (var z = 0; z < mx; z++) {r[z] = ob[z] || undefined;}}} catch (yy) {alert(yy);}return r;};
function tags(elm, tid) {if (tid) {if (elm.isString) {elm = el(elm);}return obValsl(elm.getElementsByTagName(tid));}return obValsl(document.getElementsByTagName(elm));}
function kill(tr) {tr.parentNode.removeChild(tr);}
[/CODE]




the actual html cleaning code (quite simple now):

[CODE]function getFirst500()
{
tags("script").map(kill);
alert( document.getElementsByTagName("HTML")[0].innerHTML);
}[/CODE]
Copy linkTweet thisAlerts:
@Declan1991Jul 17.2008 — It's not pretty, but it's functional.function getFirst500() {
alert(document.getElementsByTagName("html")[0].innerHTML.replace(/n/g,"NEWLINE").replace(/&lt;script.*/script&gt;/gi,"").replace(/NEWLINE/g,"n"));
}

The problem was that the script was broken up over multiple lines. I also tried the multiline flag (/<script.*script>/ig[b]m[/b]) but it didn't work.
Copy linkTweet thisAlerts:
@SkyRenderauthorJul 17.2008 — Thanks a bunch! This is exactly the information I needed. ?
Copy linkTweet thisAlerts:
@sadanyagciJul 18.2008 — Wait, so this makes a javascript page source code turn into the html version of what is seen on the client's browser?

--Never mind, I get it. It removes the functionality of the scripts as well.
Copy linkTweet thisAlerts:
@Ay__351_eJul 18.2008 — There was an error. So I deleted.
×

Success!

Help @SkyRender spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...