生活随笔
收集整理的這篇文章主要介紹了
屏蔽博客园背景动态线条
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 問題描述
由于博客園允許用戶添加含js的html代碼來裝飾自己的博客界面,這導致很多人跟風地給自己博客添加背景動態線條、live2D等,其中動態背景線條如下圖所示:
它不停地隨機生成線條,并漂浮移動,一段時間后消失。但如果鼠標靠近,就會吸附到鼠標上,且不會消失。
當我們閱讀文章時,如果鼠標停留在頁面上,那么鼠標位置就會不停地吸附線條,然后越來越多(達到最大數量時,貌似就不生成了),且不停地計算旋轉變換,這會占用一部分的CPU資源,如果其他程序也開得挺多的話,可能導致CPU風扇狂轉。
這么沒用的東西,既占CPU還分散注意力,應該統統禁掉!
2. 動態線條js加載過程
當我們打開一個博客園用戶主頁或某篇文章時,網址格式如下:
https://www.cnblogs.com/oldwangcy/p/11371550.html
此 html 會請求下載blog-common.min.js,在這個js文件中定義了一個函數loadBlogNews,然后html文件內部script會稍后調用 loadBlogNews()。
loadBlogNews 的定義如下:
function loadBlogNews() {$
.ajax({url
: getAjaxBaseUrl() + "news.aspx",type
: "get",dataType
: "text",success
: function(n
) {if (n
)if (n
.indexOf("<script") < n
.indexOf("<script>getFollowStatus")) {if (n
.indexOf("cdn.jsdelivr.net/gh/BNDong/Cnblogs-Theme-SimpleMemory") >= 0) {var t
= n
.match(/GhUserName *: *['"](\w+)['"]/);(t
=== null || t
[1].toUpperCase() === "BNDong".toUpperCase()) && (n
= n
.replace(/(GhVersions *: *["'])v1\.([0-1]\.\d+|2\.[0-5].*?),*/g, "$1v1.2.6").replace(/(Cnblogs-Theme-SimpleMemory@)v1\.([0-1]\.\d+|2\.[0-5])/gi, "$1v1.2.6"))}$
.getScript(location
.protocol
+ "//common.cnblogs.com/script/jquery.writeCapture-min.js", function() {$("#sidebar_news").writeCapture().html(n
).show()})} elsen
.indexOf("錯誤提示:發生了異常") < 0 && $("#sidebar_news").html(n
).show()}})
}
可以看到,此函數將使用 $.ajax 下載了一個名為 new.aspx 的文件,然后這一句$("#sidebar_news").writeCapture().html(n).show()執行了此文件的內容。
可以在瀏覽器開發者工具-network下找到此文件。文件內容大概如下(不同的人可能不一樣):
<h3 class="catListTitle">公告
</h3>
<div id="blog-news"><div style = "display:none;">動態線條
</div><script>!function(){function n(n,e,t){return n.getAttribute(e)||t}function e(n){return document.getElementsByTagName(n)}function t(){var t=e("script"),o=t.length,i=t[o-1];return{l:o,z:n(i,"zIndex",-1),o:n(i,"opacity",.5),c:n(i,"color","0,0,0"),n:n(i,"count",99)}}function o(){a=m.width=window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth,c=m.height=window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight}function i(){r.clearRect(0,0,a,c);var n,e,t,o,m,l;s.forEach(function(i,x){for(i.x+=i.xa,i.y+=i.ya,i.xa*=i.x>a||i.x<0?-1:1,i.ya*=i.y>c||i.y<0?-1:1,r.fillRect(i.x-.5,i.y-.5,1,1),e=x+1;e<u.length;e++)n=u[e],null!==n.x&&null!==n.y&&(o=i.x-n.x,m=i.y-n.y,l=o*o+m*m,l<n.max&&(n===y&&l>=n.max/2&&(i.x-=.03*o,i.y-=.03*m),t=(n.max-l)/n.max,r.beginPath(),r.lineWidth=t/2,r.strokeStyle="rgba("+d.c+","+(t+.2)+")",r.moveTo(i.x,i.y),r.lineTo(n.x,n.y),r.stroke()))}),x(i)}var a,c,u,m=document.createElement("canvas"),d=t(),l="c_n"+d.l,r=m.getContext("2d"),x=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(n){window.setTimeout(n,1e3/45)},w=Math.random,y={x:null,y:null,max:2e4};m.id=l,m.style.cssText="position:fixed;top:0;left:0;z-index:"+d.z+";opacity:"+d.o,e("body")[0].appendChild(m),o(),window.onresize=o,window.onmousemove=function(n){n=n||window.event,y.x=n.clientX,y.y=n.clientY},window.onmouseout=function(){y.x=null,y.y=null};for(var s=[],f=0;d.n>f;f++){var h=w()*a,g=w()*c,v=2*w()-1,p=2*w()-1;s.push({x:h,y:g,xa:v,ya:p,max:6e3})}u=s.concat([y]),setTimeout(function(){i()},100)}();</script><div style = "display:none;"> 動態線條end
</div><div id="profile_block">昵稱:
<a href="https://home.cnblogs.com/u/baby-lily/">Baby-Lily
</a><br />園齡:
<a href="https://home.cnblogs.com/u/baby-lily/" title="入園時間:2019-03-19">1年5個月
</a><br />粉絲:
<a href="https://home.cnblogs.com/u/baby-lily/followers/">24
</a><br />關注:
<a href="https://home.cnblogs.com/u/baby-lily/followees/">9
</a><div id="p_b_follow"></div><script>getFollowStatus('73594955-3c31-45bb-d67b-08d6ab35ac30');</script></div>
</div>
顯然,用戶自定義的腳本就放在這里。
3. 解決方案
既然已經知道了是怎樣加載的,那么我們可以寫一個腳本,交給TamperMonkey這個插件去執行。
大致思路是這樣
- 檢測loadBlogNews是否已成功定義,如果是,那么重寫一個函數覆蓋掉。
- 重寫的函數與原來的基本一致,但是在 new.aspx 被執行前,用正則把用戶自定義的腳本替換成空字符串。
代碼如下:
在 油猴中新建腳本,粘貼如下代碼,保存。
(function() {'use strict';let maxCheckNum
= 50;function check_f(){try {if (typeof(loadBlogNews
) == "function") {return true;}} catch(e
) {return false;}return false;}function tryStopIt(){if (!check_f()){if (maxCheckNum
> 0){maxCheckNum
-= 1setTimeout(tryStopIt
, 100); } else {console
.log("未找到,替換失敗。");}return;} loadBlogNews = function () {$
.ajax({url
: getAjaxBaseUrl() + "news.aspx",type
: "get",dataType
: "text",success
: function(n
) {if (n
)if (n
.indexOf("<script") < n
.indexOf("<script>getFollowStatus")) {if (n
.indexOf("cdn.jsdelivr.net/gh/BNDong/Cnblogs-Theme-SimpleMemory") >= 0) {var t
= n
.match(/GhUserName *: *['"](\w+)['"]/);(t
=== null || t
[1].toUpperCase() === "BNDong".toUpperCase()) && (n
= n
.replace(/(GhVersions *: *["'])v1\.([0-1]\.\d+|2\.[0-5].*?),*/g, "$1v1.2.6").replace(/(Cnblogs-Theme-SimpleMemory@)v1\.([0-1]\.\d+|2\.[0-5])/gi, "$1v1.2.6"))}$
.getScript(location
.protocol
+ "//common.cnblogs.com/script/jquery.writeCapture-min.js", function() {let reg1
= new RegExp("[\\r\\n]", "g");let reg2
= new RegExp("<script.*?>.*?window\\.requestAnimationFrame.*?</script>","g");let filted_content
= n
.replace(reg1
," ");filted_content
= filted_content
.replace(reg2
, " ");let reg3
= new RegExp("<script.+?src=[\"'](.+?)[\"'].*?>.*?</script>","g");filted_content
= filted_content
.replace(reg3
, function(subtxt
, cat1
, pian
, txt
){let bad
= false;$
.ajax({url
:cat1
,type
:"get",dataType
:"text",async:false,success
:function(resp
){resp
= resp
.replace(reg1
, " ");let reg4
= new RegExp("<script.*?>.*?window\\.requestAnimationFrame.*?</script>","g");if (reg4
.exec(resp
)) {bad
= true;}}});if (bad
) {return " ";}return subtxt
;});console
.alert("debug : " + filted_content
);$("#sidebar_news").writeCapture().html(filted_content
).show();})} elsen
.indexOf("錯誤提示:發生了異常") < 0 && $("#sidebar_news").html(n
).show();}}) } console
.log("成功地替換了 loadBlogNews");return;}tryStopIt();
})();
4. 效果測試
隨便找了個有動態線條的博客:https://www.cnblogs.com/oldwangcy/p/11371550.html
關閉腳本時:
打開腳本時:
總結
以上是生活随笔為你收集整理的屏蔽博客园背景动态线条的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。