Cnblogs code after highlighting the problem of code replication to achieve code _javascript skills

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.