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