Add button to download wallpapers in app

This commit is contained in:
redphx 2024-09-03 19:56:34 +07:00
parent 5a0ef88237
commit 7310700dbb
3 changed files with 40 additions and 11 deletions

View File

@ -245,7 +245,7 @@ window.addEventListener(BxEvent.STREAM_ERROR_PAGE, e => {
window.addEventListener(BxEvent.XCLOUD_RENDERING_COMPONENT, e => { window.addEventListener(BxEvent.XCLOUD_RENDERING_COMPONENT, e => {
const component = (e as any).component; const component = (e as any).component;
if (component === 'product-details') { if (component === 'product-details') {
ProductDetailsPage.injectShortcutButton(); ProductDetailsPage.injectButtons();
} }
}); });

View File

@ -5,30 +5,60 @@ import { ButtonStyle, createButton } from "@/utils/html";
import { t } from "@/utils/translation"; import { t } from "@/utils/translation";
export class ProductDetailsPage { export class ProductDetailsPage {
private static $btnShortcut = createButton({ private static $btnShortcut = AppInterface && createButton({
classes: ['bx-button-shortcut'], classes: ['bx-button-shortcut'],
icon: BxIcon.CREATE_SHORTCUT, icon: BxIcon.CREATE_SHORTCUT,
label: t('create-shortcut'), label: t('create-shortcut'),
style: ButtonStyle.FOCUSABLE, style: ButtonStyle.FOCUSABLE,
tabIndex: 0, tabIndex: 0,
onClick: e => { onClick: e => {
AppInterface && AppInterface.createShortcut(window.location.pathname.substring(6)); AppInterface.createShortcut(window.location.pathname.substring(6));
}, },
}); });
private static shortcutTimeoutId: number | null = null; private static $btnWallpaper = AppInterface && createButton({
classes: ['bx-button-shortcut'],
icon: BxIcon.DOWNLOAD,
label: t('wallpaper'),
style: ButtonStyle.FOCUSABLE,
tabIndex: 0,
onClick: async e => {
try {
const matches = /\/games\/(?<titleSlug>[^\/]+)\/(?<productId>\w+)/.exec(window.location.pathname);
if (!matches?.groups) {
return;
}
static injectShortcutButton() { const titleSlug = matches.groups.titleSlug;
if (!AppInterface || BX_FLAGS.DeviceInfo.deviceType !== 'android') { const productId = matches.groups.productId;
AppInterface.downloadWallpapers(titleSlug, productId);
} catch (e) {}
},
});
private static injectTimeoutId: number | null = null;
static injectButtons() {
if (!AppInterface) {
return; return;
} }
ProductDetailsPage.shortcutTimeoutId && clearTimeout(ProductDetailsPage.shortcutTimeoutId); ProductDetailsPage.injectTimeoutId && clearTimeout(ProductDetailsPage.injectTimeoutId);
ProductDetailsPage.shortcutTimeoutId = window.setTimeout(() => { ProductDetailsPage.injectTimeoutId = window.setTimeout(() => {
// Find action buttons container // Find action buttons container
const $container = document.querySelector('div[class*=ActionButtons-module__container]'); const $container = document.querySelector('div[class*=ActionButtons-module__container]');
if ($container) { if ($container && $container.parentElement) {
$container.parentElement?.appendChild(ProductDetailsPage.$btnShortcut); const fragment = document.createDocumentFragment();
// Shortcut button
if (BX_FLAGS.DeviceInfo.deviceType === 'android') {
fragment.appendChild(ProductDetailsPage.$btnShortcut);
}
// Wallpaper button
fragment.appendChild(ProductDetailsPage.$btnWallpaper);
$container.parentElement.appendChild(fragment);
} }
}, 500); }, 500);
} }

View File

@ -1,4 +1,3 @@
import { BX_FLAGS } from "./bx-flags";
import { AppInterface, STATES } from "./global"; import { AppInterface, STATES } from "./global";
import { ButtonStyle, CE, createButton, getReactProps } from "./html"; import { ButtonStyle, CE, createButton, getReactProps } from "./html";
import { t } from "./translation"; import { t } from "./translation";