Underdollar jQuery replacement

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

Version au 11/01/2018. Voir la dernière version.

Ce script ne doit pas être installé directement. C'est une librairie destinée à être incluse dans d'autres scripts avec la méta-directive // @require https://update.greasyfork.org/scripts/37273/242598/Underdollar%20jQuery%20replacement.js

Vous devrez installer une extension telle que Tampermonkey, Greasemonkey ou Violentmonkey pour installer ce script.

Vous devrez installer une extension telle que Tampermonkey pour installer ce script.

Vous devrez installer une extension telle que Tampermonkey ou Violentmonkey pour installer ce script.

Vous devrez installer une extension telle que Tampermonkey ou Userscripts pour installer ce script.

Vous devrez installer une extension telle que Tampermonkey pour installer ce script.

Vous devrez installer une extension de gestionnaire de script utilisateur pour installer ce script.

(J'ai déjà un gestionnaire de scripts utilisateur, laissez-moi l'installer !)

Vous devrez installer une extension telle que Stylus pour installer ce style.

Vous devrez installer une extension telle que Stylus pour installer ce style.

Vous devrez installer une extension telle que Stylus pour installer ce style.

Vous devrez installer une extension du gestionnaire de style pour utilisateur pour installer ce style.

Vous devrez installer une extension du gestionnaire de style pour utilisateur pour installer ce style.

Vous devrez installer une extension du gestionnaire de style pour utilisateur pour installer ce style.

(J'ai déjà un gestionnaire de style utilisateur, laissez-moi l'installer!)

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