Pick-A-Brick LDraw Colors

Replaces LEGO color names with LDraw color names on Pick a Brick

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Pick-A-Brick LDraw Colors
// @name:en     Pick-A-Brick LDraw Colors
// @description Replaces LEGO color names with LDraw color names on Pick a Brick
// @namespace   Violentmonkey Scripts
// @match       https://www.lego.com/en-us/pick-and-build/pick-a-brick*
// @grant       none
// @version     1.2.1
// @author      The0x539
// @run-at      document-start
// @license     AGPL-3.0
// ==/UserScript==
/* jshint esversion: 11 */

const gistUrl = "https://gist.githubusercontent.com/The0x539/d046e6778f38232912c6ca1d840d614e/raw";
const colorMapPromise = fetch(gistUrl).then(resp => resp.json()).then(body => new Map(body));

const actualFetch = window.fetch;
window.fetch = async (...args) => {
	const response = await actualFetch(...args);
	if (args[0] !== 'https://www.lego.com/api/graphql/PickABrickQuery') {
		return response;
	}

	const body = await response.json();
	const colorMap = await colorMapPromise;
	replaceNames(body, colorMap);

	// Reconstruct the response because we already consumed the body of the original response object.
	const { status, statusText, headers } = response;
	const options = { status, statusText, headers };
	return new Response(JSON.stringify(body), options);
};

function replaceNames(body, colorMap) {
	for (const facet of body.data.searchElements.facets) {
		if (facet.id !== "element.facet.colourFamily") {
			continue;
		}

		for (const label of facet.labels) {
			for (const child of label.children) {
				if (colorMap.has(child.key)) {
					child.name = colorMap.get(child.key);
				}
			}
		}
	}

	for (const result of body.data.searchElements.results) {
		const colorFacet = result.facets.color;
		if (colorMap.has(colorFacet.key)) {
			colorFacet.name = colorMap.get(colorFacet.key);
		}
	}
}