Tampermonkey Support Library

to reduce repetition in creating scripts for Tampermonkey support

2016-09-10 기준 버전입니다. 최신 버전을 확인하세요.

이 스크립트는 직접 설치하는 용도가 아닙니다. 다른 스크립트에서 메타 지시문 // @require https://update.greasyfork.org/scripts/23115/146830/Tampermonkey%20Support%20Library.js을(를) 사용하여 포함하는 라이브러리입니다.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램을 설치해야 합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

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);
	}
};