Underdollar jQuery replacement

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

目前为 2018-01-11 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/37273/242618/Underdollar%20jQuery%20replacement.js

  1. // ==UserScript==
  2. // @name Underdollar jQuery replacement
  3. // @namespace https://greasyfork.org
  4. // @include https://sellers.shopgoodwill.com/sellers/newAuctionItem-catsel.asp*
  5. // @include https://sellers.shopgoodwill.com/sellers/reviewItem-label.asp*
  6. // @include https://sellers.shopgoodwill.com/sellers/reviewItem-label.asp?state=2
  7. // @version 1.0.0.2
  8. // @description Replaces jQuery, which causes lots of conflicts, with a framework that is largely cross-compatible
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. console.log('squirrel');
  13.  
  14. class underdollar {
  15. constructor(selector) {
  16. this.is_$ = true;
  17.  
  18. var singleNode = (function () {
  19. // make an empty node list to inherit from
  20. var nodelist = document.createDocumentFragment().childNodes;
  21. // return a function to create object formed as desired
  22. return function (node) {
  23. return Object.create(nodelist, {
  24. '0': {value: node, enumerable: true},
  25. 'length': {value: 1},
  26. 'item': {
  27. "value": function (i) {
  28. return this[+i || 0];
  29. },
  30. enumerable: true
  31. }
  32. }); // return an object pretending to be a NodeList
  33. };
  34. }());
  35.  
  36. if (arguments.length < 1 || typeof selector == 'undefined') {
  37. this.nodeList = 'empty';
  38. } else {
  39. if (selector instanceof Element) {
  40. this.nodeList = singleNode(selector);
  41. } else if (selector instanceof NodeList || selector instanceof HTMLCollection) {
  42. this.nodeList = selector;
  43. } else {
  44. console.dir(selector);
  45. this.nodeList = document.querySelectorAll(selector);
  46. }
  47. this.selector = selector;
  48. }
  49. Array.prototype.forEach.call(document.querySelectorAll('.udTempClassSelector'), function(el) {
  50. el.classList.remove('.udTempClassSelector');
  51. });
  52.  
  53. this.length = this.nodeList.length;
  54.  
  55. }
  56.  
  57. // selection functions
  58.  
  59. parent() {
  60. if (this.nodeList instanceof NodeList) {
  61. var myParent = this.nodeList[0].parentNode;
  62. return _$(myParent);
  63. }
  64. }
  65.  
  66. children() {
  67. return _$(this.nodeList[0].children);
  68. }
  69.  
  70. first() {
  71. if (this.nodeList instanceof NodeList) {
  72. return _$(this.nodeList[0]);
  73. } else {
  74. return _$(this.selector);
  75. }
  76. }
  77.  
  78. last() {
  79. if (this.nodeList instanceof NodeList) {
  80. var length = this.nodeList.length;
  81. return _$(this.nodeList[length-1]);
  82. } else {
  83. return _$(this.selector);
  84. }
  85. }
  86.  
  87. contents() {
  88. // var frame = document.getElementById('myframe');
  89. // var c = frame.contentDocument || frame.contentWindow.document;
  90. return this.nodeList[0].contentDocument || this.nodeList[0].contentWindow.document;
  91. }
  92.  
  93.  
  94.  
  95. // display functions
  96.  
  97. css(arg1, arg2){
  98. var me = this;
  99. if (typeof arg1 == 'string' && typeof arg2 == 'string') {
  100. Array.prototype.forEach.call(this.nodeList, function(el){
  101. el.style[arg1] = arg2;
  102. });
  103. } else if (arg1 instanceof Object) {
  104. Array.prototype.forEach.call(this.nodeList, function(el) {
  105. _$().each(arg1, function(styleName, styleValue) {
  106. el.style[styleName] = styleValue;
  107. });
  108. });
  109. }
  110.  
  111. return _$(this.selector);
  112. }
  113.  
  114. hide() {
  115. this.css('display', 'none');
  116. return _$(this.selector);
  117. }
  118.  
  119. show() {
  120. this.css('display', '');
  121. return _$(this.selector);
  122. }
  123.  
  124. toggle() {
  125. if (this.nodeList instanceof NodeList) {
  126. this.each(function(el){
  127. if (el.style.display == 'none') {
  128. el.style.display = '';
  129. } else {
  130. el.style.display = 'none';
  131. }
  132. });
  133. }
  134. return _$(this.selector);
  135. }
  136.  
  137. // DOM functions
  138.  
  139. before(htmlString) {
  140. this.each(function(el){
  141. el.insertAdjacentHTML('beforebegin', htmlString);
  142. });
  143. return _$(this.selector);
  144. }
  145.  
  146. after(htmlString) {
  147. this.each(function(el){
  148. el.insertAdjacentHTML('afterend', htmlString);
  149. });
  150. return _$(this.selector);
  151. }
  152.  
  153. append(something) {
  154. if (something instanceof Element) {
  155. this.each(function(el) {
  156. // how to function with appending one existing thing when there are multiple things to append to??
  157. el.appendChild(something);
  158.  
  159. });
  160. } else if (something instanceof NodeList) {
  161. Array.prototype.forEach.call(something, function(el) {
  162. this.append(something);
  163. });
  164. } else if (typeof something == 'string') {
  165. this.each(function(el) {
  166. var newEl = document.createElement('text');
  167. newEl.innerHTML = something;
  168. el.appendChild(newEl)
  169. });
  170. } else if (something.hasOwnProperty('is_$')) {
  171. if (something.nodeList.length == 1) {
  172. this.each(function(el) {
  173. // how to function with appending one existing thing when there are multiple things to append to??
  174. el.appendChild(something.nodeList[0]);
  175. });
  176. } else if (something.nodeList.length > 1) {
  177. this.each(function(el) {
  178. // how to function with appending one existing thing when there are multiple things to append to??
  179. //el.appendChild(something.nodeList[0]);
  180. Array.prototype.forEach.call(something.nodeList, function(myNode, nodeIndex) {
  181. el.appendChild(myNode);
  182. });
  183. });
  184. }
  185. }
  186. return _$(this.selector);
  187. }
  188.  
  189. prepend(something) {
  190. if (something instanceof Element) {
  191. this.each(function(el) {
  192. // how to function with appending one existing thing when there are multiple things to append to??
  193. el.insertBefore(something, el.firstChild);
  194.  
  195. });
  196. } else if (something instanceof NodeList) {
  197. Array.prototype.forEach.call(something, function(el) {
  198. this.append(something, el.firstChild);
  199. });
  200. } else if (typeof something == 'string') {
  201. this.each(function(el) {
  202. var newEl = document.createElement('text');
  203. newEl.innerHTML = something;
  204. el.insertBefore(newEl, el.firstChild)
  205. });
  206. } else if (something.hasOwnProperty('is_$')) {
  207. if (something.nodeList.length == 1) {
  208. this.each(function(el) {
  209. // how to function with appending one existing thing when there are multiple things to append to??
  210. el.insertBefore(something.nodeList[0], el.firstChild);
  211. });
  212. } else if (something.nodeList.length > 1) {
  213. this.each(function(el) {
  214. // how to function with appending one existing thing when there are multiple things to append to??
  215. Array.prototype.forEach.call(something.nodeList, function(myNode, nodeIndex) {
  216. el.insertBefore(myNode, el.firstChild);
  217. });
  218. });
  219. }
  220. }
  221. return _$(this.selector);
  222. }
  223.  
  224. remove() {
  225. this.each(function(el) {
  226. // how to function with appending one existing thing when there are multiple things to append to??
  227. el.parentNode.removeChild(el);
  228. });
  229. }
  230.  
  231. // other element property/content functions
  232.  
  233. attr(arg1, arg2) {
  234. if (arguments.length < 2) {
  235. return _$(this.selector).first().getAttribute(arg1);
  236. } else {
  237. this.each(function(el) {
  238. el.setAttribute(arg1, arg2);
  239. });
  240. return _$(this.selector);
  241. }
  242. }
  243.  
  244. text(textString) {
  245. if (arguments.length < 1) {
  246. var myVal = '';
  247. this.each(function(el){
  248. myVal += el.textContent;
  249. });
  250. return myVal;
  251. } else {
  252. this.each(function(el){
  253. el.textContent = textString;
  254. });
  255. return _$(this.selector);
  256. }
  257. }
  258.  
  259. html(arg1) {
  260. if (arguments.length < 1) {
  261. var myVal = '';
  262. this.each(function(el){
  263. myVal += el.innerHTML;
  264. });
  265. return myVal;
  266. } else {
  267. this.each(function(el){
  268. el.innerHTML = arg1;
  269. });
  270. return _$(this.selector);
  271. }
  272. }
  273.  
  274. val(arg1) {
  275. if (arguments.length < 1) {
  276. if (this.length > 1) {
  277. var myVals = [];
  278. this.each(function(el){
  279. myVals.push(el.value)
  280. });
  281. return myVals;
  282. } else if (this.length == 1) {
  283. return this.nodeList[0].value;
  284. }
  285. } else {
  286. this.each(function(el){
  287. el.value = arg1;
  288. });
  289. return _$(this.selector);
  290. }
  291. }
  292.  
  293. isVisible() {
  294. if (this.length == 1) {
  295. return this.nodeList[0].offsetWidth !== 0 && this.nodeList[0].offsetHeight !== 0;
  296. } else if (this.length == 1) {
  297. var numVisible = 0;
  298. this.each(function(el){
  299. if (!(el.offsetWidth !== 0) || !(el.offsetHeight !== 0)) {
  300. numVisible++;
  301. }
  302. });
  303. return numVisible;
  304. }
  305. }
  306.  
  307.  
  308.  
  309. // function functions
  310.  
  311. bind(eventName, fn) {
  312. Array.prototype.forEach.call(this.nodeList, function(el) {
  313. el.addEventListener(eventName, fn);
  314. });
  315.  
  316. }
  317.  
  318. // utility functions
  319.  
  320. filter(fn) {
  321. /* this.each(function(el){
  322. if (fn(el)) {
  323.  
  324. }
  325. });*/
  326. return Array.prototype.filter.call(this.nodeList, fn);
  327. }
  328.  
  329. each(arg1, arg2){
  330. if (arg1 instanceof Object && arg2 instanceof Function) {
  331. for (var p in arg1) {
  332. if (arg1.hasOwnProperty(p)) {
  333. arg2(p, arg1[p]);
  334. }
  335. }
  336. } else if (this.nodeList instanceof Array || this.nodeList instanceof NodeList) {
  337. Array.prototype.forEach.call(this.nodeList, arg1);
  338. }
  339.  
  340. return _$(this.selector);
  341. }
  342.  
  343. iterateOverObject(obj, fn) {
  344. if (obj instanceof Object) {
  345. for (var p in obj) {
  346. if (obj.hasOwnProperty(p)) {
  347. fn(p, obj[p]);
  348. }
  349. }
  350. }
  351.  
  352. return _$(this.selector);
  353. }
  354.  
  355. }
  356.  
  357. function _$(selector) {
  358. return new underdollar(selector);
  359. }