// ==UserScript==
// @name blankii脚本练习
// @version 0.1
// @author blankii
// @email blankii@qq.com
// @match https://www.baidu.com/
// @require http://code.jquery.com/jquery-latest.js
// @require https://greasyfork.org/scripts/380706-blankiijs/code/blankiiJS.js?version=682162
// @icon https://weibo.com/favicon.ico
// @namespace https://greasyfork.org/users/220272
// ==/UserScript==
var DB_NAME = 'indexedDB-test', VERSION = 1, db;
var request = indexedDB.open(DB_NAME, VERSION);
request.onsuccess = function(event) {
db = event.target.result;
// console.log(event.target === request); // true
db.onsuccess = function(event) {
console.log('数据库操作成功!');
};
db.onerror = function(event) {
console.error('数据库操作发生错误!', event.target.errorCode);
};
console.log('打开数据库成功!');
};
request.onerror = function(event) {
console.error('创建数据库出错');
console.error('error code:', event.target.errorCode);
};
request.onupgradeneeded = function(event) {
// 更新对象存储空间和索引 ....
console.log("onupgradeneeded")
var database = event.target.result;
var objectStore
if (!database.objectStoreNames.contains('800w')) {
objectStore = database.createObjectStore("800w",{ autoIncrement: true });
objectStore.createIndex('no', 'no', { unique: false });
objectStore.createIndex('plan_value', 'plan_value', { unique: false });
objectStore.createIndex('real_value', 'real_value', { unique: false });
objectStore.createIndex('result_value', 'result_value', { unique: false });
objectStore.createIndex('win', 'win', { unique: false });
objectStore.createIndex('rate', 'rate', { unique: false });
objectStore.createIndex('anl_model', 'anl_model', { unique: false });
objectStore.createIndex('isFollow', 'isFollow', { unique: false });
objectStore.createIndex('time', 'time', { unique: false });
}
};
function add(){
// 创建一个事务,类型:IDBTransaction,文档地址: https://developer.mozilla.org/en-US/docs/Web/API/IDBTransaction
var transaction = db.transaction('800w', 'readwrite');
// 通过事务来获取IDBObjectStore
var store = transaction.objectStore('800w');
var addPersonRequest = store.add({
no: '120',//期数
plan_value:'大',//计划
real_value: '大',//实际投注
result_value:'大',//实际结果
win:true,//是否赢
rate:1,//投注倍率
anl_model:'a111',//分析结果
isFollow:true,//是否跟计划
time:'2019-04-10 10:34:13'//时间
});
// 监听添加成功事件
addPersonRequest.onsuccess = function(e) {
console.log(e.target.result); // 打印添加成功数据的 主键(id)
};
// 监听失败事件
addPersonRequest.onerror = function(e) {
console.log(e.target.error);
};
}
setTimeout(function(){
getDataByKey("800w","name")
},333)
function getDataByKey(storeName){
console.log("select")
var transaction=db.transaction(storeName,'readwrite');
var store=transaction.objectStore(storeName);
var allRecords = store.getAll();
var str=""
allRecords.onsuccess = function() {
for(var key in allRecords.result){
var value=allRecords.result[key]
str=str+value.no+"\t"
+value.plan_value+"\t"
+value.real_value+"\t"
+value.result_value+"\t"
+value.win+"\t"
+value.rate+"\t"
+value.anl_model+"\t"
+value.isFollow+"\t"
+value.time+"\n"
}
console.log(str);
};
}