Underdollar jQuery replacement

Replaces jQuery, which causes lots of conflicts, with a framework that is largely cross-compatible

目前為 2018-01-11 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/37273/242598/Underdollar%20jQuery%20replacement.js

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Underdollar jQuery replacement
// @namespace   https://greasyfork.org
// @include     https://sellers.shopgoodwill.com/sellers/newAuctionItem-catsel.asp*
// @include     https://sellers.shopgoodwill.com/sellers/reviewItem-label.asp*
// @include     https://sellers.shopgoodwill.com/sellers/reviewItem-label.asp?state=2
// @version     1.0.0
// @description Replaces jQuery, which causes lots of conflicts, with a framework that is largely cross-compatible
// @grant       none
// ==/UserScript==

class underdollar {
    constructor(selector) {
		this.is_$ = true;
		var singleNode = (function () {
			// make an empty node list to inherit from
			var nodelist = document.createDocumentFragment().childNodes;
			// return a function to create object formed as desired
			return function (node) {
				return Object.create(nodelist, {
					'0': {value: node, enumerable: true},
					'length': {value: 1},
					'item': {
						"value": function (i) {
							return this[+i || 0];
						}, 
						enumerable: true
					}
				}); // return an object pretending to be a NodeList
			};
		}());	
	
		if (arguments.length < 1 || typeof selector == 'undefined') {
			this.nodeList = 'empty';
		} else {
			if (selector instanceof Element) {
				this.nodeList = singleNode(selector);
			} else if (selector instanceof NodeList || selector instanceof HTMLCollection) {
				this.nodeList = selector;
			} else {
				console.dir(selector);
				this.nodeList = document.querySelectorAll(selector);
			}
			this.selector = selector;
		}
		Array.prototype.forEach.call(document.querySelectorAll('.udTempClassSelector'), function(el) {
			el.classList.remove('.udTempClassSelector');
		});
		
		this.length = this.nodeList.length;
		
	}
	
	each(arg1, arg2){
		if (arg1 instanceof Object && arg2 instanceof Function) {
				for (var p in arg1) {
					if (arg1.hasOwnProperty(p)) {
						arg2(p, arg1[p]);
					}
			}
		} else if (this.nodeList instanceof Array || this.nodeList instanceof NodeList) {
			Array.prototype.forEach.call(this.nodeList, arg1);
		}
		
		return _$(this.selector);
	}
	
// selection functions
	
	parent() {
		if (this.nodeList instanceof NodeList) {
			var myParent = this.nodeList[0].parentNode;
			return _$(myParent);
		}
	}
	
	children() {
		return _$(this.nodeList[0].children);
	}
	
	first() {
		if (this.nodeList instanceof NodeList) {
			return _$(this.nodeList[0]);
		} else {
			return _$(this.selector);
		}
	}	
	
	last() {
		if (this.nodeList instanceof NodeList) {
			var length = this.nodeList.length;
			return _$(this.nodeList[length-1]);
		} else {
			return _$(this.selector);
		}
	}
	
	contents() {
//		var frame = document.getElementById('myframe');
//		var c = frame.contentDocument || frame.contentWindow.document;
		return this.nodeList[0].contentDocument || this.nodeList[0].contentWindow.document;
	}
	
	
	
// display functions

	css(arg1, arg2){
		var me = this;
		if (typeof arg1 == 'string' && typeof arg2 == 'string') {
			Array.prototype.forEach.call(this.nodeList, function(el){
				el.style[arg1] = arg2;
			});
		} else if (arg1 instanceof Object) {
			Array.prototype.forEach.call(this.nodeList, function(el) {
				_$().each(arg1, function(styleName, styleValue) {
					el.style[styleName] = styleValue;
				});
			});
		}
		
		return _$(this.selector);
	}	
	
	hide() {
		this.css('display', 'none');
		return _$(this.selector);
	}
	
	show() {
		this.css('display', '');
		return _$(this.selector);
	}
	
	toggle() {
		if (this.nodeList instanceof NodeList) {
			this.each(function(el){
				if (el.style.display == 'none') {
					el.style.display = '';
				} else {
					el.style.display = 'none';
				}
			});
		}
		return _$(this.selector);
	}

// DOM functions

	before(htmlString) {
		this.each(function(el){
			el.insertAdjacentHTML('beforebegin', htmlString);
		});
		return _$(this.selector);
	}
	
