HIT Database Backup

Does things...

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         HIT Database Backup
// @namespace    https://github.com/Kadauchi
// @version      1.0.2
// @description  Does things...
// @author       Kadauchi
// @icon         http://i.imgur.com/oGRQwPN.png
// @include      https://www.mturk.com/hitdb
// ==/UserScript==

document.body.innerHTML = `creating hit_database.json.... please wait`;

let hitdb;

const request = window.indexedDB.open(`HITDB`);

request.onsuccess = (event) => {
	hitdb = event.target.result;
	generateFile();
};
request.onupgradeneeded = (event) => {
	document.body.innerHTML = `no hit database found`;
};
request.onerror = (event) => {
	document.body.innerHTML = `error: something went wrong`;
};

async function generateFile() {
	const data = JSON.stringify({
		HIT: await getObjectStore(`HIT`),
		STATS: await getObjectStore(`STATS`),
		NOTES: await getObjectStore(`NOTES`)
	});

	document.body.innerHTML = `creating hit_database.json.... this may take awhile`;

	const exportFile = document.createElement(`a`);
        document.body.appendChild(exportFile);
	exportFile.href = window.URL.createObjectURL(new Blob([data], { type: `application/json` }));
	exportFile.download = `hit_database.json`;
	exportFile.click();

	document.body.innerHTML = `hit_database.json.... downloaded`;
}

function getObjectStore(name) {
	return new Promise((resolve) => {
		try {
			const transaction = hitdb.transaction([name], `readonly`);
			const objectStore = transaction.objectStore(name);

			let cursorCount = 0, cursorAccumulator = [];

			objectStore.openCursor().onsuccess = (event) => {
				document.body.innerHTML = `processing hit database.... ${name} ${++ cursorCount}`;

				const cursor = event.target.result;

				if (cursor) {
					cursorAccumulator.push(cursor.value);
					cursor.continue();
				}
				else {
					resolve(cursorAccumulator);
				}
			};
		}
		catch (error) {
			resolve();
		}
	});
}