Did not expect the recent (December 2012) Implementation of the Code replication problem, or all the content is a line, only people with tools can quickly see the code, so that the code to use up a lot simpler, you can directly copy Ah, do not have to transform what every time.
This article technology is mainly technical, see what they use the method, need friends can refer to. To facilitate backup, first pack a code, there is a need for their own research.
Copy Code code as follows:
#region Copy&run Code
$ (function () {
var hlcodes = $ ("#cnblogs_post_body div.cnblogs_code");
if (hlcodes.length) {
Loadencoderjs ();
$.each (Hlcodes, function () {
var htmlcontent = $ (this). html ();
$ (this). HTML (Htmlcontent.replace (/(<br\s*\/?>) {3}/gi, ' <br/><br/> '));
if ($ (this). Find ("Div.cnblogs_code_hide"). Length = = 0) {
if (parseint (this). CSS ("height") > 30) {
Showcopycode ($ (this));
var regex =/<script\s+type=[\ "\]text\/javascript[\" \]>/gi;
if (Regex.test (this). Text ()) {
Showruncode ($ (this));
}
}
}
});
}
});
function Showcopycode (Element) {
$ (Element). Append (' <div class= "Cnblogs_code_toolbar" ><span class= "cnblogs_code_copy" ><a href= " Javascript:void (0); "onclick=" Copycnblogscode (This) "> Copy Code </a></span>");
}
function Loadencoderjs () {
var encoderjs = document.createelement (' script ');
Encoderjs.type = ' Text/javascript ';
ENCODERJS.SRC = ' http://common.cnblogs.com/script/encoder.js ';
var node = document.getelementsbytagname (' script ') [0];
Node.parentNode.insertBefore (encoderjs, node);
}
function Copycnblogscode (Element) {
var codecontainer = Getcnblogscodecontainer (Element);
var cbcode = Getcnblogscodetext (Codecontainer);
var textarea = document.createelement (' textarea ');
$ (TEXTAREA). Val (Cbcode). Select ();
$ (TEXTAREA). CSS ("width", $ (Codecontainer). CSS ("width");
$ (TEXTAREA). CSS ("height", $ (Codecontainer). CSS ("height");
$ (TEXTAREA). CSS ("font-family", "Courier New");
$ (TEXTAREA). CSS ("Font-size", "12px");
$ (TEXTAREA). CSS ("Line-height", "1.5");
$ (Codecontainer). Parent (). HTML (textarea);
$ (TEXTAREA). Select ();
$ ("<div> press CTRL + C to copy code </div>"). InsertAfter ($ (textarea));
}
function Getcnblogscodecontainer (Element) {
var Codecontainer = $ (Element). Parent (). Parent (). Parent (). Find ("pre");
if (Codecontainer.length = = 0) {
Codecontainer = $ (Element). Parent (). Parent (). Parent (). Find ("div").
}
return codecontainer;
}
function Getcnblogscodetext (codecontainer) {
var cbcode = ' \ n ' + $ (codecontainer). HTML ()
. replace (//g, ' ")
. replace (/<br\s*\/?>/ig, ' \ n ')
. replace (/<[^>]*>/g, "");
Cbcode = Cbcode.replace (/\n (\s*\d+)/ig, ' \ n ');
Cbcode = Cbcode.replace (/\n/g, ' \ r \ n ');
if (typeof encoder!= undefined) {
Cbcode = Encoder.htmldecode (Cbcode);
}
Cbcode = $.trim (Cbcode);
return cbcode;
}
function Showruncode (Element) {
var codecopydiv = $ (Element). Find ("Div.cnblogs_code_toolbar");
if (codecopydiv.length) {
$ (Codecopydiv). Append (' <span class= "Cnblogs_code_runjs" ><a href= "javascript:void (0);" onclick= "RunJsCode (this) "> Run code </a></span>");
}
}
function Runjscode (Element) {
var codecontainer = Getcnblogscodecontainer (Element);
var cbcode = Getcnblogscodetext (Codecontainer);
var newwin = window.open ("," _blank ",");
Newwin.document.open (' text/html ', ' replace ');
Newwin.opener = null;
Newwin.document.write (Cbcode);
Newwin.document.close ();
}
#endregion
Package download