MTurk HIT DataBase Patch v1

Removes current day's hits from the DB to prepare it for the new update

נכון ליום 03-06-2015. ראה הגרסה האחרונה.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        MTurk HIT DataBase Patch v1
// @namespace https://greasyfork.org/users/710
// @description Removes current day's hits from the DB to prepare it for the new update
// @include     https://www.mturk.com/mturk/dashboard
// @version     4
// @grant       none
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
// ==/UserScript==

var HITStorage = {};
var indexedDB = window.indexedDB || window.webkitIndexedDB ||
    window.mozIndexedDB;
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.mozIDBTransaction;
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.mozIDBKeyRange;
HITStorage.IDBTransactionModes = { "READ_ONLY": "readonly", "READ_WRITE": "readwrite", "VERSION_CHANGE": "versionchange" };
var IDBKeyRange = window.IDBKeyRange;

HITStorage.indexedDB = {};
HITStorage.indexedDB = {};
HITStorage.indexedDB.db = null;

HITStorage.indexedDB.onerror = function(e) {
    console.log(e);
};
var v = 4;
var dates = [];
var dates2 = [];

if (confirm("This will delete all hits done from your DB for 2015-06-02."))
{
    var request = indexedDB.open("HITDB", 4);
    request.onsuccess = function(e) {
        HITStorage.indexedDB.db = e.target.result;
        var db = HITStorage.indexedDB.db;
        var trans = db.transaction(["HIT"], HITStorage.IDBTransactionModes.READ_WRITE);
        var store = trans.objectStore("HIT");
        range = IDBKeyRange.bound("2015-04-19", "2015-06-02", false, false);
        range2 = IDBKeyRange.bound("1900-01-01","2015-04-19", false, false);

        store.index('date').openCursor(range).onsuccess = function(event) {
            var cursor = event.target.result;
            if (cursor)
            {
                if (cursor.value.hitId.indexOf("?") > -1)
                {
                    console.log(cursor.value);
                    dates.push(cursor.value.date);
                    //cursor.delete();
                }
                cursor.continue();
            }
            else
            {
                db.close();
            }
        };
    }
    request = indexedDB.open("HITDB", 4);
    request.onsuccess = function(e) {
        HITStorage.indexedDB.db = e.target.result;
        var db = HITStorage.indexedDB.db;
        var trans = db.transaction(["HIT"], HITStorage.IDBTransactionModes.READ_WRITE);
        var store = trans.objectStore("HIT");

        store.index('date').openCursor(range2).onsuccess = function(event) {
            var cursor = event.target.result;
            if (cursor)
            {
                if (cursor.value.hitId.indexOf("?") > -1)
                {
                    console.log(cursor.value);
                    dates.push(cursor.value.date);
                    //cursor.delete();
                }
                cursor.continue();
            }
            else
            {
                db.close();
            }
        };
    }
    console.log(dates);
    console.log(dates2);
    request = indexedDB.open("HITDB", 4);
    request.onsuccess = function(e) {
        HITStorage.indexedDB.db = e.target.result;
        var db = HITStorage.indexedDB.db;
        var trans = db.transaction(["HIT"], HITStorage.IDBTransactionModes.READ_WRITE);
        var store = trans.objectStore("HIT");
        trans = db.transaction(["STATS"], HITStorage.IDBTransactionModes.READ_WRITE);
        store = trans.objectStore("STATS");

        console.log("Second");
        store.openCursor(range).onsuccess = function(event) {
            var cursor = event.target.result;
            if (cursor)
            {
                if (dates.contains(cursor.value.date))
                {
                    console.log(cursor.value);
                    //cursor.delete();
                }
                cursor.continue();
            }
            else
            {
                db.close();
            }
        };
    }
    request = indexedDB.open("HITDB", 4);
    request.onsuccess = function(e) {
        HITStorage.indexedDB.db = e.target.result;
        var db = HITStorage.indexedDB.db;
        var trans = db.transaction(["HIT"], HITStorage.IDBTransactionModes.READ_WRITE);
        var store = trans.objectStore("HIT");
        store.openCursor(range2).onsuccess = function(event) {
            var cursor = event.target.result;
            if (cursor)
            {
                if (dates2.contains(cursor.value.date))
                {
                    console.log(cursor.value);
                    //cursor.delete();
                }
                cursor.continue();
            }
            else
            {
                db.close();
                alert("Done");
            }
        };
    }
}