diff --git a/src/background/background.js b/src/background/background.js index 584f21ba5..502047525 100644 --- a/src/background/background.js +++ b/src/background/background.js @@ -1380,9 +1380,9 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { } else if (request.feature === 'viewid') { request.ids.forEach((id) => { chrome.storage.local.get( - ['viewIdEnabled'], + ['viewIdEnabled2'], (settings) => { - if (settings.viewIdEnabled) { + if (settings.viewIdEnabled2) { chrome.contextMenus.create({ id: `rovalra-viewid-${id}`, title: request.data.title, diff --git a/src/background/settingsCompat.ts b/src/background/settingsCompat.ts index 46f54eeb8..8f1ea8ba2 100644 --- a/src/background/settingsCompat.ts +++ b/src/background/settingsCompat.ts @@ -3,6 +3,11 @@ const settingDeprecations: Record Promise, sets: (key: string, value: any) => void) => void) | undefined> = { "EnableGameTrailer": undefined, "currencyTransferEnabled": undefined, + "viewIdEnabled": async (value, gets, sets) => { + if (await gets("viewIdEnabled2") === undefined) { + sets("viewIdEnabled2", value); + } + }, }; diff --git a/src/content/core/settings/settingConfig.js b/src/content/core/settings/settingConfig.js index 4ef06a8ce..c3191f87e 100644 --- a/src/content/core/settings/settingConfig.js +++ b/src/content/core/settings/settingConfig.js @@ -3752,7 +3752,7 @@ export const SETTINGS_CONFIG = { }, }, }, - viewIdEnabled: { + viewIdEnabled2: { label: 'View Bundled IDs', description: 'Allows you to view all bundles items and their IDs of any catalog item.', diff --git a/src/content/features/sitewide/viewid.ts b/src/content/features/sitewide/viewid.ts deleted file mode 100644 index b244817a2..000000000 --- a/src/content/features/sitewide/viewid.ts +++ /dev/null @@ -1,296 +0,0 @@ -import { callRobloxApiJson } from '../../core/api.js'; -import { getAssetIdFromUrl } from '../../core/idExtractor.js'; -import { t } from '../../core/locale/i18n.js'; -import { createButton } from '../../core/ui/buttons.js'; -import { createOverlay } from '../../core/ui/overlay.js'; -import { awaitSafe, sleep } from '../../core/utils/async.js'; -import { parseMarkdown } from '../../core/utils/markdown.js'; - -type RequestType = { - action: 'view-ids', - data: { - targetId: string | number - } -}; - -const CONFIG = Object.freeze({ - PrimaryTableID: "rovalra-viewid-" + crypto.randomUUID(), - TaxonomyTableID: "rovalra-viewid-" + crypto.randomUUID(), - BundledItemsID: "rovalra-viewid-" + crypto.randomUUID(), - - NoteText: "Click to copy.", - NoteTextClicked: "Copied!" -} as const); - -const popupMarkdown = ` -## Primary - -| Property | Value | -| -------- | ----- | -| Asset ID | {{assetid}} | -| Product ID | {{productid}} | -

${CONFIG.NoteText}

- -## Taxonomy - -| # | Name | ID | -| - | ---- | -- | -{{taxonomy}} -

${CONFIG.NoteText}

- -{{bundledsection}} -`; - -const bundledSectionMarkdown = ` -## Bundled Items - -| # | Name | Type | ID | -| - | ---- | ---- | -- | -{{bundled}} -

${CONFIG.NoteText}

-`; - -function formatUIMarkdown(assetId: number, productId: number, taxonomy: Array<{taxonomyName: string, taxonomyId: string}>, - bundledItems: Array<{name: string, type: string, id: number}>): [HTMLElement, string, string] { - - taxonomy = Array.isArray(taxonomy) ? taxonomy : []; - bundledItems = Array.isArray(bundledItems) - ? bundledItems.filter((item) => item?.id != null) - : []; - - let resultMarkdown = popupMarkdown; - const copyableId = (id: string | number) => - `${String(id)}`; - - resultMarkdown = resultMarkdown.replaceAll("{{assetid}}", copyableId(assetId)); - resultMarkdown = resultMarkdown.replaceAll("{{productid}}", copyableId(productId)); - - let taxonomyMarkdown = ""; - for (let i = 0; i < taxonomy.length; i++) { - const item = taxonomy[i]; - taxonomyMarkdown += `| ${i} | ${item.taxonomyName} | ${copyableId(item.taxonomyId)} |\n`; - } - if (taxonomy.length === 0) - taxonomyMarkdown = `| | | |\n`; - resultMarkdown = resultMarkdown.replaceAll("{{taxonomy}}", taxonomyMarkdown); - - let bundledMarkdown = ""; - for (let i = 0; i < bundledItems.length; i++) { - const item = bundledItems[i]; - bundledMarkdown += `| ${i} | ${item.name} | ${item.type} | ${copyableId(item.id)} |\n`; - } - if (bundledItems.length === 0) - bundledMarkdown = `| | | | |\n`; - resultMarkdown = resultMarkdown.replaceAll("{{bundled}}", bundledMarkdown); - resultMarkdown = resultMarkdown.replace( - "{{bundledsection}}", - bundledItems.length > 0 - ? bundledSectionMarkdown.replace("{{bundled}}", bundledMarkdown) - : "", - ); - - let resultHtml = parseMarkdown(resultMarkdown); - - const container = document.createElement("div"); - container.innerHTML = resultHtml; - - const tables = Array.from(container.querySelectorAll("table")); - const primaryTable = tables[0]; - const taxonomyTable = tables[1]; - const bundledTable = tables[2]; - [primaryTable, taxonomyTable, bundledTable].forEach((table, index) => { - if (!table) return; - table.id = [CONFIG.PrimaryTableID, CONFIG.TaxonomyTableID, CONFIG.BundledItemsID][index]; - table.classList.add("rovalra-viewid-table"); - }); - - const getTableNote = (table?: HTMLTableElement) => - table?.nextElementSibling?.querySelector("small") ?? null; - - async function onEvent(table?: HTMLTableElement) { - const noteElement = getTableNote(table); - if (!noteElement || noteElement === null) { - console.warn(`Note element not found.`); - return; - } - noteElement.textContent = CONFIG.NoteTextClicked; - await sleep(1000); - noteElement.textContent = CONFIG.NoteText; - } - - container.querySelectorAll(".rovalra-viewid-single-copy").forEach((element) => { - element.addEventListener("click", async (event) => { - event.stopPropagation(); - const value = element.dataset.copyValue; - if (!value) return; - - await awaitSafe(navigator.clipboard.writeText.bind(navigator.clipboard), value); - await onEvent(element.closest("table") ?? undefined); - }); - }); - - const getPrimary = () => `Asset ID: rbxassetid://${assetId}\nProduct ID: rbxassetid://${productId}\n`; - const getTaxonomy = () => { - let text = ""; - for (let i = 0; i < taxonomy.length; i++) { - const taxonomyItem = taxonomy[i]; - text += `${i} - "${taxonomyItem.taxonomyName}" - ${taxonomyItem.taxonomyId}\n`; - } - return text; - } - const getBundled = () => { - let text = ""; - for (let i = 0; i < bundledItems.length; i++) { - const bundleItem = bundledItems[i]; - text += `${bundleItem.type} "${bundleItem.name}": rbxassetid://${bundleItem.id}\n`; - } - return text; - } - - primaryTable?.addEventListener("click", async () => { - await awaitSafe(navigator.clipboard.writeText.bind(navigator.clipboard), getPrimary()); - await onEvent(primaryTable); - }); - - taxonomyTable?.addEventListener("click", async () => { - const text = getTaxonomy(); - if (text) - await awaitSafe(navigator.clipboard.writeText.bind(navigator.clipboard), text); - await onEvent(taxonomyTable); - }); - - bundledTable?.addEventListener("click", async () => { - const text = getBundled(); - if (text) - await awaitSafe(navigator.clipboard.writeText.bind(navigator.clipboard), text); - await onEvent(bundledTable); - }); - - const fullCopy = getPrimary() + "\nTaxonomy:\n" + getTaxonomy() + "\nBundled Items:\n" + getBundled(); - - return [container, resultMarkdown, fullCopy]; -} - -async function HandleMessage(request: RequestType) { - request = request as RequestType; - const id = Number(request.data.targetId); - - const bundledata = (await awaitSafe(callRobloxApiJson, { - subdomain: 'catalog', - endpoint: `/v1/bundles/details?bundleIds=${id}` - }))?.[0]; - - const itemType: number = (!!bundledata) ? 2 : 1; - - const itemdata = (await awaitSafe(callRobloxApiJson, { - subdomain: 'catalog', - endpoint: '/v1/catalog/items/details', - method: 'POST', - body: { - "items": [ - { - "itemType": itemType, - "id": id - } - ] - } - }))?.["data"]?.[0]; - - if (itemdata === undefined) - console.error(`RoValra: Failed to retrieve item data for ${itemType === 1 ? "Asset" : "Bundle"} ${id}.`) - const result = formatUIMarkdown(itemdata.id, itemdata.productId, itemdata.taxonomy, itemdata.bundledItems); - const bodyContentContainer = result[0]; - const bodyContentMarkdown = result[1]; - const fullCopyableData = result[2]; - - const copyHtmlBtn = createButton(await t('viewid.popup.button.copyMd'), 'secondary', { - onClick: async () => { - await navigator.clipboard.writeText(bodyContentMarkdown); - }, - id: "rovalra-viewid-button-sec" - }) - - const copyDataBtn = createButton(await t('viewid.popup.button.copy'), 'primary', { - onClick: async () => { - await navigator.clipboard.writeText(fullCopyableData); - }, - id: "rovalra-viewid-button-main" - }) - - const overlay = createOverlay( - { - title: await t(`viewid.popup.title`), - bodyContent: bodyContentContainer, - showLogo: true, - actions: [copyDataBtn, copyHtmlBtn], - onClose: () => { }, - } - ); -} - -async function OnMouseDown(event: MouseEvent) { - if (event.button !== 2) return; - - const link = event.target?.closest('a'); - const ids = []; - const translation = await t("viewid.viewId"); - - if (link) { - const url = link.href; - - const bundleMatch = url.match(/\/bundles\/(\d+)/); - const catalogMatch = url.match(/\/catalog\/(\d+)/); - const gamePassMatch = url.match(/\/game-pass\/(\d+)/); - const badgeMatch = url.match(/\/badges\/(\d+)/); - const groupMatch = url.match(/\/(?:groups|communities)\/(\d+)/); - const eventMatch = url.match(/\/events\/(\d+)/); - const devProductMatch = url.match( - /\/developer-product\/\d+\/product\/(\d+)/, - ); - - let targetId; - - if (bundleMatch) { - ids.push(bundleMatch[1]); - } else if (catalogMatch) { - ids.push(catalogMatch[1]); - } else if (gamePassMatch) { - ids.push(gamePassMatch[1]); - } else if (badgeMatch) { - ids.push(badgeMatch[1]); - } else if (groupMatch) { - ids.push(groupMatch[1]); - } else if (eventMatch) { - ids.push(eventMatch[1]); - } else if (devProductMatch) { - ids.push(devProductMatch[1]); - } else { - const assetId = getAssetIdFromUrl(url); - if (assetId) - ids.push(assetId); - } - } - - chrome.runtime.sendMessage({ - action: 'updateContextMenu', - feature: 'viewid', - ids: ids, - data: { - title: translation - } - }); -} - -export function init() { - chrome.runtime.onMessage.addListener(async (request: any | RequestType) => { - if (request.action === 'view-ids') { - await HandleMessage(request); - } - }); - - document.addEventListener( - 'mousedown', - OnMouseDown, - { capture: true }, - ); -} diff --git a/src/content/features/sitewide/viewid.tsx b/src/content/features/sitewide/viewid.tsx new file mode 100644 index 000000000..1263c7357 --- /dev/null +++ b/src/content/features/sitewide/viewid.tsx @@ -0,0 +1,371 @@ +import { render } from 'preact'; +import { callRobloxApiJson } from '../../core/api.js'; +import { getAssetIdFromUrl } from '../../core/idExtractor.js'; +import { t } from '../../core/locale/i18n.js'; +import { createButton } from '../../core/ui/buttons.js'; +import { createOverlay } from '../../core/ui/overlay.js'; +import { awaitSafe, sleep } from '../../core/utils/async.js'; +import { parseMarkdown } from '../../core/utils/markdown.js'; +import { useEffect, useState } from 'preact/hooks'; + +type RequestType = { + action: 'view-ids', + data: { + targetId: string | number + } +}; + +const CONFIG = Object.freeze({ + PrimaryTableID: "rovalra-viewid-" + crypto.randomUUID(), + TaxonomyTableID: "rovalra-viewid-" + crypto.randomUUID(), + BundledItemsID: "rovalra-viewid-" + crypto.randomUUID(), + + NoteText: "Click to copy.", + NoteTextClicked: "Copied!" +} as const); + +function getPrimary(assetId: number, productId: number) { + return `Asset ID: rbxassetid://${assetId}\nProduct ID: rbxassetid://${productId}\n`; +} + +function getTaxonomy(taxonomy: Array<{taxonomyName: string, taxonomyId: string}>) { + let text = ""; + for (let i = 0; i < taxonomy.length; i++) { + const taxonomyItem = taxonomy[i]; + text += `${i} - "${taxonomyItem.taxonomyName}" - ${taxonomyItem.taxonomyId}\n`; + } + return text; +} + +function getBundled(bundledItems: Array<{name: string, type: string, id: number}>) { + let text = ""; + for (let i = 0; i < bundledItems.length; i++) { + const bundleItem = bundledItems[i]; + text += `${bundleItem.type} "${bundleItem.name}": rbxassetid://${bundleItem.id}\n`; + } + return text; +} + +function getInfo(assetId: number, productId: number, taxonomy: Array<{taxonomyName: string, taxonomyId: string}>, + bundledItems: Array<{name: string, type: string, id: number}>): [string] { + + const fullCopy = getPrimary(assetId, productId) + "\nTaxonomy:\n" + getTaxonomy(taxonomy) + "\nBundled Items:\n" + getBundled(bundledItems); + + return [fullCopy]; +} + +function CopyableID({ copy_id }: { copy_id: string | number }) { + return { + awaitSafe(navigator.clipboard.writeText.bind(navigator.clipboard), String(copy_id)); + }}>{String(copy_id)}; +} + +function CopyLabel({ copy_data }: { copy_data: string }) { + return

{ + const element = event.currentTarget; + + const child = element.querySelector("small")!; + + child.textContent = CONFIG.NoteTextClicked; + + awaitSafe(navigator.clipboard.writeText.bind(navigator.clipboard), String(copy_data)); + + setTimeout(() => { + child.textContent = CONFIG.NoteText; + }, 5000); + }}>{CONFIG.NoteText}

+} + +function PrimarySection({ assetID, productID }: { assetID: string, productID: string }) { + return
+

Primary

+ + + + + + + + + + + + + +
PropertyValue
Asset ID
Product ID
+ +
; +} + +function TaxonomySection({ id }: { id: string }) { + const [data, setData] = useState(); + + useEffect(() => { + void (async () => { + const bundledata = (await awaitSafe(callRobloxApiJson, { + subdomain: 'catalog', + endpoint: `/v1/bundles/details?bundleIds=${id}` + }))?.[0]; + + const itemType: number = (!!bundledata) ? 2 : 1; + + const itemdata = (await awaitSafe(callRobloxApiJson, { + subdomain: 'catalog', + endpoint: '/v1/catalog/items/details', + method: 'POST', + body: { + "items": [ + { + "itemType": itemType, + "id": id + } + ] + } + }))?.["data"]?.[0]; + + setData(itemdata); + })(); + }, [id]); + + return
+

Taxonomy

+ + + + + + + { + ...(() => { + const elems = []; + + if (!data?.['taxonomy']) { + return []; + } + + const taxonomy = Array.isArray(data.taxonomy) ? data.taxonomy : []; + + for (let i = 0; i < taxonomy.length; i++) { + const item = taxonomy[i]; + + elems.push( + + + + ) + } + + return elems; + })() + } +
#PropertyValue
{i}{item.taxonomyName} + +
+ +
+} + +type Taxonomy = { + taxonomyName: string; + taxonomyId: string; +}[] +type BundledItems = { + name: string; + type: string; + id: number; +}[] + +function BundledItemsSection({ bundledItems }: {bundledItems: BundledItems }) { + return bundledItems.length !== 0 ?
+

Bundled Items

+ + + + + + + + { + ...(() => { + const elements = []; + + for (let i = 0; i < bundledItems.length; i++) { + const item = bundledItems[i]; + + elements.push( + + + + + ) + } + + return elements; + })() + } +
#NameTypeID
{i}{item.name}{item.type}{item.id}
+ +
: <>; +} + +function PopupBody({ item_id }: { item_id: string }) { + const [assetId, setAssetId] = useState("0"); + const [productId, setProductId] = useState("0"); + const [taxonomy, setTaxonomy] = useState([]); + const [bundledItems, setBundledItems] = useState([]); + + useEffect(() => { + void (async () => { + const bundledata = (await awaitSafe(callRobloxApiJson, { + subdomain: 'catalog', + endpoint: `/v1/bundles/details?bundleIds=${item_id}` + }))?.[0]; + + const itemType: number = (!!bundledata) ? 2 : 1; + + const itemdata = (await awaitSafe(callRobloxApiJson, { + subdomain: 'catalog', + endpoint: '/v1/catalog/items/details', + method: 'POST', + body: { + "items": [ + { + "itemType": itemType, + "id": item_id + } + ] + } + }))?.["data"]?.[0]; + + setAssetId(itemdata.id); + setProductId(itemdata.productId); + setTaxonomy(itemdata.taxonomy); + setBundledItems(itemdata.bundledItems); + })(); + }, [item_id]); + + return <> + + + + +} + +async function HandleMessage(request: RequestType) { + request = request as RequestType; + const id = Number(request.data.targetId); + + const bundledata = (await awaitSafe(callRobloxApiJson, { + subdomain: 'catalog', + endpoint: `/v1/bundles/details?bundleIds=${id}` + }))?.[0]; + + const itemType: number = (!!bundledata) ? 2 : 1; + + const itemdata = (await awaitSafe(callRobloxApiJson, { + subdomain: 'catalog', + endpoint: '/v1/catalog/items/details', + method: 'POST', + body: { + "items": [ + { + "itemType": itemType, + "id": id + } + ] + } + }))?.["data"]?.[0]; + + if (itemdata === undefined) { + console.error(`RoValra: Failed to retrieve item data for ${itemType === 1 ? "Asset" : "Bundle"} ${id}.`); + return; + } + const result = document.createElement("div"); + render(, result); + const bodyContentContainer = result; + const [ fullCopyableData ] = getInfo(itemdata.id, itemdata.productId, itemdata.taxonomy ?? [], itemdata.bundledItems ?? []); + + const copyDataBtn = createButton(await t('viewid.popup.button.copy'), 'primary', { + onClick: async () => { + await navigator.clipboard.writeText(fullCopyableData); + }, + id: "rovalra-viewid-button-main" + }) + + const overlay = createOverlay( + { + title: await t(`viewid.popup.title`), + bodyContent: bodyContentContainer, + showLogo: true, + actions: [copyDataBtn] as any, + onClose: () => { }, + } + ); +} + +async function OnMouseDown(event: MouseEvent) { + if (event.button !== 2) return; + + const link = event.target?.closest('a'); + const ids = []; + const translation = await t("viewid.viewId"); + + if (link) { + const url = link.href; + + const bundleMatch = url.match(/\/bundles\/(\d+)/); + const catalogMatch = url.match(/\/catalog\/(\d+)/); + const gamePassMatch = url.match(/\/game-pass\/(\d+)/); + const badgeMatch = url.match(/\/badges\/(\d+)/); + const groupMatch = url.match(/\/(?:groups|communities)\/(\d+)/); + const eventMatch = url.match(/\/events\/(\d+)/); + const devProductMatch = url.match( + /\/developer-product\/\d+\/product\/(\d+)/, + ); + + let targetId; + + if (bundleMatch) { + ids.push(bundleMatch[1]); + } else if (catalogMatch) { + ids.push(catalogMatch[1]); + } else if (gamePassMatch) { + ids.push(gamePassMatch[1]); + } else if (badgeMatch) { + ids.push(badgeMatch[1]); + } else if (groupMatch) { + ids.push(groupMatch[1]); + } else if (eventMatch) { + ids.push(eventMatch[1]); + } else if (devProductMatch) { + ids.push(devProductMatch[1]); + } else { + const assetId = getAssetIdFromUrl(url); + if (assetId) + ids.push(assetId); + } + } + + chrome.runtime.sendMessage({ + action: 'updateContextMenu', + feature: 'viewid', + ids: ids, + data: { + title: translation + } + }); +} + +export function init() { + chrome.runtime.onMessage.addListener(async (request: any | RequestType) => { + if (request.action === 'view-ids') { + await HandleMessage(request); + } + }); + + document.addEventListener( + 'mousedown', + OnMouseDown, + { capture: true }, + ); +} diff --git a/src/css/features/sitewide/viewid.scss b/src/css/features/sitewide/viewid.scss index 59796be53..ebac1f564 100644 --- a/src/css/features/sitewide/viewid.scss +++ b/src/css/features/sitewide/viewid.scss @@ -34,3 +34,18 @@ button#rovalra-viewid-button-main, button#rovalra-viewid-button-sec { scale: 0.95; } } + +.rovalra-viewid-copy { + cursor: pointer; + + transition: scale 0.1s ease; + + &:hover { + scale: 1.01; + transition: scale 0.1s ease; + } + + &:active { + scale: 0.99; + } +}