使用油猴脚本,自动转载博客园文章
今天在网站上看到别人写的快速添加随便和文章,网站如下
网址:https://greasyfork.org/zh-CN/scripts/31329-%E5%8D%9A%E5%AE%A2%E5%9B%AD/code
我就想自己是否可以也参考使用,用于转载文章呢?
需要完成以下任务:
添加按钮或链接,触发事件
在触发的事件中获取当前文章标题、内容、网址等信息
打开添加随便页面,把上面的信息自动填充到相应位置
手动选择分类、手动发布随笔。
以前写过一个使用bat发布博客的,今天还是想使用javascript脚本来尝试一把。
版本1
初步版本,基本可以使用了,应该还有很多bug需要修复,今天先这样吧
// ==UserScript== // @name 博客园快速转载随笔文章 // @namespace http://tampermonkey.net/ // @iconURL https://www.cnblogs.com/favicon.ico // @version 0.1 // @description 快速转载随笔文章! // @author JackMeng // @match http*://www.cnblogs.com/*/p/*.html // @match http*://i.cnblogs.com/* // @run-at document-end // @require https://cdn.bootcss.com/jquery/2.2.1/jquery.min.js // @grant GM_openInTab // @grant GM_setValue // @grant GM_getValue // ==/UserScript== (function() { 'use strict'; // Your code here... //window.onload = Delay(); $(document).ready(function(){ // 在此处执行希望在页面加载完成后执行的操作 setTimeout(AddWrite,2000); }); function AddWrite() { var host = window.location.host; if(host=="www.cnblogs.com") { var blogTitle=$('#cb_post_title_url')[0].innerText; var blogUrl=$('#cb_post_title_url')[0].baseURI; var blogBody=$('#cnblogs_post_body')[0].outerHTML; GM_setValue("blogTitle",blogTitle); GM_setValue("blogUrl",blogUrl); GM_setValue("blogBody",blogBody); var par = document.getElementById("span_userinfo"); par=document.body; var f = par.firstElementChild; //打开百度页面 //active:true,新标签页获取页面焦点 //setParent :true:新标签页面关闭后,焦点重新回到源页面 //const newTap = GM_openInTab("https://i.cnblogs.com/EditPosts.aspx?opt=1",{ active: true, setParent :true}); //debugger; var a = document.createElement("a"); a.href="https://i.cnblogs.com/EditPosts.aspx?opt=1"; a.style="position: fixed; z-index: 99; bottom: 2px; right: 120px;"; a.target="_blank" a.innerText="添加随笔"; par.insertBefore(a,f); } else { $("#post-title").val(GM_getValue("blogTitle")+"=");//可尝试innerHTML试试 //$('#post-title').prop('value', GM_getValue("blogTitle")); //document.getElementById("post-title").value=GM_getValue("blogTitle"); document.getElementsByClassName("mceIcon mce_code")[0].click(); setTimeout(setBody,1000); } } function setBody() { var u=GM_getValue("blogUrl"); var b=GM_getValue("blogBody"); var ifm = $("#mce_37_ifr")[0] var aa =ifm.contentDocument.getElementById("htmlSource"); aa.value=b+"<p> </p><p>出处:"+u+"</p>"; var bb =ifm.contentDocument.getElementById("insert"); bb.click(); } })();
版本2
1)修复z-index会遮挡的问题,
2)增加背景色,醒目
// ==UserScript== // @name 博客园快速转载随笔文章 // @namespace http://tampermonkey.net/ // @iconURL https://www.cnblogs.com/favicon.ico // @version 0.2 // @description 快速转载随笔文章! // @author JackMeng // @match http*://www.cnblogs.com/*/p/*.html // @match http*://i.cnblogs.com/* // @run-at document-end // @require https://cdn.bootcss.com/jquery/2.2.1/jquery.min.js // @grant GM_openInTab // @grant GM_setValue // @grant GM_getValue // ==/UserScript== (function() { 'use strict'; // Your code here... //window.onload = Delay(); $(document).ready(function(){ // 在此处执行希望在页面加载完成后执行的操作 setTimeout(AddWrite,2000); }); function AddWrite() { var host = window.location.host; if(host=="www.cnblogs.com") { var blogTitle=$('#cb_post_title_url')[0].innerText; var blogUrl=$('#cb_post_title_url')[0].baseURI; var blogBody=$('#cnblogs_post_body')[0].outerHTML; GM_setValue("blogTitle",blogTitle); GM_setValue("blogUrl",blogUrl); GM_setValue("blogBody",blogBody); var par = document.getElementById("span_userinfo"); par=document.body; var f = par.firstElementChild; //debugger; var a = document.createElement("a"); a.href="https://i.cnblogs.com/EditPosts.aspx?opt=1"; a.style="position: fixed; z-index: 9999; bottom: 2px; right: 120px;background-color:#00f;color:#fff;"; a.target="_blank" a.innerText="添加随笔"; par.insertBefore(a,f); } else { $("#post-title").val(GM_getValue("blogTitle")+"=");//可尝试innerHTML试试 //$('#post-title').prop('value', GM_getValue("blogTitle")); //document.getElementById("post-title").value=GM_getValue("blogTitle"); document.getElementsByClassName("mceIcon mce_code")[0].click(); setTimeout(setBody,1000); } } function setBody() { var u=GM_getValue("blogUrl"); var b=GM_getValue("blogBody"); var ifm = $("#mce_37_ifr")[0] var aa =ifm.contentDocument.getElementById("htmlSource"); aa.value=b+"<p> </p><p>出处:"+u+"</p>"; var bb =ifm.contentDocument.getElementById("insert"); bb.click(); } })();
版本3
1)背景色两边预留padding样式
2)调整数据的保存方式(单击时获取数据),并增加使用后对数据清理的功能(在编辑页面绑定完成后清理数据)
3)修改为Promise的链式调用
// ==UserScript== // @name 博客园快速转载随笔文章 // @namespace http://tampermonkey.net/ // @iconURL https://www.cnblogs.com/favicon.ico // @version 0.3 // @description 快速转载随笔文章! // @author JackMeng // @match http*://www.cnblogs.com/*/p/*.html // @match http*://i.cnblogs.com/* // @run-at document-end // @require https://cdn.bootcss.com/jquery/2.2.1/jquery.min.js // @grant GM_openInTab // @grant GM_setValue // @grant GM_getValue // @grant GM_deleteValue // ==/UserScript== (function() { 'use strict'; // Your code here... $(document).ready(function(){ // 在此处执行希望在页面加载完成后执行的操作 setTimeout(AddWrite,1000); }); async function AddWrite() { var host = window.location.host; if(host=="www.cnblogs.com") { var par = document.getElementById("span_userinfo"); par=document.body; var f = par.firstElementChild; //debugger; var a = document.createElement("a"); a.id="aLink"; a.href="https://i.cnblogs.com/EditPosts.aspx?opt=1"; a.style="position: fixed; z-index: 9999; bottom: 2px; right: 120px;background-color:#00f;color:#fff;padding:0 6px;"; a.target="_blank" a.onclick = ()=>{ var blogTitle=$('#cb_post_title_url')[0].innerText; var blogUrl=$('#cb_post_title_url')[0].baseURI; var blogBody=$('#cnblogs_post_body')[0].outerHTML; GM_setValue("blogTitle",blogTitle); GM_setValue("blogUrl",blogUrl); GM_setValue("blogBody",blogBody); }; a.innerText="添加随笔"; par.insertBefore(a,f); } else { var bTitle = GM_getValue("blogTitle")+"="; if(bTitle!="undefined=") { $("#post-title").val(bTitle);//可尝试innerHTML试试 //$('#post-title').prop('value', GM_getValue("blogTitle")); //document.getElementById("post-title").value=GM_getValue("blogTitle"); //var p = new Promise((res,rej) => setTimeout(()=>{document.getElementsByClassName("mceIcon mce_code")[0].click();console.log(111);},800)) //.then(() => setTimeout(setBody,1500)) //.then(()=>clearData()); sleep(1000) .then(()=>{document.getElementsByClassName("mceIcon mce_code")[0].click();}) .then(() => sleep(500)) .then(() => setBody()) .then(() => sleep(500)) .then(() => clearData()); } } } function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } function setBody() { //debugger; var u=GM_getValue("blogUrl"); var b=GM_getValue("blogBody"); var ifm = $("#mce_37_ifr")[0] var aa =ifm.contentDocument.getElementById("htmlSource"); aa.value=b+"<p> </p><p>出处:"+u+"</p>"; var bb =ifm.contentDocument.getElementById("insert"); bb.click(); } function clearData() { GM_deleteValue("blogTitle"); GM_deleteValue("blogUrl"); GM_deleteValue("blogBody"); } })();
版本4
1)增加匹配csdn的功能
2)在出处的地方增加原文链接
// ==UserScript== // @name 博客园、csdn转载文章 // @namespace http://tampermonkey.net/ // @iconURL https://www.cnblogs.com/favicon.ico // @version 0.4 // @description 快速转载随笔文章! // @author JackMeng // @match http*://www.cnblogs.com/*/p/* // @match http*://blog.csdn.net/* // @match http*://i.cnblogs.com/* // @run-at document-end // @require https://cdn.bootcss.com/jquery/2.2.1/jquery.min.js // @grant GM_openInTab // @grant GM_setValue // @grant GM_getValue // @grant GM_deleteValue // ==/UserScript== (function() { 'use strict'; // Your code here... $(document).ready(function(){ // 在此处执行希望在页面加载完成后执行的操作 setTimeout(AddWrite,1000); }); async function AddWrite() { var par=document.body; var f = par.firstElementChild; var host = window.location.host; if(host=="www.cnblogs.com" || host=="blog.csdn.net") { //debugger; var a = document.createElement("a"); a.id="aLink"; a.href="https://i.cnblogs.com/EditPosts.aspx?opt=1"; a.style="position: fixed; z-index: 9999; bottom: 2px; right: 120px;background-color:#00f;color:#fff;padding:0 6px;"; a.target="_blank" a.onclick = getBlogData; a.innerText="添加随笔"; par.insertBefore(a,f); } else { var bTitle = GM_getValue("blogTitle")+"="; if(bTitle!="undefined=") { $("#post-title").val(bTitle);//可尝试innerHTML试试 //$('#post-title').prop('value', GM_getValue("blogTitle")); //document.getElementById("post-title").value=GM_getValue("blogTitle"); //var p = new Promise((res,rej) => setTimeout(()=>{document.getElementsByClassName("mceIcon mce_code")[0].click();console.log(111);},800)) //.then(() => setTimeout(setBody,1500)) //.then(()=>clearData()); sleep(1000) .then(()=>{document.getElementsByClassName("mceIcon mce_code")[0].click();}) .then(() => sleep(500)) .then(() => setBody()) .then(() => sleep(500)) .then(() => clearData()); } } } function getBlogData() { var host = window.location.host; var blogTitle=""; var blogUrl=""; var blogBody=""; if(host=="www.cnblogs.com" ) { blogTitle=$('#cb_post_title_url')[0].innerText; blogUrl=$('#cb_post_title_url')[0].baseURI; blogBody=$('#cnblogs_post_body')[0].outerHTML; } if(host=="blog.csdn.net") { blogTitle=articleTitle; blogUrl=articleDetailUrl; blogBody=$("#content_views")[0].outerHTML; } GM_setValue("blogTitle",blogTitle); GM_setValue("blogUrl",blogUrl); GM_setValue("blogBody",blogBody); } function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } function setBody() { //debugger; var u=GM_getValue("blogUrl"); var b=GM_getValue("blogBody"); var ifm = $("#mce_37_ifr")[0] var aa =ifm.contentDocument.getElementById("htmlSource"); aa.value=b+"<p></p><p>【出处】:<a href='"+u+"' target='_blank'>"+u+"</a></p>"; aa.value+="<p>=======================================================================================</p>"; var bb =ifm.contentDocument.getElementById("insert"); bb.click(); } function clearData() { GM_deleteValue("blogTitle"); GM_deleteValue("blogUrl"); GM_deleteValue("blogBody"); } })();
版本5
1)增加对博客园存档文章的转载
2)增加时间信息
// ==UserScript== // @name 博客园、csdn转载文章 // @namespace http://tampermonkey.net/ // @iconURL https://www.cnblogs.com/favicon.ico // @version 0.5 // @description 快速转载随笔文章! // @author JackMeng // @match http*://www.cnblogs.com/*/p/* // @match http*://www.cnblogs.com/*/archive/* // @match http*://blog.csdn.net/* // @match http*://i.cnblogs.com/* // @run-at document-end // @require https://cdn.bootcss.com/jquery/2.2.1/jquery.min.js // @grant GM_openInTab // @grant GM_setValue // @grant GM_getValue // @grant GM_deleteValue // ==/UserScript== (function() { 'use strict'; Date.prototype.Format = function(fmt){ var o = { "M+" : this.getMonth()+1, //月份 "d+" : this.getDate(), //日 "h+" : this.getHours(), //小时 "m+" : this.getMinutes(), //分 "s+" : this.getSeconds(), //秒 "q+" : Math.floor((this.getMonth()+3)/3), //季度 "S" : this.getMilliseconds() //毫秒 }; if(/(y+)/.test(fmt)) fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); for(var k in o) if(new RegExp("("+ k +")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length))); return fmt; } // Your code here... $(document).ready(function(){ // 在此处执行希望在页面加载完成后执行的操作 setTimeout(AddWrite,1000); }); async function AddWrite() { var par=document.body; var f = par.firstElementChild; var host = window.location.host; if(host=="www.cnblogs.com" || host=="blog.csdn.net") { //debugger; var a = document.createElement("a"); a.id="aLink"; a.href="https://i.cnblogs.com/EditPosts.aspx?opt=1"; a.style="position: fixed; z-index: 9999; bottom: 2px; right: 120px;background-color:#00f;color:#fff;padding:0 6px;"; a.target="_blank" a.onclick = getBlogData; a.innerText="添加随笔"; par.insertBefore(a,f); } else { var bTitle = GM_getValue("blogTitle")+"="; if(bTitle!="undefined=") { $("#post-title").val(bTitle);//可尝试innerHTML试试 //$('#post-title').prop('value', GM_getValue("blogTitle")); //document.getElementById("post-title").value=GM_getValue("blogTitle"); //var p = new Promise((res,rej) => setTimeout(()=>{document.getElementsByClassName("mceIcon mce_code")[0].click();console.log(111);},800)) //.then(() => setTimeout(setBody,1500)) //.then(()=>clearData()); sleep(1000) .then(()=>{document.getElementsByClassName("mceIcon mce_code")[0].click();}) .then(() => sleep(500)) .then(() => setBody()) .then(() => sleep(500)) .then(() => clearData()); } } } function getBlogData() { var host = window.location.host; var blogTitle=""; var blogUrl=""; var blogBody=""; if(host=="www.cnblogs.com" ) { blogTitle=$('#cb_post_title_url')[0].innerText; blogUrl=$('#cb_post_title_url')[0].baseURI; blogBody=$('#cnblogs_post_body')[0].outerHTML; } if(host=="blog.csdn.net") { blogTitle=articleTitle; blogUrl=articleDetailUrl; blogBody=$("#content_views")[0].outerHTML; } GM_setValue("blogTitle",blogTitle); GM_setValue("blogUrl",blogUrl); GM_setValue("blogBody",blogBody); } function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } function setBody() { //debugger; var u=GM_getValue("blogUrl"); var b=GM_getValue("blogBody"); var ifm = $("#mce_37_ifr")[0] var aa =ifm.contentDocument.getElementById("htmlSource"); //aa.value=b+"<p></p><p>"+new Date().Format("yyyy-MM-dd hh:mm:ss")+"【出处】:<a href='"+u+"' target='_blank'>"+u+"</a></p>"; aa.value=b+`<p></p><p>${new Date().Format("yyyy-MM-dd hh:mm:ss")}【出处】:<a href='${u}' target='_blank'>${u}</a></p>`; aa.value+="<p>=======================================================================================</p>"; var bb =ifm.contentDocument.getElementById("insert"); bb.click(); } function clearData() { GM_deleteValue("blogTitle"); GM_deleteValue("blogUrl"); GM_deleteValue("blogBody"); } })();
版本6
1)使用@exclude排除指定URL的页面
2)界面改为一键转载
// ==UserScript== // @name 文章转载:博客园、csdn // @namespace http://tampermonkey.net/ // @iconURL https://www.cnblogs.com/favicon.ico // @version 0.6 // @description 快速转载随笔文章! // @author JackMeng // @match http*://www.cnblogs.com/*/p/* // @match http*://www.cnblogs.com/*/archive/* // @match http*://blog.csdn.net/* // @match http*://i.cnblogs.com/* // @exclude http*://www.cnblogs.com/mq0036* // @run-at document-end // @require https://cdn.bootcss.com/jquery/2.2.1/jquery.min.js // @grant GM_openInTab // @grant GM_setValue // @grant GM_getValue // @grant GM_deleteValue // ==/UserScript== (function() { 'use strict'; Date.prototype.Format = function(fmt){ var o = { "M+" : this.getMonth()+1, //月份 "d+" : this.getDate(), //日 "h+" : this.getHours(), //小时 "m+" : this.getMinutes(), //分 "s+" : this.getSeconds(), //秒 "q+" : Math.floor((this.getMonth()+3)/3), //季度 "S" : this.getMilliseconds() //毫秒 }; if(/(y+)/.test(fmt)) fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); for(var k in o) if(new RegExp("("+ k +")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length))); return fmt; } // Your code here... $(document).ready(function(){ // 在此处执行希望在页面加载完成后执行的操作 setTimeout(AddWrite,1000); }); async function AddWrite() { var par=document.body; var f = par.firstElementChild; var host = window.location.host; if(host=="www.cnblogs.com" || host=="blog.csdn.net") { //debugger; var a = document.createElement("a"); a.id="aLink"; a.href="https://i.cnblogs.com/EditPosts.aspx?opt=1"; a.style="position: fixed; z-index: 9999; bottom: 2px; right: 120px;background-color:#00f;color:#fff;padding:0 6px;"; a.target="_blank" a.onclick = getBlogData; a.innerText="一键转载"; par.insertBefore(a,f); } else { var bTitle = GM_getValue("blogTitle")+"="; if(bTitle!="undefined=") { $("#post-title").val(bTitle);//可尝试innerHTML试试 //$('#post-title').prop('value', GM_getValue("blogTitle")); //document.getElementById("post-title").value=GM_getValue("blogTitle"); //var p = new Promise((res,rej) => setTimeout(()=>{document.getElementsByClassName("mceIcon mce_code")[0].click();console.log(111);},800)) //.then(() => setTimeout(setBody,1500)) //.then(()=>clearData()); sleep(1000) .then(()=>{document.getElementsByClassName("mceIcon mce_code")[0].click();}) .then(() => sleep(500)) .then(() => setBody()) .then(() => sleep(500)) .then(() => clearData()); } } } function getBlogData() { var host = window.location.host; var blogTitle=""; var blogUrl=""; var blogBody=""; if(host=="www.cnblogs.com" ) { blogTitle=$('#cb_post_title_url')[0].innerText; blogUrl=$('#cb_post_title_url')[0].baseURI; blogBody=$('#cnblogs_post_body')[0].outerHTML; } if(host=="blog.csdn.net") { blogTitle=articleTitle; blogUrl=articleDetailUrl; blogBody=$("#content_views")[0].outerHTML; } GM_setValue("blogTitle",blogTitle); GM_setValue("blogUrl",blogUrl); GM_setValue("blogBody",blogBody); } function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } function setBody() { //debugger; var u=GM_getValue("blogUrl"); var b=GM_getValue("blogBody"); var ifm = $("#mce_37_ifr")[0] var aa =ifm.contentDocument.getElementById("htmlSource"); //aa.value=b+"<p></p><p>"+new Date().Format("yyyy-MM-dd hh:mm:ss")+"【出处】:<a href='"+u+"' target='_blank'>"+u+"</a></p>"; aa.value=b+`<p></p><p>${new Date().Format("yyyy-MM-dd hh:mm:ss")}【出处】:<a href='${u}' target='_blank'>${u}</a></p>`; aa.value+="<p>=======================================================================================</p>"; var bb =ifm.contentDocument.getElementById("insert"); bb.click(); } function clearData() { GM_deleteValue("blogTitle"); GM_deleteValue("blogUrl"); GM_deleteValue("blogBody"); } })();
关注我】。(●'◡'●)
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【因为,我的写作热情也离不开您的肯定与支持,感谢您的阅读,我是【Jack_孟】!
本文来自博客园,作者:jack_Meng,转载请注明原文链接:https://www.cnblogs.com/mq0036/p/17606390.html
【免责声明】本文来自源于网络,如涉及版权或侵权问题,请及时联系我们,我们将第一时间删除或更改!