	after(htmlString) {
		this.each(function(el){
			el.insertAdjacentHTML('afterend', htmlString);
		});
		return _$(this.selector);
	}
	
	append(something) {
		if (something instanceof Element) {
			this.each(function(el) {
				// how to function with appending one existing thing when there are multiple things to append to??
				el.appendChild(something);
				
			});
		} else if (something instanceof NodeList) {
			Array.prototype.forEach.call(something, function(el) {
				this.append(something);
			});
		} else if (typeof something == 'string') {
			this.each(function(el) {
				var newEl = document.createElement('text');
				newEl.innerHTML = something;
				el.appendChild(newEl)
			});
		} else if (something.hasOwnProperty('is_$')) {
			if (something.nodeList.length == 1) {
				this.each(function(el) {
					// how to function with appending one existing thing when there are multiple things to append to??
					el.appendChild(something.nodeList[0]);
				});
			} else if (something.nodeList.length > 1) {
				this.each(function(el) {
					// how to function with appending one existing thing when there are multiple things to append to??
					//el.appendChild(something.nodeList[0]);
					Array.prototype.forEach.call(something.nodeList, function(myNode, nodeIndex) {
						el.appendChild(myNode);
					});
				});
			}
		}
		return _$(this.selector);
	}

	prepend(something) {
		if (something instanceof Element) {
			this.each(function(el) {
				// how to function with appending one existing thing when there are multiple things to append to??
				el.insertBefore(something, el.firstChild);
				
			});
		} else if (something instanceof NodeList) {
			Array.prototype.forEach.call(something, function(el) {
				this.append(something, el.firstChild);
			});
		} else if (typeof something == 'string') {
			this.each(function(el) {
				var newEl = document.createElement('text');
				newEl.innerHTML = something;
				el.insertBefore(newEl, el.firstChild)
			});
		} else if (something.hasOwnProperty('is_$')) {
			if (something.nodeList.length == 1) {
				this.each(function(el) {
					// how to function with appending one existing thing when there are multiple things to append to??
					el.insertBefore(something.nodeList[0], el.firstChild);
				});
			} else if (something.nodeList.length > 1) {
				this.each(function(el) {
					// how to function with appending one existing thing when there are multiple things to append to??
					Array.prototype.forEach.call(something.nodeList, function(myNode, nodeIndex) {
						el.insertBefore(myNode, el.firstChild);
					});
				});
			}
		}
		return _$(this.selector);
	}
	
	remove() {
		this.each(function(el) {
			// how to function with appending one existing thing when there are multiple things to append to??
			el.parentNode.removeChild(el);
		});
	}

// other element property/content functions

	attr(arg1, arg2) {
		if (arguments.length < 2) {
			return _$(this.selector).first().getAttribute(arg1);
		} else {
			this.each(function(el) {
				el.setAttribute(arg1, arg2);
			});
			return _$(this.selector);
		}
	}
	
	text(textString) {
		if (arguments.length < 1) {
			var myVal = '';
			this.each(function(el){
				myVal += el.textContent;
			});
			return myVal;
		} else {
			this.each(function(el){
				el.textContent = textString;
			});
			return _$(this.selector);
		}
	}
	
	html(arg1) {
		if (arguments.length < 1) {
			var myVal = '';
			this.each(function(el){
				myVal += el.innerHTML;
			});
			return myVal;
		} else {
			this.each(function(el){
				el.innerHTML = arg1;
			});
			return _$(this.selector);
		}
	}
	
	val(arg1) {
		if (arguments.length < 1) {
			if (this.length > 1) {
				var myVals = [];
				this.each(function(el){
					myVals.push(el.value)
				});
				return myVals;
			} else if (this.length == 1) {
				return this.nodeList[0].value;
			}
		} else {
			this.each(function(el){
				el.value = arg1;
			});
			return _$(this.selector);
		}
	}
	
// function functions
	
	bind(eventName, fn) {
		Array.prototype.forEach.call(this.nodeList, function(el) {
			el.addEventListener(eventName, fn);
		});
		
	}
	
// utility functions	
	
	iterateOverObject(obj, fn) {
		if (obj instanceof Object) {
			for (var p in obj) {
				if (obj.hasOwnProperty(p)) {
					fn(p, obj[p]);
				}
			}
		}
		
		return _$(this.selector);
	}
	
}

function _$(selector) {
	return new underdollar(selector);
}