ClockworkSquirrel / ROBLOX - Get Decal ImageID

// ==UserScript==
// @name         ROBLOX - Get Decal ImageID
// @description  Gets and displays the ID of a decal's image.
//
// @author       ClockworkSquirrel
// @version      1.0.0
//
// @icon         http://svgshare.com/i/a9.svg
//
// @require      https://code.jquery.com/jquery-3.1.1.min.js
// @match        https://*.roblox.com/library/*/*
//
// @connect      api.roblox.com
//
// @run-at       document-start
//
// @grant        GM_xmlhttpRequest
// @grant        GM_setClipboard
// ==/UserScript==

(function() {
	'use strict';

	var res = {
		api: "https://api.roblox.com",
		assetTypeId: 1,
		maxAttempts: 16,
		loadingImg: "https://static.rbxcdn.com/images/Shared/loading.gif",
	};

	var scriptRunning = false;
	function documentReady(){
		if (scriptRunning) return;
		scriptRunning = true;

		let container = $("div#item-container[data-item-id][data-asset-type][data-user-id]:eq(0)");
		if (container !== null && container !== undefined){
			let assetId = container.attr("data-item-id"), assetType = container.attr("data-asset-type"), creatorId = container.attr("data-user-id");

			if (assetType.toLowerCase() == "decal"){
				let foundImage, attempts = 1, checkAssetTimer, processing = false;
				let loadingImg = $(document.createElement("img")).attr("src", res.loadingImg).height("16px")
				.css("display", "block").css("margin", "0 auto 12px auto")
				.insertBefore("div#item-details:eq(0) *:eq(0)");

				let checkAssetId = function(){
					if (processing) return;
					processing = true;

					if (foundImage === null || attempts <= res.maxAttempts){
						GM_xmlhttpRequest({
							method: "GET",
							url: res.api+"/marketplace/productinfo?assetId="+(assetId-attempts),
							onload: function(response){
								attempts++;

								if (response.status === 200 && response.responseText.length > 0){
									let data = $.parseJSON(response.responseText);
									
									if (data.AssetTypeId == res.assetTypeId && data.Creator.Id == creatorId){
										clearInterval(checkAssetTimer); foundImage = data.AssetId;
										loadingImg.remove();
										
										$(document.createElement("input")).addClass("copy-to-clipboard form-control input-field")
										.attr("type", "text").attr("readonly", true).css("margin-bottom", "12px").val(foundImage)
										.css("cursor", "pointer").click(function(evt){evt.preventDefault();$(this).select();})
										
										.contextmenu(function(evt){
											if (!evt.ctrlKey){
												evt.preventDefault();
												$(this).select();

												GM_setClipboard(foundImage);

												try{
													let popup = $(".content .alert-success").text("Copied to Clipboard!");
													Roblox.BootstrapWidgets.ToggleSystemMessage(popup, 0, 3e3);
												}catch(_){}
											}
										})
										
										.insertBefore("div#item-details:eq(0) *:eq(0)");
									}
								}

								processing = false;
							}
						});
					}else if(attempts > res.maxAttempts){
						clearInterval(checkAssetTimer);
						loadingImg.remove();
						
						try{
							let popup = $(".content .alert-warning").text("Couldn't find Image ID ("+(attempts-1)+" attempts)");
							Roblox.BootstrapWidgets.ToggleSystemMessage(popup, 0, 3e3);
						}catch(_){}
					}
				};
				
				checkAssetTimer = setInterval(checkAssetId, 1e2);
			}
		}
	}

	var checkTimer;
	checkTimer = setInterval(function(){
		let itemContainer = document.getElementById("item-container");
		if ((itemContainer !== null && itemContainer !== undefined) && ($ !== null && $ !== undefined)){
			clearInterval(checkTimer);
			documentReady();
		}
	}, 1e3);
})();