查詢寶箱當然要有數據源,我們把每次搶到的寶箱都保存記錄,這樣,我們可以查詢一段時間內的“成果”,
我第一個想到的是 寫入數據庫,一個小巧的數據庫,當然選mysql啦,如果不想安裝其它東西,瀏覽器自帶的IndexedDB,也行,因為Web Sql 不能持久化,所以不用。
先說數據從哪來,每次搶寶箱成功,都會 彈出,”恭喜您,領取了XXXX 個魚丸”, ” 恭喜您,領取了XXXX 個穩”,然后 查出在 mod-all1.js內,this.appendTips(n),這個n會變動的。
function insertData(_data) { console.log(_data); window.postMessage({"insertSql": _data}, '*'); };
可以在外面定義一個 方法,用window.postMessage傳給popup層,popoup 把數據處理后,傳給background層,background 寫入數據庫。
Popup接收
//接收 window.postMessage({"insertSql": _data}, '*'); 類型的消息
window.addEventListener("message", function(e) { if (e.data.hasOwnProperty("insertSql") ) { var data = e.data.insertSql; data.roomId = roomObj.getRoomId(); //房間id data.time = RoomObj.formatDateTime(new Date());//格式化后的當前時間("YYYY-MM-DD HH:mm:ss") }, false);
再用 chrome.runtime.sendMessage(
_data, //數據
function(response) {
}
);
發給 Background層
Background 用chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){}接收,然后用WebSocket 發給后端,后端,我用C#寫的,調用 Fleck.dll 內的WebSocketServer 與js對接,拿到數據,寫入 數據庫。
有些人 不喜歡安裝其他東西,也不喜歡運行其它程序,那就只能放入 瀏覽器IndexedDB內了,但一卸載擴展數據就會丟失,查詢大量數據時,也耗時,
background層 加載indexedDB.js
//初始化數據庫
1 var request; 2 3 var db; 4 5 function initDB(_name,_version){ 6 7 try { 8 9 if (!window.indexedDB) { 10 11 console.log("browser doesn't support indexedDB"); 12 13 } else { 14 15 var name = _name || 'test'; 16 17 var version = _version || 2; 18 19 20 21 request = window.indexedDB.open(_name,_version); 22 23 request.onerror = function (event) { 24 25 console.log('數據庫打開報錯'); 26 27 }; 28 29 request.onsuccess = function (event) { 30 31 db = request.result; 32 33 console.log('數據庫打開成功'); 34 35 }; 36 37 //增加數據庫版本號時,會觸發onupgradeneeded事件(會在onsuccess之前被調用) 38 39 request.onupgradeneeded = function (event) { 40 41 db = event.target.result; 42 43 }; 44 45 } 46 47 } catch(e) { 48 49 console.log(e.message); 50 51 } 52 53 }; 54 55 //初始化 indexedDB 56 57 initDB("DouyuRoom",2); 58 59 request.onupgradeneeded = function(event) { 60 61 db = event.currentTarget.result; 62 63 var objectStore = db.createObjectStore('t_treasure',{keyPath: 'id',autoIncrement: true }); 64 65 }; 66 67 function Treasure() { 68 69 } 70 71 var treasure = new Treasure(); 72 73 Treasure.add = function(_obj) { 74 75 var tx = db.transaction("t_treasure", 'readwrite'); 76 77 var store = tx.objectStore("t_treasure"); 78 79 _obj.time= formatDateTime(new Date()); 80 81 console.log(_obj); 82 83 store.add(_obj); 84 85 }; 86 87 88 89 function time1Lesstime2(d1,d2) 90 91 { 92 93 return d1.replace(/-/g,"").replace(/:/g,"").replace(/ /g,"")< d2.replace(/-/g,"").replace(/:/g,"").replace(/ /g,""); 94 95 }; 96 97 var dataFromIndexDb; 98 99 Treasure.find = function(_s,_e){ 100 101 _s=_s +"00:00:00"; 102 103 _e=_e +"23:59:59"; 104 105 dataFromIndexDb=[]; 106 107 var objectStore = db.transaction('t_treasure').objectStore('t_treasure'); 108 109 objectStore.openCursor().onsuccess = function (event) { 110 111 var cursor = event.target.result; 112 113 if (cursor) { 114 115 if (time1Lesstime2(_s,cursor.value.time) &&time1Lesstime2(cursor.value.time,_e)){ 116 117 var t= {time:cursor.value.time,roomId: cursor.value.roomId,src_nick:cursor.value.src_nick,award_type:cursor.value.award_type,silver:cursor.value.silver,prop_count:cursor.value.prop_count}; 118 119 // console.log('time: ' + cursor.value.time); 120 121 dataFromIndexDb.push(t); 122 123 cursor.continue(); 124 125 } 126 127 } else { 128 129 //console.log('沒有數據了!'); 130 131 } 132 133 }; 134 135 }; 136 137 function formatDateTime(inputTime) { 138 139 var date = new Date(inputTime); 140 141 var y = date.getFullYear(); 142 143 var m = date.getMonth() + 1; 144 145 m = m < 10 ? ('0' + m) : m; 146 147 var d = date.getDate(); 148 149 d = d < 10 ? ('0' + d) : d; 150 151 var h = date.getHours(); 152 153 h = h < 10 ? ('0' + h) : h; 154 155 var minute = date.getMinutes(); 156 157 var second = date.getSeconds(); 158 159 minute = minute < 10 ? ('0' + minute) : minute; 160 161 second = second < 10 ? ('0' + second) : second; 162 163 return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second; 164 165 };
這樣,就會寫入 瀏覽器的IndexedDB ,
至於查詢頁面,有數據源了,大家各有各的方法顯示查詢結果,我用的是vue.js 與 組件 | Element ,