Bricklink - Wanted List: Auto-View Image

Automatically views the image when adding an item to your wanted list.

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

  1. // ==UserScript==
  2. // @name Bricklink - Wanted List: Auto-View Image
  3. // @namespace http://badwing.com/
  4. // @version 0.1
  5. // @description Automatically views the image when adding an item to your wanted list.
  6. // @author Christopher Hiller
  7. // @include http://www.bricklink.com/wantedAddDetail.asp?*
  8. // @require //code.jquery.com/jquery-1.11.1.min.js
  9. // ==/UserScript==
  10.  
  11. // reference to jQuery object of the <img> tag
  12. var img_thumb;
  13.  
  14. /**
  15. * Given an ID, put the image in the placeholder.
  16. * @param {string} id Part ID
  17. */
  18. var getImage = function getImage(id) {
  19. var src = '/getPic.asp?itemType=P&itemNo=' + id;
  20. $.ajaxQueue(src).done(function(src) {
  21. return function() {
  22. img_thumb.attr('src', src);
  23. };
  24. }(src));
  25. };
  26.  
  27. $(function() {
  28. var txt_itemId = $('input[name="p_selecteditemID"]'),
  29. val = txt_itemId.val();
  30. img_thumb = $('img[name="img1"]');
  31. // no longer necessary
  32. img_thumb.removeAttr('onerror');
  33. if (val) {
  34. getImage(val);
  35. }
  36. // update the img as the user types
  37. txt_itemId.keyup(function() {
  38. var val = this.value;
  39. if (val) {
  40. getImage(this.value);
  41. }
  42. });
  43. // hide unneccessary garbage
  44. $('input[name="imageType"]').next().css('visibility', 'hidden');
  45. });
  46.  
  47. /*! jQuery Ajax Queue - v0.1.2pre - 2013-03-19
  48. * https://github.com/gnarf37/jquery-ajaxQueue
  49. * Copyright (c) 2013 Corey Frang; Licensed MIT */
  50. (function($) {
  51.  
  52. // jQuery on an empty object, we are going to use this as our Queue
  53. var ajaxQueue = $({});
  54.  
  55. $.ajaxQueue = function( ajaxOpts ) {
  56. var jqXHR,
  57. dfd = $.Deferred(),
  58. promise = dfd.promise();
  59.  
  60. // run the actual query
  61. function doRequest( next ) {
  62. jqXHR = $.ajax( ajaxOpts );
  63. jqXHR.done( dfd.resolve )
  64. .fail( dfd.reject )
  65. .then( next, next );
  66. }
  67.  
  68. // queue our ajax request
  69. ajaxQueue.queue( doRequest );
  70.  
  71. // add the abort method
  72. promise.abort = function( statusText ) {
  73.  
  74. // proxy abort to the jqXHR if it is active
  75. if ( jqXHR ) {
  76. return jqXHR.abort( statusText );
  77. }
  78.  
  79. // if there wasn't already a jqXHR we need to remove from queue
  80. var queue = ajaxQueue.queue(),
  81. index = $.inArray( doRequest, queue );
  82.  
  83. if ( index > -1 ) {
  84. queue.splice( index, 1 );
  85. }
  86.  
  87. // and then reject the deferred
  88. dfd.rejectWith( ajaxOpts.context || ajaxOpts, [ promise, statusText, "" ] );
  89. return promise;
  90. };
  91.  
  92. return promise;
  93. };
  94.  
  95. })(jQuery);