Harpocrates

Helps the Olympian scripts when they need GreaseMonkey functions

当前为 2016-09-14 提交的版本,查看 最新版本

// ==UserScript==
// @name         Harpocrates
// @namespace    mobiusevalon.tibbius.com
// @version      0.3
// @author       Mobius Evalon
// @description  Helps the Olympian scripts when they need GreaseMonkey functions
// @license      Creative Commons Attribution-ShareAlike 4.0; http://creativecommons.org/licenses/by-sa/4.0/
// @include      /^https{0,1}:\/\/\w{0,}\.?mturk\.com.+/
// @include      /^https{0,1}:\/\/\w*\.amazon\.com\/ap\/signin.*(?:openid\.assoc_handle|pf_rd_i)=amzn_mturk/
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

// use of unsafeWindow is necessary to get harpocrates into the un-sandboxed
// space with the rest of the olympian scripts
if(unsafeWindow.olympus === undefined) unsafeWindow.olympus = {};

unsafeWindow.olympus.harpocrates = {
	__12_hrs:43200000,
	login_timestamp:0,
	__init:function() {
		console.log("harpocrates init");
		if(/^https{0,1}:\/\/\w{0,}\.amazon\.com\/ap\/signin/i.test(window.location.href)) {
			$("#signInSubmit-input").click(function() {
				GM_setValue("login_timestamp",Date.now());
			});
		}
		else {
			olympus.style.add(
				".red {color: #f00;}"+
				".logout_time {margin-right: 4px;}"
			);
			this.login_timestamp = (GM_getValue("login_timestamp",0)*1);
			$("span.header_links").append(
				$("<br/>"),
				$("<b/>")
					.attr({
						"id":"harpocrates_logout_time",
						"title":("You logged into Mechanical Turk on "+olympus.utilities.datetime.output(this.login_timestamp,"d M \\a\\t H:i")+".\n\n"+
								 "The cookie for Mechanical Turk expires after 12 hours, which means you will be required to login again when this timer reaches zero.")
					})
			);
			this.update();
		}
	},
	update:function() {
		if(olympus.harpocrates.login_timestamp > 0) {
			var time_left = ((olympus.harpocrates.__12_hrs-(Date.now()-olympus.harpocrates.login_timestamp))/1000);
			$("#harpocrates_logout_time")
				.attr("class",((time_left <= 3600 ? " red" : "")+(time_left <= 1800 ? " anim_pulse" : "")))
				.text("Time to logout: "+(time_left <= 300 ? "any moment" : olympus.utilities.dhms(time_left)));
			setTimeout(olympus.harpocrates.update,60000);
		}
		else $("#harpocrates_logout_time")
			.removeClass("red anim_pulse")
			.attr("title","")
			.text("");
	}
};