CSDN,CNBLOG博客文章一键转载插件

CSDN博客文章转载插件 可以实现CSDN上的文章一键转载

  1. // ==UserScript==
  2. // @name CSDN,CNBLOG博客文章一键转载插件
  3. // @version 7.0.0.Beta
  4. // @description CSDN博客文章转载插件 可以实现CSDN上的文章一键转载
  5. // @author By Jackie http://csdn.admans.cn/
  6. // @match *://blog.csdn.net/*/article/details/*
  7. // @match *://mp.csdn.net/postedit*
  8. // @match *://mp.csdn.net/postedit?opt=1
  9. // @match *://mp.csdn.net/console/editor/html?opt=1
  10. // @match *://www.cnblogs.com/*/p/*.html
  11. // @match *://www.cnblogs.com/*/p/*
  12. // @match *://www.cnblogs.com/*/articles/*.html
  13. // @match *://www.cnblogs.com/*/archive/*/*/*/*.html
  14. // @match *://*.blog.csdn.net/article/details/*
  15. // @match *://*.cnblogs.com/*/p/*.html
  16. // @match *://www.cnblogs.com/*/p/*.html
  17. // @match *://i.cnblogs.com/EditArticles.aspx?opt=1
  18. // @match *://i.cnblogs.com/EditPosts.aspx?opt=1
  19. // @match *://i-beta.cnblogs.com/posts/edit?opt=1
  20. // @match *://mp.csdn.net/mp_blog/creation/editor?opt=1
  21. // @match *://i.cnblogs.com/posts/edit?opt=1
  22. // @match *://editor.csdn.net/md/?not_checkout=1
  23. // @match *://mp.csdn.net/mp_blog/creation/editor?*
  24. // @match *://wenku.csdn.net/column/*
  25. // @require https://unpkg.com/turndown/dist/turndown.js
  26. // @grant GM_addStyle
  27. // @namespace https://greatest.deepsurf.us/users/164689
  28. // @supportURL https://github.com/JackieZheng/CsdnCnblogReproduce/issues/
  29. // @icon https://www.google.cn/s2/favicons?domain=csdn.net
  30. // ==/UserScript==
  31. GM_addStyle("#ReproduceBtn{position: absolute;float: right;right: 0px;width: auto;background: #0f962191;z-index: 9989;color: white;text-align: center;margin: 5px;padding: 5px;border-radius: 5px;cursor: pointer;line-height: 100%;}");
  32.  
  33. GM_addStyle(".tag__box{width: 100% !important;}");
  34. GM_addStyle(".tag__box div.tag__options-list{width: 100% !important;max-height:300px !important;padding: 8px 8px 0 0;display: flex;flex-direction: row;align-content: flex-start;align-items: center;justify-content: space-between;flex-wrap: wrap;}");
  35.  
  36. GM_addStyle(".tag__box div .tag__options-list .tag__option-box:last-child {margin-right: auto;}");
  37. GM_addStyle(".tag__box div .tag__options-list .tag__option-box:hover {color:#67c23a;}");
  38. GM_addStyle(".tag__box div.tag__options-content{height: auto !important;}");
  39.  
  40. (function() {
  41. 'use strict';
  42. var cnblog = location.href.indexOf("cnblogs.com") > -1 ? true: false;
  43.  
  44.  
  45. const messageListener = (event) => {
  46. if (event.origin.indexOf("csdn.net") > -1) {
  47. // console.log(event.data);
  48. let intput_editor = document.getElementsByTagName("iframe")[0] == undefined ? null: document.getElementsByTagName("iframe")[0].contentDocument.body;
  49. let input_title = (document.getElementById('post-title') ||document.getElementById('txtTitle') || document.getElementById('Editor_Edit_txbTitle') || document.querySelector('input.cnb-input'))||document.getElementsByClassName("article-bar__title")[0];
  50.  
  51. if(intput_editor&&input_title){
  52.  
  53. const article=event.data
  54.  
  55. input_title.value = article.title;
  56. event.source.postMessage("Get", event.origin);
  57.  
  58. intput_editor.innerHTML=article.content;
  59. if (intput_editor.children.ReadBtn) intput_editor.children.ReadBtn.remove();
  60. if (intput_editor.children.ReproduceBtn) intput_editor.children.ReproduceBtn.remove();
  61. let mathspace=intput_editor.querySelectorAll("[width*='thickmathspace']");
  62. mathspace.forEach(function(ele){
  63. ele.outerHTML=" ";
  64. });
  65. let mathspan=intput_editor.querySelectorAll("[class*='MathJax']");
  66. mathspan.forEach(function(ele){
  67. let innerText= ele.innerText;
  68. ele.outerHTML="<span>"+innerText+"</span>";
  69. });
  70.  
  71. let origin_input= document.querySelector("input[placeholder=请填写原文链接]");
  72.  
  73. if (origin_input) origin_input.value = article.url;
  74. if(document.querySelector("[class^='el-checkbox__original'][value]"))document.querySelector("[class^='el-checkbox__original'][value]").click();
  75. if (document.querySelector("[class^='el-checkbox__input']"))document.querySelector("[class^='el-checkbox__input']").className= 'el-checkbox__input is-checked';
  76. if(document.querySelector("[class^='el-radio__original'][value='repost']"))document.querySelector("[class^='el-radio__original'][value='repost']").click();
  77. if(document.querySelector("[class^='el-radio__original'][value='0']"))document.querySelector("[class^='el-radio__original'][value='0']").click()
  78. if(document.querySelector("[class^='el-radio__original'][value='1']"))document.querySelectorAll("[class^='el-radio__original'][value='1']")[1].click()
  79.  
  80. if(document.querySelectorAll("[class*='copyright-box']"))
  81. {
  82. document.querySelectorAll("[class*='copyright-box']")[0].style.display="";
  83. document.querySelectorAll("[class*='copyright-box']")[1].style.display="";
  84. }
  85.  
  86. removeMsgListener();
  87. }
  88.  
  89.  
  90. }
  91.  
  92. };
  93.  
  94. const removeMsgListener=()=>{
  95. window.removeEventListener('message', messageListener);
  96. }
  97.  
  98. window.addEventListener('message', messageListener);
  99.  
  100. // 原始文章窗口
  101. if (location.href.indexOf("article/details") > -1 || location.href.indexOf("wenku.csdn.net") > -1 || location.href.indexOf("www.cnblogs.com") > -1) {
  102. var divBtn = document.createElement("div");
  103. divBtn.setAttribute("id", "ReproduceBtn");
  104. divBtn.innerHTML = '引用';
  105. if (cnblog) {
  106. divBtn.style.marginTop = "-40px";
  107. divBtn.style.position = "relative";
  108. }
  109. var article = document.getElementsByClassName('article_content')[0] || document.getElementsByClassName('postBody')[0] || document.getElementsByClassName('blogpost-body')[0]||document.getElementsByClassName('content-view')[0];
  110. article.insertBefore(divBtn, article.childNodes[0]);
  111. var posteditUrl = cnblog ? "https://i-beta.cnblogs.com/posts/edit?opt=1": "https://mp.csdn.net/mp_blog/creation/editor?opt=1";
  112. console.log(location.href)
  113. divBtn.onclick = function() {
  114. var writeWin=window.open(posteditUrl);
  115. var timer=setInterval(() => {
  116. //writeWin.postMessage(document.getElementsByTagName('html')[0].innerHTML, posteditUrl)
  117.  
  118. var aTitle = "[转]" + document.title.split('_')[0] + "(转载请删除括号里的内容)";
  119. var aUrl=window.location.href.split('?')[0]+' ';
  120. var authorName = "";
  121. if (document.getElementsByClassName('follow-nickName').length > 0) {
  122. authorName = document.getElementsByClassName('follow-nickName')[0].innerText;
  123. } else if (document.getElementById('profile_block')) {
  124. authorName = document.getElementById('profile_block').childNodes[1].innerText;
  125. } else if (document.getElementById('author_profile_detail')) {
  126. authorName = document.getElementById('author_profile_detail').childNodes[1].innerText;
  127. }
  128. else if(document.getElementsByClassName('author-name'))
  129. {
  130. authorName = document.getElementsByClassName('author-name')[0]?.innerText;
  131. }
  132. var blogContent = (document.getElementById('content_views') ||document.getElementById('cnblogs_post_body')|| document.getElementsByClassName('content-view')[0]).innerHTML + "<br>---------------------" + "<br>作者:" + authorName + "<br>来源:" + (cnblog == true ? "CNBLOGS": "CSDN") + "<br>原文:" + aUrl + "<br>版权声明:本文为作者原创文章,转载请附上博文链接!" + "<br>内容解析By:<a href=https://greatest.deepsurf.us/zh-CN/scripts/381053-csdn-cnblog%E5%8D%9A%E5%AE%A2%E6%96%87%E7%AB%A0%E4%B8%80%E9%94%AE%E8%BD%AC%E8%BD%BD%E6%8F%92%E4%BB%B6 target=_blank>CSDN,CNBLOG博客文章一键转载插件</a>";
  133. var aContent = blogContent.replace(/<ul class=\"pre-numbering\"[^>]*>(.*?)<\/ul>/g, '').replace(/<div class=\"hljs-ln-line hljs-ln-n\"[^>]*>(.*?)<\/div>/g, '').replace(/<div class=\"hljs-ln-numbers\"[^>]*>(.*?)<\/div>/g, '').replace(/<div class=\"cnblogs_code_toolbar\"[\s\S].*?<\/div>/g, '').replace(/<a[\s\S].*class=\"toolbar_item[\s\S].*>?<\/a>/g, '').replace(/\n/g, '').replace(/<nobr aria-hidden=\"true\">(.*?)<\/nobr>/g, '').replace(/<script type=\"math\/tex\"[^>]*>(.*?)<\/script>/g, '');
  134. if (cnblog) {
  135. aContent = "(转载请删除括号里的内容)" + aContent;
  136. } else {
  137. /*处理csdn代码*/
  138. var rePre = /<pre[^>]*>(.*?)<\/pre>/gi;
  139. //aContent=aContent.replace(/\n/g,'');
  140. var arrMactches = aContent.match(rePre);
  141. if (arrMactches != null && arrMactches.length > 0) {
  142.  
  143. for (var i = 0; i < arrMactches.length; i++) {
  144.  
  145. var preText = '';
  146. var codeTag = document.getElementsByTagName('pre')[i];
  147. if(codeTag){
  148. if(codeTag.querySelector("ul[class*='pre-numbering']")){
  149. codeTag.querySelector("ul[class*='pre-numbering']").remove();
  150. }
  151. var eles = codeTag.getElementsByTagName('li');
  152. if (eles.length > 0) {
  153. for (var j = 0; j < eles.length; j++) {
  154. preText += eles[j].innerText+"\n";
  155. }
  156. } else {
  157. preText += codeTag.innerText;
  158. }
  159. }
  160. var preCodeHtml = "<pre><code class=\"hljs\">" + preText.replace(/</g, '&lt;').replace(/>/g, '&gt;') + "</code></pre>";
  161.  
  162. aContent = aContent.replace(arrMactches[i], preCodeHtml);
  163. }
  164. }
  165. aContent="(转载请删除括号里的内容)" + aContent;
  166. }
  167.  
  168. let article_title=aTitle;
  169. let article_content=aContent;
  170. let article_url=aUrl;
  171.  
  172. let article = { title: article_title,content:article_content,url:article_url};
  173. writeWin.postMessage(article, posteditUrl)
  174. },500);
  175. window.addEventListener('message', (e) => {
  176. // console.log(e)
  177. if(e.data=='Get'){
  178. console.log('Send over.')
  179. clearInterval(timer);
  180. }
  181. });
  182. }
  183. }
  184. })();