Tampermonkey Support Library

to reduce repetition in creating scripts for Tampermonkey support

ของเมื่อวันที่ 04-11-2016 ดู เวอร์ชันล่าสุด

สคริปต์นี้ไม่ควรถูกติดตั้งโดยตรง มันเป็นคลังสำหรับสคริปต์อื่น ๆ เพื่อบรรจุด้วยคำสั่งเมทา // @require https://update.greasyfork.org/scripts/23115/156075/Tampermonkey%20Support%20Library.js

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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!)

var tm = {
	addGlobalStyle: function(css) {
	    var head, style;
	    head = document.getElementsByTagName('head')[0];
	    if (!head) { return; }
	    style = document.createElement('style');
	    style.type = 'text/css';
	    style.innerHTML = css;
	    head.appendChild(style);
	},
	log: function(msg) {
		console.log('Tampermonkey: ' + msg);
	},
	selectText: function (targetClass) {
		var textToCopy, range;
		try {
			if (document.selection) {
				range = document.body.createTextRange();
				range.moveToElementText(document.getElementsByClassName(targetClass)[0]);
				range.select();
			} else if (window.getSelection) {
				range = document.createRange();
				range.selectNode(document.getElementsByClassName(targetClass)[0]);
				window.getSelection().addRange(range);
			}
		} catch (err) {
			tm.log('Failed to select text');
		}
	},
	sysFriendly: function(el) {
	    var text = $(el).text();
		$(el).text(text.replace(/\/|\:|\<|\>|\\|\||\*|\?/g, '-'));
	},
	ping: function (ip, callback) { // via http://jsfiddle.net/GSSCD/203/
		if (!this.inUse) {
			this.status = 'unchecked';
			this.inUse = true;
			this.callback = callback;
			this.ip = ip;
			var _that = this;
			this.img = new Image();

			this.img.onload = function () {
				_that.inUse = false;
				_that.callback('responded');
			};

			this.img.onerror = function (e) {
				if (_that.inUse) {
					_that.inUse = false;
					_that.callback('error', e);
				}
			};
			
			this.start = new Date().getTime();
			this.img.src = "http://" + ip;
			this.timer = setTimeout(function () {
				if (_that.inUse) {
					_that.inUse = false;
					_that.callback('timeout');
				}
			}, 1500);
		}
	},
	isTagIn: function (targetString, tags) {
		var isFound = false;
		_.each(tags, function scanTarget (tag) {
			if (targetString.toLowerCase().indexOf(tag.toLowerCase()) > -1) {
				isFound = true;
			}
		});
		return isFound;
	},
	copyTextToClipboard: function (text) {
		var copyFrom = document.createElement("textarea");
		copyFrom.textContent = text;
		var body = document.getElementsByTagName('body')[0];
		body.appendChild(copyFrom);
		copyFrom.select();
		document.execCommand('copy');
		body.removeChild(copyFrom);
	},
	waitFor = function (awaitThis, doThis, intervalSpeed = 100, maxTries = 100) {
		var currentTry = 0,
		stateLoading = function (awaitCondition) {
		  console.log(awaitCondition + ' | goober length = ' + $('.goober').length);
			if( !awaitCondition ) {
				console.log('wait');
				currentTry++;
				if (currentTry < maxTries) {
				  window.setTimeout(stateLoading(awaitCondition), intervalSpeed); /* this checks the flag every intervalSpeed milliseconds*/
				} else {
				  console.log('final try');
				}
			} else {
				console.log('good');
				doThis();
			}
		};
		stateLoading(awaitThis);
	}
};