BrickLink - Navigate Between Orders

Adds buttons to quickly switch between orders on the Order Detail page.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        BrickLink - Navigate Between Orders
// @name:en     BrickLink - Navigate Between Orders
// @namespace   Violentmonkey Scripts
// @match       https://www.bricklink.com/orderPlaced.asp*
// @match       https://www.bricklink.com/orderDetail.asp*
// @grant       GM.setValue
// @grant       GM.getValue
// @version     0.2
// @author      The0x539
// @description Adds buttons to quickly switch between orders on the Order Detail page.
// @license     AGPL-3.0
// ==/UserScript==

function stashOrderIds() {
	const elements = document.querySelectorAll('table.orders-table > * > tr:not(:first-child) > td:first-child > a');
	const ids = [...elements].map(id => id.textContent);
	ids.sort();
	GM.setValue('order-ids', ids);

	// TODO: Improve this to use data from past page loads rather than replacing everything with whatever was seen most recently
}

async function addLinks() {
	const header = document.querySelector('center > div');

	const mainHeader = header.firstElementChild;
	mainHeader.classList.replace('left', 'center');

	const ids = await GM.getValue('order-ids', []);

	if (ids.length < 2) return;

	const currentId = new URLSearchParams(location.search).get('ID');
	const i = ids.findIndex(v => v === currentId);
	if (i < 0) return;

	if (i - 1 > 0) {
		const link = document.createElement('a');
		link.href = `orderDetail.asp?ID=${ids[i-1]}`;
		link.innerHTML = '<< Previous';
		link.style.float = 'left';
		header.insertBefore(link, mainHeader);
	}
	if (i + 1 < ids.length) {
		const link = document.createElement('a');
		link.href = `orderDetail.asp?ID=${ids[i+1]}`;
		link.innerHTML = 'Next >>';
		link.style.float = 'right';
		header.insertBefore(link, mainHeader);
	}
}

if (location.pathname.includes('orderPlaced.asp')) {
	stashOrderIds();
} else {
	addLinks();
}