Bricklink - Wanted List: Auto-View Image

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

目前為 2014-11-05 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Bricklink - Wanted List: Auto-View Image
// @namespace    http://badwing.com/
// @version      0.1
// @description  Automatically views the image when adding an item to your wanted list.
// @author       Christopher Hiller
// @include      http://www.bricklink.com/wantedAddDetail.asp?*
// @require      //code.jquery.com/jquery-1.11.1.min.js 
// ==/UserScript==

// reference to jQuery object of the <img> tag
var img_thumb;

/**
 * Given an ID, put the image in the placeholder.
 * @param {string} id Part ID
 */
var getImage = function getImage(id) {
    var src = '/getPic.asp?itemType=P&itemNo=' + id;
    
    $.ajaxQueue(src).done(function(src) { 
        return function() {
            img_thumb.attr('src', src);
    	}; 
    }(src));
};

$(function() {
    var txt_itemId = $('input[name="p_selecteditemID"]'),
        val = txt_itemId.val();
    img_thumb = $('img[name="img1"]');
    
    // no longer necessary
    img_thumb.removeAttr('onerror');
    
    if (val) {
        getImage(val);
    }
    
    // update the img as the user types
    txt_itemId.keyup(function() {
        var val = this.value;
        if (val) {
            getImage(this.value);
        }        
    });
    
    // hide unneccessary garbage
    $('input[name="imageType"]').next().css('visibility', 'hidden');
});

/*! jQuery Ajax Queue - v0.1.2pre - 2013-03-19
* https://github.com/gnarf37/jquery-ajaxQueue
* Copyright (c) 2013 Corey Frang; Licensed MIT */
(function($) {

// jQuery on an empty object, we are going to use this as our Queue
var ajaxQueue = $({});

$.ajaxQueue = function( ajaxOpts ) {
    var jqXHR,
        dfd = $.Deferred(),
        promise = dfd.promise();

    // run the actual query
    function doRequest( next ) {
        jqXHR = $.ajax( ajaxOpts );
        jqXHR.done( dfd.resolve )
            .fail( dfd.reject )
            .then( next, next );
    }

    // queue our ajax request
    ajaxQueue.queue( doRequest );

    // add the abort method
    promise.abort = function( statusText ) {

        // proxy abort to the jqXHR if it is active
        if ( jqXHR ) {
            return jqXHR.abort( statusText );
        }

        // if there wasn't already a jqXHR we need to remove from queue
        var queue = ajaxQueue.queue(),
            index = $.inArray( doRequest, queue );

        if ( index > -1 ) {
            queue.splice( index, 1 );
        }

        // and then reject the deferred
        dfd.rejectWith( ajaxOpts.context || ajaxOpts, [ promise, statusText, "" ] );
        return promise;
    };

    return promise;
};

})(jQuery);