How to copy the source of a page and make it work
This was helpful: http://www.design215.com/toolbox/whitespace.php
but i wanted to host in here in my computer locally, but it doesn't work when i view source code and copy the whole thing?
i'm a javascript noob, i just wanted to put in a page the box below DO NOT use this for Javascript. Use our Javascri开发者_C百科pt Compressor.
until the top of notes
Hope someone could help me do that :)
as for request, I helped you ripped it off. Should work locally without internet connection. And fyi I ripped everything except the compression statistics. Do you need it as well? I believed you must be using it for some simple text editing.
Save this piece of code into a file called compressor.html
or whatever .html
you like, and run it with browser.
Disclaimer: Nothing fancy, purely ripping off the features from what you stated. And no dependencies needed, only a browser will be needed to run this code.
Tested with Chrome only, as I'm on mbp now. ;)
<!DOCTYPE html>
<html>
<head>
<style type="text/css" media="screen">
textarea{width:600px;height:120px;}
</style>
<script type="text/javascript" charset="utf-8">
/**
* Script logic is ripped from
* http://www.design215.com/toolbox/whitespace.php
*
* Ripped by Lionel Chan
* on 16 January 2011
* for stackoverflow.com
* http://stackoverflow.com/questions/4698907/how-to-copy-the-source-of-a-page-and-make-it-work
*/
function trim(a){return a.replace(/^\s+|\s+$/g,"")}
function right(a,pos){var z=a.length; return a.substring(z-pos,z)}
function clearTxt() {
var src = document.getElementById('source');
src.value = '';
src.focus();
}
function compress() {
var src = document.getElementById('source');
var dest = document.getElementById('destination');
var txt = src.value;
txt = txt.replace(/\r\n/g, "\n");
txt = txt.replace(/\r/g, "\n");
txt = txt.replace(/\n/g, "\r\n");
txt = txt.replace(/\t/g, " ");
var q = [];
var p = txt.split(/\r\n/);
for(var k = 0 ; k < p.length ; k++) {
var tmp = p[k];
tmp = tmp.replace(/(<!-)(-)[\s\S]+(-)(->)/ig,"");
tmp = tmp.replace(/\s{2,}/g," ");
tmp = trim(tmp);
if (tmp) q.push(tmp);
}
var z = "";
if (document.getElementById('remove-line').checked) {
var x = 0;
for (var k = 0 ; k < q.length ; k++) {
//line breaks every 1024 chars
if (x + q[k].length > 1024) {
z += "\r\n";
x = 0;
}
x += q[k].length + 1;
z += q[k];
if (right(z, 1) != ">") z += " ";
}
}else{
z = q.join("\r\n");
}
z += "\r\n";
dest.value = z;
}
</script>
</head>
<body>
<div><textarea id="source"></textarea></div>
<div>
<input type="checkbox" id="remove-line" /><label for="remove-line">Remove line feeds</label><br />
<input type="button" onclick="clearTxt()" value="Clear Box" />
<input type="button" onclick="compress()" value="Compress" />
</div>
<div><textarea id="destination"></textarea></div>
</body>
</html>
You don't only need the HTML source, but any linked JavaScript files as well (and the files they link to, etc).
But I think it would be illegal to just copy a whole web page, and browsers (for some reason) have stricter security settings for javascript that is run locally. If you need to do this offline, you'd better find another tool to do that. Until then, just make a bookmark to this page.
精彩评论