Skip to content

Instantly share code, notes, and snippets.

@ccloli
Created July 17, 2014 08:05
Show Gist options
  • Save ccloli/12b6a5b2a7c817a206b0 to your computer and use it in GitHub Desktop.
Save ccloli/12b6a5b2a7c817a206b0 to your computer and use it in GitHub Desktop.
// From CC Code
if(document.getElementsByClassName('d_post_content')[0]){
var d=/#(CC_Cipher|youtube|utb|acfun|bilibili|tucao|catfun|soundcloud|xiami|moefm)#/,
t1=new Date().getTime(),
list=[],
xpath = document.evaluate(
'//cc//text()', //XPath表达式:选择所有文本元素(text node)
document,//document.body, //筛选环境
null, //指定命名空间,不用管它
XPathResult.ORDERED_NODE_ITERATOR_TYPE, //被匹配的元素按出现顺序排列
null
),
k,x;
while (k = xpath.iterateNext()) {
if(k.parentElement.hasAttribute('class')&&k.parentElement.className=='cc_video')continue;
if (!d.test(x = k.textContent)) continue; // !此处出现 XSS 漏洞
if (!d.test(x = k.textContent.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;"))) continue; // 此为修补方案
if(x.indexOf('#CC_Cipher#')>=0)x=x.replace(/#CC_Cipher#([a-zA-z0-9+\/=]{20})([a-zA-z0-9+\/=]*)([a-zA-z0-9+\/=]{20})/gi,'<span class="cc_cipher" ciphertext="$1$2$3">#CC_Cipher#$1...$3</span>');
if(x.indexOf('#youtube#')>=0||x.indexOf('#utb#')>=0)x=x.replace(/#youtube#([\w-]{11})|#utb#([\w-]{11})/gi,'<span class="cc_video" type="youtube" mid="$1$2" titie="点击展开">#youtube#$1$2</span>');
if(x.indexOf('#acfun#')>=0)x=x.replace(/#acfun#(ac\d+_\d+|ac\d+)/gi,'<span class="cc_video" type="acfun" mid="$1" titie="点击展开">#acfun#$1</span>');
if(x.indexOf('#bilibili#')>=0)x=x.replace(/#bilibili#(av)?(\d+_\d+|\d+)/gi,'<span class="cc_video" type="bilibili" mid="$1$2" titie="点击展开">#bilibili#$1$2</span>');
if(x.indexOf('#tucao#')>=0)x=x.replace(/#tucao#([\d_]+)/gi,'<span class="cc_video" type="tucao" mid="$1" titie="点击展开">#tucao#$1</span>');
if(x.indexOf('#catfun#')>=0)x=x.replace(/#catfun#(cat[\d_]+)/gi,'<span class="cc_video" type="catfun" mid="$1" titie="点击展开">#catfun#$1</span>');
if(x.indexOf('#soundcloud#')>=0)x=x.replace(/#soundcloud#(tracks(?:\/|%2F)\d+|[-\/\w]+)/gi,'<span class="cc_video" type="soundcloud" mid="$1" titie="点击展开">#soundcloud#$1</span>');
if(x.indexOf('#xiami#')>=0)x=x.replace(/#xiami#(\d+)/gi,'<span class="cc_video" type="xiami" mid="$1" titie="点击展开">#xiami#$1</span>');
if(x.indexOf('#moefm#html5?')>=0)x=x.replace(/#moefm#html5\?((?:song|music|radio)=[\d,]+)/gi,'<span class="cc_video" type="moefm_html5" mid="$1" titie="点击展开">#moefm#html5?$1</span>');
if(x.indexOf('#moefm#')>=0)x=x.replace(/#moefm#((?:song|music|radio)=[\d,]+)/gi,'<span class="cc_video" type="moefm" mid="$1" titie="点击展开">#moefm#$1</span>');
list.push([k,x]);
}
while (e = list.shift()){
var node = document.createElement('a');
e[0].parentElement.replaceChild(node,e[0]);
node.outerHTML = e[1];
}
var ciphertext=document.getElementsByClassName('p_postlist')[0].getElementsByClassName('cc_cipher'),
pvideo=document.getElementsByClassName('p_postlist')[0].getElementsByClassName('cc_video');
for(var j=0;j<ciphertext.length;j++)ciphertext[j].onclick=cc_decode;
for(var j=0;j<pvideo.length;j++)pvideo[j].onclick=cc_devideo;
console.log('%cCC Code%c 替换正文节点时间统计:'+(new Date().getTime()-t1)+'ms','color:#4a82f0;text-decoration:underline','color:#4a82f0');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment