前景提要
前幾期寫了一篇關于谷歌插件制作的文章,但是因為沒有正經的小實戰,一直鴿了這么多期文章,這里寫一篇比較受關注的騰訊視頻的水印去除的文章,方便各位日后爬取騰訊視頻的時候總帶其水印爬蟲,
學習制作谷歌插件對于爬蟲喜歡使用 selenium 的人士有很大的幫助,可以解決訪問網頁出現的一系列問題,
很多人學習python,不知道從何學起,
很多人學習python,掌握了基本語法過后,不知道在哪里尋找案例上手,
很多已經做案例的人,卻不知道如何去學習更加高深的知識,
那么針對這三類人,我給大家提供一個好的學習平臺,免費領取視頻教程,電子書籍,以及課程的源代碼!
QQ群:961562169
回顧制作插件
簡單介紹一下 manifest.json 組態檔里面所需要的引數,
- name 拓展的名字,必須是字串型別
- version 字串型別,是當前插件的版本號
- description 拓展的介紹資訊
- permissions 是一個String陣列,每一個權限都使用String來表示,某些關鍵權限在安裝前會告知用戶
- cookies 啟動cookies權限
- tabs 啟動管理Chrome瀏覽器標簽欄權限
- activeTab 啟動與當前頁面互動的API權限
- contextMenus 啟動右鍵選單權限
- history 啟動歷史記錄權限
- storage 啟動本地存盤資料權限
- debugger 啟動使用debugger工具權限
- background 啟動拓展后端環境
- browser_action 瀏覽器的右上角顯示
- default_title 滑鼠移入,顯示簡短描述
- default_popup 滑鼠點擊,彈出的顯示內容
- default_icon 右上角拓展圖示
- background 常駐后臺腳本
- scripts 后臺常駐,直到關閉瀏覽器一直執行的腳本
- popup 除錯頁面
- persistent 持久運行
- content_scripts 向頁面注入腳本
- matches 匹配網址的正則串列
- run_at 選擇注入JS的時機
- document_start:所有css加載完畢,但DOM尚未創建時
- document_end:DOM創建完成,但圖片及frame等子資源尚未加載時
- document_idle:document_end之后,window.onload之前
- js 需要注入的腳本檔案串列
- all_frames 是否運行在頁面所有的frame中
- commands 使用命令 API 添加快捷鍵,并為它們系結默認的組合鍵
- attach-debugger 命令名
- suggested_key 設定組合鍵
- description 命令描述
- manifest_version manifest檔案版本號,Chrome18開始必須為2
Chrome拓展開發之去騰訊視頻廣告
去除廣告的原理很簡單,我們首先定位到元素面板的廣告節點,然后用JS方法remove去除,
這里我們使用Chrome插件來實作,定位的函式是document.querySelectorAll,獲取到的是DOM物件,因而可以直接使用它下屬的remove方法去除,這里的定位CSS選擇器我們需要根據具體的網站結構來撰寫,
這里的注入時機是在DOM渲染之后,也就是document.end,
全部代碼如下
manifest.json 檔案代碼如下:
{
"manifest_version": 2,
"name": "騰訊視頻去除水印",
"version": "0.0.1",
"description": "解決騰訊視頻爬蟲出現水印的現象,進入谷歌后自動執行去除水印.",
"author": "Lux",
"content_scripts": [
{
"matches": ["https://v.qq.com/x/cover/*"],
"run_at": "document_end",
"js": ["advertising.js"],
"all_frames": true
}
]
}
advertising.js 代碼如下:
0) {/n if (nowTime < maxTime) {/n let now = new Date().getTime();/n var method = threadArr[0];/n method();/n threadArr.splice(0, 1);/n let nowNew = (new Date().getTime() - now);/n nowTime += nowNew;/n doingThread();/n } else {/n/t/t nowTime=0;/n setTimeout(doingThread, 1);/n }/n }else {/n setTimeout(doingThread,100)/n }/n }/n}/nvar fn = function () {/n img = document.querySelectorAll('img.txp_waterMark_pic')\n if (img.length == 1){\n\timg.forEach(function(a){a.remove()});\n\treturn true;\n }\n else{\n\tconsole.log('not img');\n\treturn false\n }\n}\n\nvar thread = new thread();\nthread.start()\ntry{\n for (let i = 0; i < 1000000; i++) {\n \tthread.addThread(function () {\n if (fn()){throw 'jumpout';}\n })\n }\n}catch(e){}\n","classes":[]}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet">var thread = function () {
var nowTime = 0,
maxTime = 15;
var threadArr = [];
this.addThread = function (fn) {
threadArr.push(fn)
}
this.start=function () {
doingThread();
}
var doingThread = function () {
if (threadArr.length > 0) {
if (nowTime < maxTime) {
let now = new Date().getTime();
var method = threadArr[0];
method();
threadArr.splice(0, 1);
let nowNew = (new Date().getTime() - now);
nowTime += nowNew;
doingThread();
} else {
nowTime=0;
setTimeout(doingThread, 1);
}
}else {
setTimeout(doingThread,100)
}
}
}
var fn = function () {
img = document.querySelectorAll('img.txp_waterMark_pic')
if (img.length == 1){
img.forEach(function(a){a.remove()});
return true;
}
else{
console.log('not img');
return false
}
}
var thread = new thread();
thread.start()
try{
for (let i = 0; i < 1000000; i++) {
thread.addThread(function () {
if (fn()){throw 'jumpout';}
})
}
}catch(e){}
谷歌擴展打包
在Chrome擴展程式下將代碼打包進入 crx 中,
?
拿到crx之后即可分享給自己的小伙伴使用啦,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/157388.html
標籤:其他
