因業務需要,寫了個manifest資源提取工具,該機制是將html文件作爲入口文件進行資源抓取。原理是先簡單掃html token,然後直接遍歷每個tag token是否屬於需要的資源(css,js,img),獲取到css link的時候,再抓取裏的圖片文件,最後再合併成資源集合對象並創建manifest文件。
安裝:
$ npm install manifest-res
使用:
var mr = require("manifest-res");
mr.build("http://www.cnblogs.com", "./cnblogs.appcache", function(data){
console.log(data);
});
該例子成功後會在當前目錄下創建cnblogs.appcache文件。
對象方法說明:
build(file, manifestFileName, callback);
參數:
file參數可以是本地html文件路徑,也可以是http路徑;
manifestFileName爲新生成的文件名;
callback回調接收一個data(Array)參數,爲提取出來的資源列表;
項目地址:https://github.com/randomyang/manifest-res