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.  
  28. /*! jQuery Ajax Queue - v0.1.2pre - 2013-03-19
  29. * https://github.com/gnarf37/jquery-ajaxQueue
  30. * Copyright (c) 2013 Corey Frang; Licensed MIT */
  31. (function($) {
  32.  
  33. // jQuery on an empty object, we are going to use this as our Queue
  34. var ajaxQueue = $({});
  35.  
  36. $.ajaxQueue = function( ajaxOpts ) {
  37. var jqXHR,
  38. dfd = $.Deferred(),
  39. promise = dfd.promise();
  40.  
  41. // run the actual query
  42. function doRequest( next ) {
  43. jqXHR = $.ajax( ajaxOpts );
  44. jqXHR.done( dfd.resolve )
  45. .fail( dfd.reject )
  46. .then( next, next );
  47. }
  48.  
  49. // queue our ajax request
  50. ajaxQueue.queue( doRequest );
  51.  
  52. // add the abort method
  53. promise.abort = function( statusText ) {
  54.  
  55. // proxy abort to the jqXHR if it is active
  56. if ( jqXHR ) {
  57. return jqXHR.abort( statusText );
  58. }
  59.  
  60. // if there wasn't already a jqXHR we need to remove from queue
  61. var queue = ajaxQueue.queue(),
  62. index = $.inArray( doRequest, queue );
  63.  
  64. if ( index > -1 ) {
  65. queue.splice( index, 1 );
  66. }
  67.  
  68. // and then reject the deferred
  69. dfd.rejectWith( ajaxOpts.context || ajaxOpts, [ promise, statusText, "" ] );
  70. return promise;
  71. };
  72.  
  73. return promise;
  74. };
  75.  
  76. })(jQuery);
  77.  
  78. $(function() {
  79. var txt_itemId = $('input[name="p_selecteditemID"]'),
  80. val = txt_itemId.val();
  81. img_thumb = $('img[name="img1"]');
  82. // no longer necessary
  83. img_thumb.removeAttr('onerror');
  84. if (val) {
  85. getImage(val);
  86. }
  87. // update the img as the user types
  88. txt_itemId.keyup(function() {
  89. var val = this.value;
  90. if (val) {
  91. getImage(this.value);
  92. }
  93. });
  94. // hide unneccessary garbage
  95. $('input[name="imageType"]').next().css('visibility', 'hidden');
  96. });
  97.