BrickLink - Wanted List: Automatically Load Images

Automatically load images when adding items to wanted list

目前为 2014-11-08 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name BrickLink - Wanted List: Automatically Load Images
  3. // @namespace http://badwing.com/
  4. // @homepage https://github.com/boneskull/bricklink-scripts
  5. // @version 0.1.1
  6. // @description Automatically load images when adding items to wanted list
  7. // @author Christopher Hiller <chiller@badwing.com>
  8. // @include http://www.bricklink.com/wantedAddDetail.asp?act=a*
  9. // @require //code.jquery.com/jquery-1.11.1.min.js
  10. // @require //cdnjs.cloudflare.com/ajax/libs/spin.js/2.0.1/jquery.spin.min.js
  11. // ==/UserScript==
  12. // AjaxQ jQuery Plugin
  13. // Copyright (c) 2012 Foliotek Inc.
  14. // MIT License
  15. // https://github.com/Foliotek/ajaxq
  16.  
  17. (function($) {
  18.  
  19. var queues = {};
  20. var activeReqs = {};
  21.  
  22. // Register an $.ajaxq function, which follows the $.ajax interface, but allows a queue name which will force only one request per queue to fire.
  23. $.ajaxq = function(qname, opts) {
  24.  
  25. if (typeof opts === "undefined") {
  26. throw ("AjaxQ: queue name is not provided");
  27. }
  28.  
  29. // Will return a Deferred promise object extended with success/error/callback, so that this function matches the interface of $.ajax
  30. var deferred = $.Deferred(),
  31. promise = deferred.promise();
  32.  
  33. promise.success = promise.done;
  34. promise.error = promise.fail;
  35. promise.complete = promise.always;
  36.  
  37. // Create a deep copy of the arguments, and enqueue this request.
  38. var clonedOptions = $.extend(true, {}, opts);
  39. enqueue(function() {
  40. // Send off the ajax request now that the item has been removed from the queue
  41. var jqXHR = $.ajax.apply(window, [clonedOptions]);
  42.  
  43. // Notify the returned deferred object with the correct context when the jqXHR is done or fails
  44. // Note that 'always' will automatically be fired once one of these are called: http://api.jquery.com/category/deferred-object/.
  45. jqXHR.done(function() {
  46. deferred.resolve.apply(this, arguments);
  47. });
  48. jqXHR.fail(function() {
  49. deferred.reject.apply(this, arguments);
  50. });
  51.  
  52. jqXHR.always(dequeue); // make sure to dequeue the next request AFTER the done and fail callbacks are fired
  53. return jqXHR;
  54. });
  55.  
  56. return promise;
  57.  
  58.  
  59. // If there is no queue, create an empty one and instantly process this item.
  60. // Otherwise, just add this item onto it for later processing.
  61. function enqueue(cb) {
  62. if (!queues[qname]) {
  63. queues[qname] = [];
  64. var xhr = cb();
  65. activeReqs[qname] = xhr;
  66. }
  67. else {
  68. queues[qname].push(cb);
  69. }
  70. }
  71.  
  72. // Remove the next callback from the queue and fire it off.
  73. // If the queue was empty (this was the last item), delete it from memory so the next one can be instantly processed.
  74. function dequeue() {
  75. if (!queues[qname]) {
  76. return;
  77. }
  78. var nextCallback = queues[qname].shift();
  79. if (nextCallback) {
  80. var xhr = nextCallback();
  81. activeReqs[qname] = xhr;
  82. }
  83. else {
  84. delete queues[qname];
  85. delete activeReqs[qname];
  86. }
  87. }
  88. };
  89.  
  90. // Register a $.postq and $.getq method to provide shortcuts for $.get and $.post
  91. // Copied from jQuery source to make sure the functions share the same defaults as $.get and $.post.
  92. $.each( [ "getq", "postq" ], function( i, method ) {
  93. $[ method ] = function( qname, url, data, callback, type ) {
  94.  
  95. if ( $.isFunction( data ) ) {
  96. type = type || callback;
  97. callback = data;
  98. data = undefined;
  99. }
  100.  
  101. return $.ajaxq(qname, {
  102. type: method === "postq" ? "post" : "get",
  103. url: url,
  104. data: data,
  105. success: callback,
  106. dataType: type
  107. });
  108. };
  109. });
  110.  
  111. var isQueueRunning = function(qname) {
  112. return queues.hasOwnProperty(qname);
  113. };
  114.  
  115. var isAnyQueueRunning = function() {
  116. for (var i in queues) {
  117. if (isQueueRunning(i)) return true;
  118. }
  119. return false;
  120. };
  121.  
  122. $.ajaxq.isRunning = function(qname) {
  123. if (qname) return isQueueRunning(qname);
  124. else return isAnyQueueRunning();
  125. };
  126.  
  127. $.ajaxq.getActiveRequest = function(qname) {
  128. if (!qname) throw ("AjaxQ: queue name is required");
  129.  
  130. return activeReqs[qname];
  131. };
  132.  
  133. $.ajaxq.abort = function(qname) {
  134. if (!qname) throw ("AjaxQ: queue name is required");
  135. var current = $.ajaxq.getActiveRequest(qname);
  136. delete queues[qname];
  137. delete activeReqs[qname];
  138. if (current) current.abort();
  139. };
  140.  
  141. $.ajaxq.clear = function(qname) {
  142. if (!qname) {
  143. for (var i in queues) {
  144. if (queues.hasOwnProperty(i)) {
  145. queues[i] = [];
  146. }
  147. }
  148. }
  149. else {
  150. if (queues[qname]) {
  151. queues[qname] = [];
  152. }
  153. }
  154. };
  155.  
  156. })(jQuery);
  157.  
  158. (function () {
  159. 'use strict';
  160.  
  161. var img_thumb,
  162. getImage,
  163. getUrlParam;
  164.  
  165. /**
  166. * Given a Location object oTarget, parse the value of a particular parameter
  167. * of its search string.
  168. * @see https://developer.mozilla.org/en-US/docs/Web/API/Location
  169. * @see https://developer.mozilla.org/en-US/docs/Web/API/URLUtils.search
  170. * @param {Location} oTarget Location object
  171. * @param {string} sVar String to parse
  172. * @returns {string}
  173. */
  174. getUrlParam = function getURLParam(oTarget, sVar) {
  175. return decodeURI(oTarget.search.replace(new RegExp('^(?:.*[&\\?]' +
  176. encodeURI(sVar).replace(/[\.\+\*]/g, '\\$&') + '(?:\\=([^&]*))?)?.*$', 'i'),
  177. '$1'));
  178. };
  179.  
  180. /**
  181. * Given an ID, put the image in the placeholder.
  182. * @param {string} id Part ID
  183. */
  184. getImage = function getImage(id) {
  185. var item_type = getUrlParam(window.location, 'a'),
  186. src = '/getPic.asp?itemType=' + item_type + '&itemNo=' + id;
  187.  
  188. $.getq(src).done(function (src) {
  189. return function () {
  190. img_thumb.attr('src', src);
  191. };
  192. }(src));
  193. };
  194.  
  195. $(function () {
  196. var txt_itemId = $('input[name="p_selecteditemID"]'),
  197. val = txt_itemId.val(),
  198. anchor = $('<a href="#" title="Click to Refresh"></a>')
  199. .click(function () {
  200. getImage(txt_itemId.val());
  201. });
  202.  
  203. img_thumb = $('img[name="img1"]');
  204.  
  205. // remove error handler; no longer necessary
  206. // wrap in anchor and bind the click event to refresh.
  207. img_thumb.removeAttr('onerror')
  208. .wrap(anchor);
  209.  
  210. // if we already have a value, get the image for it
  211. if (val) {
  212. getImage(val);
  213. }
  214.  
  215. // update the img as the user types
  216. txt_itemId.keyup(function () {
  217. var val = this.value;
  218. if (val) {
  219. getImage(this.value);
  220. }
  221. });
  222.  
  223. // hide unneccessary garbage
  224. $('input[name="imageType"]').next().css('visibility', 'hidden');
  225. });
  226.  
  227.  
  228. })();