Home  >  Article  >  Web Front-end  >  cnblogs Code Highlighting Code Copying Problem Solving Implementation Code_Javascript Skills

cnblogs Code Highlighting Code Copying Problem Solving Implementation Code_Javascript Skills

WBOY
WBOYOriginal
2016-05-16 17:58:351134browse

Unexpectedly, the problem of code copying was realized recently (December 2012). Otherwise, all the content is one line, and only those with tools can quickly see the code. In this way, the code is much simpler to use and can be copied directly. There is no need to convert every time.
The technology of this article is mainly about technology. Let’s take a look at what methods they used. Friends in need can refer to it. To facilitate backup, package a copy of the code first and study it yourself if necessary.

Copy code The code is 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(/(){3}/gi, '

'));
if ($(this).find("div.cnblogs_code_hide").length == 0) {
if (parseInt($( this).css("height"), 10) > 30) {
showCopyCode($(this));
var regex = //gi;
if (regex.test($(this).text())) {
showRunCode($(this));
}
}
}
});
}
});

function showCopyCode(element) {
$(element).append('
< span class="cnblogs_code_copy">Copy code');
}

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();
$("
Press Ctrl C to copy the code
").insertAfter($(textarea));
}

function getCnblogsCodeContainer(element) {
var codeContainer = $(element).parent().parent().parent().find("pre");
if (codeContainer.length = = 0) {
codeContainer = $(element).parent().parent().parent().find("div").first();
}
return codeContainer;
}

function getCnblogsCodeText(codeContainer) {
var cbCode = 'n' $(codeContainer).html()
.replace(/ /g, ' ')
.replace(/ /ig, 'n')
.replace(/<[^>]*>/g, '');
cbCode = cbCode.replace(/n (s*d )/ig, 'n');
cbCode = cbCode.replace(/n/g, 'rn');
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('Run code');
}
}

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
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn