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/242623/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.1.0
  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. this.nodeList = document.querySelectorAll(selector);
  45. }
  46. this.selector = selector;
  47. }
  48. Array.prototype.forEach.call(document.querySelectorAll('.udTempClassSelector'), function(el) {
  49. el.classList.remove('.udTempClassSelector');
  50. });
  51.  
  52. this.length = this.nodeList.length;
  53.  
  54. }
  55.  
  56. // selection functions
  57.  
  58. parent() {
  59. if (this.nodeList instanceof NodeList) {
  60. var myParent = this.nodeList[0].parentNode;
  61. return _$(myParent);
  62. }
  63. }
  64.  
  65. children() {
  66. return _$(this.nodeList[0].children);
  67. }
  68.  
  69. first() {
  70. if (this.nodeList instanceof NodeList) {
  71. return _$(this.nodeList[0]);
  72. } else {
  73. return _$(this.selector);
  74. }
  75. }
  76.  
  77. last() {
  78. if (this.nodeList instanceof NodeList) {
  79. var length = this.nodeList.length;
  80. return _$(this.nodeList[length-1]);
  81. } else {
  82. return _$(this.selector);
  83. }
  84. }
  85.  
  86. contents() {
  87. // var frame = document.getElementById('myframe');
  88. // var c = frame.contentDocument || frame.contentWindow.document;
  89. return this.nodeList[0].contentDocument || this.nodeList[0].contentWindow.document;
  90. }
  91.  
  92.  
  93.  
  94. // display functions
  95.  
  96. css(arg1, arg2){
  97. var me = this;
  98. if (typeof arg1 == 'string' && typeof arg2 == 'string') {
  99. Array.prototype.forEach.call(this.nodeList, function(el){
  100. el.style[arg1] = arg2;
  101. });
  102. } else if (arg1 instanceof Object) {
  103. Array.prototype.forEach.call(this.nodeList, function(el) {
  104. _$().each(arg1, function(styleName, styleValue) {
  105. el.style[styleName] = styleValue;
  106. });
  107. });
  108. }
  109.  
  110. return _$(this.selector);
  111. }
  112.  
  113. hide() {
  114. this.css('display', 'none');
  115. return _$(this.selector);
  116. }
  117.  
  118. show() {
  119. this.css('display', '');
  120. return _$(this.selector);
  121. }
  122.  
  123. toggle() {
  124. if (this.nodeList instanceof NodeList) {
  125. this.each(function(el){
  126. if (el.style.display == 'none') {
  127. el.style.display = '';
  128. } else {
  129. el.style.display = 'none';
  130. }
  131. });
  132. }
  133. return _$(this.selector);
  134. }
  135.  
  136. // DOM functions
  137.  
  138. before(htmlString) {
  139. this.each(function(el){
  140. el.insertAdjacentHTML('beforebegin', htmlString);
  141. });
  142. return _$(this.selector);
  143. }
  144.  
  145. after(htmlString) {
  146. this.each(function(el){
  147. el.insertAdjacentHTML('afterend', htmlString);
  148. });
  149. return _$(this.selector);
  150. }
  151.  
  152. append(something) {
  153. if (something instanceof Element) {
  154. this.each(function(el) {
  155. // how to function with appending one existing thing when there are multiple things to append to??
  156. el.appendChild(something);
  157.  
  158. });
  159. } else if (something instanceof NodeList) {
  160. Array.prototype.forEach.call(something, function(el) {
  161. this.append(something);
  162. });
  163. } else if (typeof something == 'string') {
  164. this.each(function(el) {
  165. var newEl = document.createElement('text');
  166. newEl.innerHTML = something;
  167. el.appendChild(newEl)
  168. });
  169. } else if (something.hasOwnProperty('is_$')) {
  170. if (something.nodeList.length == 1) {
  171. this.each(function(el) {
  172. // how to function with appending one existing thing when there are multiple things to append to??
  173. el.appendChild(something.nodeList[0]);
  174. });
  175. } else if (something.nodeList.length > 1) {
  176. this.each(function(el) {
  177. // how to function with appending one existing thing when there are multiple things to append to??
  178. //el.appendChild(something.nodeList[0]);
  179. Array.prototype.forEach.call(something.nodeList, function(myNode, nodeIndex) {
  180. el.appendChild(myNode);
  181. });
  182. });
  183. }
  184. }
  185. return _$(this.selector);
  186. }
  187.  
  188. prepend(something) {
  189. if (something instanceof Element) {
  190. this.each(function(el) {
  191. // how to function with appending one existing thing when there are multiple things to append to??
  192. el.insertBefore(something, el.firstChild);
  193.  
  194. });
  195. } else if (something instanceof NodeList) {
  196. Array.prototype.forEach.call(something, function(el) {
  197. this.append(something, el.firstChild);
  198. });
  199. } else if (typeof something == 'string') {
  200. this.each(function(el) {
  201. var newEl = document.createElement('text');
  202. newEl.innerHTML = something;
  203. el.insertBefore(newEl, el.firstChild)
  204. });
  205. } else if (something.hasOwnProperty('is_$')) {
  206. if (something.nodeList.length == 1) {
  207. this.each(function(el) {
  208. // how to function with appending one existing thing when there are multiple things to append to??
  209. el.insertBefore(something.nodeList[0], el.firstChild);
  210. });
  211. } else if (something.nodeList.length > 1) {
  212. this.each(function(el) {
  213. // how to function with appending one existing thing when there are multiple things to append to??
  214. Array.prototype.forEach.call(something.nodeList, function(myNode, nodeIndex) {
  215. el.insertBefore(myNode, el.firstChild);
  216. });
  217. });
  218. }
  219. }
  220. return _$(this.selector);
  221. }
  222.  
  223. remove() {
  224. this.each(function(el) {
  225. // how to function with appending one existing thing when there are multiple things to append to??
  226. el.parentNode.removeChild(el);
  227. });
  228. }
  229.  
  230. // other element property/content functions
  231.  
  232. attr(arg1, arg2) {
  233. if (arguments.length < 2) {
  234. return _$(this.selector).first().getAttribute(arg1);
  235. } else {
  236. this.each(function(el) {
  237. el.setAttribute(arg1, arg2);
  238. });
  239. return _$(this.selector);
  240. }
  241. }
  242.  
  243. text(textString) {
  244. if (arguments.length < 1) {
  245. var myVal = '';
  246. this.each(function(el){
  247. myVal += el.textContent;
  248. });
  249. return myVal;
  250. } else {
  251. this.each(function(el){
  252. el.textContent = textString;
  253. });
  254. return _$(this.selector);
  255. }
  256. }
  257.  
  258. html(arg1) {
  259. if (arguments.length < 1) {
  260. var myVal = '';
  261. this.each(function(el){
  262. myVal += el.innerHTML;
  263. });
  264. return myVal;
  265. } else {
  266. this.each(function(el){
  267. el.innerHTML = arg1;
  268. });
  269. return _$(this.selector);
  270. }
  271. }
  272.  
  273. val(arg1) {
  274. if (arguments.length < 1) {
  275. if (this.length > 1) {
  276. var myVals = [];
  277. this.each(function(el){
  278. myVals.push(el.value)
  279. });
  280. return myVals;
  281. } else if (this.length == 1) {
  282. return this.nodeList[0].value;
  283. }
  284. } else {
  285. this.each(function(el){
  286. el.value = arg1;
  287. });
  288. return _$(this.selector);
  289. }
  290. }
  291.  
  292. isVisible() {
  293. if (this.length == 1) {
  294. return this.nodeList[0].offsetWidth !== 0 && this.nodeList[0].offsetHeight !== 0;
  295. } else if (this.length == 1) {
  296. var numVisible = 0;
  297. this.each(function(el){
  298. if (!(el.offsetWidth !== 0) || !(el.offsetHeight !== 0)) {
  299. numVisible++;
  300. }
  301. });
  302. return numVisible;
  303. }
  304. }
  305.  
  306.  
  307.  
  308. // function functions
  309.  
  310. bind(eventName, fn) {
  311. Array.prototype.forEach.call(this.nodeList, function(el) {
  312. el.addEventListener(eventName, fn);
  313. });
  314.  
  315. }
  316.  
  317. // utility functions
  318.  
  319. filter(fn) {
  320. /* this.each(function(el){
  321. if (fn(el)) {
  322.  
  323. }
  324. });*/
  325. return Array.prototype.filter.call(this.nodeList, fn);
  326. }
  327.  
  328. each(arg1, arg2){
  329. if (arg1 instanceof Object && arg2 instanceof Function) {
  330. for (var p in arg1) {
  331. if (arg1.hasOwnProperty(p)) {
  332. arg2(p, arg1[p]);
  333. }
  334. }
  335. } else if (this.nodeList instanceof Array || this.nodeList instanceof NodeList) {
  336. Array.prototype.forEach.call(this.nodeList, arg1);
  337. }
  338.  
  339. return _$(this.selector);
  340. }
  341.  
  342. iterateOverObject(obj, fn) {
  343. if (obj instanceof Object) {
  344. for (var p in obj) {
  345. if (obj.hasOwnProperty(p)) {
  346. fn(p, obj[p]);
  347. }
  348. }
  349. }
  350.  
  351. return _$(this.selector);
  352. }
  353.  
  354. }
  355.  
  356. function _$(selector) {
  357. return new underdollar(selector);
  358. }